2017-08-16 115 views
2

我用玩具例如打認識PCA VS keras的自動編碼keras自動編碼器VS PCA

我已經理解PCA下面的代碼:

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from sklearn import decomposition 
from sklearn import datasets 

iris = datasets.load_iris() 
X = iris.data 
pca = decomposition.PCA(n_components=3) 
pca.fit(X) 

pca.explained_variance_ratio_ 
array([ 0.92461621, 0.05301557, 0.01718514]) 

pca.components_ 
array([[ 0.36158968, -0.08226889, 0.85657211, 0.35884393], 
     [ 0.65653988, 0.72971237, -0.1757674 , -0.07470647], 
     [-0.58099728, 0.59641809, 0.07252408, 0.54906091]]) 

我已經做了一些閱讀和玩碼包括this one

但是,參考代碼對我的理解程度來說感覺太高了。

是否有人有一個簡短的自動編碼器代碼,可以告訴我

(1)如何從自動編碼器來拉頭3個組件

(2)如何理解量方差的自動編碼器捕獲

(3)自動編碼器組件如何與之比較的PCA分量

+0

https://stackoverflow.com/questions/47842931/valueerror-error-when-checking-target-expected-model-2-to-have-shape-none-25/47847014#47847014有什麼建議嗎? –

回答

2

首先,自動編碼器的目的是要學習的表示(編碼)爲一組數據,典型地一毛錢的目的不變性減少。所以,autoencoder的目標輸出是自動編碼器輸入本身。

如[1]所示,如果存在一個線性隱層並且使用均方誤差準則來訓練網絡,那麼隱藏單元學習將投入投影在first k principal components的範圍內數據。 在[2]中可以看到,如果隱藏層是非線性的,則自動編碼器的行爲與PCA的行爲不同,能夠捕獲輸入分佈的多模態特徵。

自動編碼器是特定於數據的,這意味着它們只能壓縮類似於他們所訓練的數據。因此,隱藏層學到的特徵的有用性可用於評估該方法的功效。

因此,評估自動編碼器降維效果的一種方法是切割中間隱藏層的輸出,並通過減少的數據比較所需算法的準確性/性能,而不是使用原始數據。 一般來說,PCA是一種線性方法,而自動編碼器通常是非線性的。在數學上,很難將它們比較在一起,但直觀上我使用Autoencoder提供了一個降低MNIST數據集維數的例子,以便更好地理解。該代碼是在這裏:

from keras.datasets import mnist 
from keras.models import Model 
from keras.layers import Input, Dense 
from keras.utils import np_utils 
import numpy as np 

num_train = 60000 
num_test = 10000 

height, width, depth = 28, 28, 1 # MNIST images are 28x28 
num_classes = 10 # there are 10 classes (1 per digit) 

(X_train, y_train), (X_test, y_test) = mnist.load_data() 

X_train = X_train.reshape(num_train, height * width) 
X_test = X_test.reshape(num_test, height * width) 
X_train = X_train.astype('float32') 
X_test = X_test.astype('float32') 

X_train /= 255 # Normalise data to [0, 1] range 
X_test /= 255 # Normalise data to [0, 1] range 

Y_train = np_utils.to_categorical(y_train, num_classes) # One-hot encode the labels 
Y_test = np_utils.to_categorical(y_test, num_classes) # One-hot encode the labels 

input_img = Input(shape=(height * width,)) 

x = Dense(height * width, activation='relu')(input_img) 

encoded = Dense(height * width//2, activation='relu')(x) 
encoded = Dense(height * width//8, activation='relu')(encoded) 

y = Dense(height * width//256, activation='relu')(x) 

decoded = Dense(height * width//8, activation='relu')(y) 
decoded = Dense(height * width//2, activation='relu')(decoded) 

z = Dense(height * width, activation='sigmoid')(decoded) 
model = Model(input_img, z) 

model.compile(optimizer='adadelta', loss='mse') # reporting the accuracy 

model.fit(X_train, X_train, 
     epochs=10, 
     batch_size=128, 
     shuffle=True, 
     validation_data=(X_test, X_test)) 

mid = Model(input_img, y) 
reduced_representation =mid.predict(X_test) 

out = Dense(num_classes, activation='softmax')(y) 
reduced = Model(input_img, out) 
reduced.compile(loss='categorical_crossentropy', 
      optimizer='adam', 
      metrics=['accuracy']) 

reduced.fit(X_train, Y_train, 
     epochs=10, 
     batch_size=128, 
     shuffle=True, 
     validation_data=(X_test, Y_test)) 


scores = reduced.evaluate(X_test, Y_test, verbose=1) 
print("Accuracy: ", scores[1]) 

它產生在\ mathbb {R}一$ Y \^{3} $(就像你通過decomposition.PCA(n_components=3)得到什麼)。例如,在這裏你看到層y的輸出爲數字5例如在數據集:

class y_1 y_2  y_3  
    5  87.38 0.00 20.79 

正如你在上面的代碼中看到,當我們層y連接到softmax緻密層:

mid = Model(input_img, y) 
reduced_representation =mid.predict(X_test) 

新模型mid給我們提供了一個很好的關於95%的分類精度。因此,可以說y是數據集的高效提取特徵向量。


參考文獻:

[1]:Bourlard,埃爾韋,和Yves坎普。 「通過多層感知器和奇異值分解進行自動關聯」。生物控制論59.4(1988):291-294。

[2]:Japkowicz,Nathalie,Stephen Jose Hanson和Mark A.Gluck。 「非線性自動關聯不等同於PCA。」神經計算12.3(2000):531-545。

+0

https://stackoverflow.com/questions/47842931/valueerror-error-when-checking-target-expected-model-2-to-have-shape-none-25/47847014#47847014有什麼建議嗎? –