2017-12-13 6 views
0

Ich versuche, die 2d Faltungsautoencoder Beispiel aus der keras Webseite anzupassen: https://blog.keras.io/building-autoencoders-in-keras.htmlkeras Autoencoder "Fehler bei der Überprüfung Ziel"

zu meinem eigenen Fall, in dem i 1d Eingänge verwenden:

from keras.layers import Input, Dense, Conv1D, MaxPooling1D, UpSampling1D 
from keras.models import Model 
from keras import backend as K 
import scipy as scipy 
import numpy as np 

mat = scipy.io.loadmat('edata.mat') 
emat = mat['edata'] 

input_img = Input(shape=(64,1)) # adapt this if using `channels_first` image data format 

x = Conv1D(32, (9), activation='relu', padding='same')(input_img) 
x = MaxPooling1D((4), padding='same')(x) 
x = Conv1D(16, (9), activation='relu', padding='same')(x) 
x = MaxPooling1D((4), padding='same')(x) 
x = Conv1D(8, (9), activation='relu', padding='same')(x) 
encoded = MaxPooling1D(4, padding='same')(x) 

x = Conv1D(8, (9), activation='relu', padding='same')(encoded) 
x = UpSampling1D((4))(x) 
x = Conv1D(16, (9), activation='relu', padding='same')(x) 
x = UpSampling1D((4))(x) 
x = Conv1D(32, (9), activation='relu')(x) 
x = UpSampling1D((4))(x) 
decoded = Conv1D(1, (9), activation='sigmoid', padding='same')(x) 

autoencoder = Model(input_img, decoded) 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 

x_train = emat[:,0:80000] 
x_train = np.reshape(x_train, (x_train.shape[1], 64, 1)) 
x_test = emat[:,80000:120000] 
x_test = np.reshape(x_test, (x_test.shape[1], 64, 1)) 

from keras.callbacks import TensorBoard 

autoencoder.fit(x_train, x_train, 
       epochs=50, 
       batch_size=128, 
       shuffle=True, 
       validation_data=(x_test, x_test), 
       callbacks=[TensorBoard(log_dir='/tmp/autoencoder')]) 

jedoch erhalte ich diese Störung, wenn ich versuche, die autoencoder.fit zu laufen():

ValueError: Error when checking target: expected conv1d_165 to have shape (None, 32, 1) but got array with shape (80000, 64, 1)

ich weiß, ich mache einige wahrscheinlich sache falsch, wenn ich meine Schichten aufstelle, änderte ich gerade die Größen maxpool und conv2d zu einer 1d Form ... ich habe sehr wenig Erfahrung mit keras oder autoencoders, jedermann sehen, was ich falsch mache?

dank

EDIT: der Fehler, wenn ich es auf einem frischen Konsole ausführen:

ValueError: Error when checking target: expected conv1d_7 to have shape (None, 32, 1) but got array with shape (80000, 64, 1)

hier ist die Ausgabe von autoencoder.summary()

Layer (type)     Output Shape    Param # 
================================================================= 
input_1 (InputLayer)   (None, 64, 1)    0   
_________________________________________________________________ 
conv1d_1 (Conv1D)   (None, 64, 32)   320  
_________________________________________________________________ 
max_pooling1d_1 (MaxPooling1 (None, 16, 32)   0   
_________________________________________________________________ 
conv1d_2 (Conv1D)   (None, 16, 16)   4624  
_________________________________________________________________ 
max_pooling1d_2 (MaxPooling1 (None, 4, 16)    0   
_________________________________________________________________ 
conv1d_3 (Conv1D)   (None, 4, 8)    1160  
_________________________________________________________________ 
max_pooling1d_3 (MaxPooling1 (None, 1, 8)    0   
_________________________________________________________________ 
conv1d_4 (Conv1D)   (None, 1, 8)    584  
_________________________________________________________________ 
up_sampling1d_1 (UpSampling1 (None, 4, 8)    0   
_________________________________________________________________ 
conv1d_5 (Conv1D)   (None, 4, 16)    1168  
_________________________________________________________________ 
up_sampling1d_2 (UpSampling1 (None, 16, 16)   0   
_________________________________________________________________ 
conv1d_6 (Conv1D)   (None, 8, 32)    4640  
_________________________________________________________________ 
up_sampling1d_3 (UpSampling1 (None, 32, 32)   0   
_________________________________________________________________ 
conv1d_7 (Conv1D)   (None, 32, 1)    289  
================================================================= 
Total params: 12,785 
Trainable params: 12,785 
Non-trainable params: 0 
_________________________________________________________________ 
+0

Es ist schwer zu sehen, welche Schicht 'conv1d_165' genau ist (Sie haben das wahrscheinlich schon oft gemacht); Führen Sie ein neues Skript oder Kernel erneut aus, und fügen Sie das Ergebnis von 'autoencoder.summary()' hinzu, bevor Sie – desertnaut

+0

einfügen. Danke!Ich bearbeitete meinen Beitrag mit der frischen Konsole Ausgabe und autoencoder.summary() –

+0

Ich bearbeitete (formatiert) die Ausgabe Ihrer 'Zusammenfassung()' (es war unlesbar) - Bitte bestätigen Sie, dass es das gleiche mit Ihrem ist – desertnaut

Antwort

1

Da die Autoencoder Ausgang der Rekonstruktion sollte Eingabe, eine Mindestanforderung ist, dass ihre Abmessungen übereinstimmen sollten, oder?

an Ihrem autoencoder.summary() Sehen, es einfach ist, zu bestätigen, dass dies nicht der Fall ist: Ihre Eingabe ist die Form (64,1), während der Ausgang des letzten Faltungsschicht conv1d_7(32,1) ist (wir ignorieren die None in der ersten Dimension, da sie beziehen sich auf die Losgröße).

einen Blick auf die example in the Keras blog Lassen Sie haben Sie einen Link zu (es ist ein 2D-Autoencoder, aber die Idee ist die gleiche):

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D 
from keras.models import Model 
from keras import backend as K 

input_img = Input(shape=(28, 28, 1)) # adapt this if using `channels_first` image data format 

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) 
x = MaxPooling2D((2, 2), padding='same')(x) 
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) 
x = MaxPooling2D((2, 2), padding='same')(x) 
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) 
encoded = MaxPooling2D((2, 2), padding='same')(x) 

# at this point the representation is (4, 4, 8) i.e. 128-dimensional 

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded) 
x = UpSampling2D((2, 2))(x) 
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) 
x = UpSampling2D((2, 2))(x) 
x = Conv2D(16, (3, 3), activation='relu')(x) 
x = UpSampling2D((2, 2))(x) 
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x) 

autoencoder = Model(input_img, decoded) 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 

Hier das Ergebnis autoencoder.summary() in diesem Fall:

_________________________________________________________________ 
Layer (type)     Output Shape    Param # 
================================================================= 
input_1 (InputLayer)   (None, 28, 28, 1)   0   
_________________________________________________________________ 
conv2d_1 (Conv2D)   (None, 28, 28, 16)  160  
_________________________________________________________________ 
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)  0   
_________________________________________________________________ 
conv2d_2 (Conv2D)   (None, 14, 14, 8)   1160  
_________________________________________________________________ 
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 8)   0   
_________________________________________________________________ 
conv2d_3 (Conv2D)   (None, 7, 7, 8)   584  
_________________________________________________________________ 
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 8)   0   
_________________________________________________________________ 
conv2d_4 (Conv2D)   (None, 4, 4, 8)   584  
_________________________________________________________________ 
up_sampling2d_1 (UpSampling2 (None, 8, 8, 8)   0   
_________________________________________________________________ 
conv2d_5 (Conv2D)   (None, 8, 8, 8)   584  
_________________________________________________________________ 
up_sampling2d_2 (UpSampling2 (None, 16, 16, 8)   0   
_________________________________________________________________ 
conv2d_6 (Conv2D)   (None, 14, 14, 16)  1168  
_________________________________________________________________ 
up_sampling2d_3 (UpSampling2 (None, 28, 28, 16)  0   
_________________________________________________________________ 
conv2d_7 (Conv2D)   (None, 28, 28, 1)   145  
================================================================= 
Total params: 4,385 
Trainable params: 4,385 
Non-trainable params: 0 

Es ist leicht zu bestätigen, dass hier die Abmessungen des Eingangs und des Ausgangs (letzte Faltungsschicht conv2d_7) tatsächlich beide (28, 28, 1) sind.

So ist die summary() Methode Ihr Freund beim Bau von Autoencodern; Sie sollten mit den Parametern experimentieren, bis Sie sicher sind, dass Sie eine Ausgabe derselben Dimensionalität wie Ihre Eingabe produzieren. Ich schaffte es so mit Ihrem Autoencoder einfach zu tun, indem die size Argument der letzten UpSampling1D Schicht von 4 bis 8 Wechsel:

input_img = Input(shape=(64,1)) 

x = Conv1D(32, (9), activation='relu', padding='same')(input_img) 
x = MaxPooling1D((4), padding='same')(x) 
x = Conv1D(16, (9), activation='relu', padding='same')(x) 
x = MaxPooling1D((4), padding='same')(x) 
x = Conv1D(8, (9), activation='relu', padding='same')(x) 
encoded = MaxPooling1D(4, padding='same')(x) 

x = Conv1D(8, (9), activation='relu', padding='same')(encoded) 
x = UpSampling1D((4))(x) 
x = Conv1D(16, (9), activation='relu', padding='same')(x) 
x = UpSampling1D((4))(x) 
x = Conv1D(32, (9), activation='relu')(x) 
x = UpSampling1D((8))(x)    ## <-- change here (was 4) 
decoded = Conv1D(1, (9), activation='sigmoid', padding='same')(x) 

autoencoder = Model(input_img, decoded) 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 

In diesem Fall wird die autoencoder.summary() wird:

Layer (type)     Output Shape    Param # 
================================================================= 
input_1 (InputLayer)   (None, 64, 1)    0   
_________________________________________________________________ 
conv1d_1 (Conv1D)   (None, 64, 32)   320  
_________________________________________________________________ 
max_pooling1d_1 (MaxPooling1 (None, 16, 32)   0   
_________________________________________________________________ 
conv1d_2 (Conv1D)   (None, 16, 16)   4624  
_________________________________________________________________ 
max_pooling1d_2 (MaxPooling1 (None, 4, 16)    0   
_________________________________________________________________ 
conv1d_3 (Conv1D)   (None, 4, 8)    1160  
_________________________________________________________________ 
max_pooling1d_3 (MaxPooling1 (None, 1, 8)    0   
_________________________________________________________________ 
conv1d_4 (Conv1D)   (None, 1, 8)    584  
_________________________________________________________________ 
up_sampling1d_1 (UpSampling1 (None, 4, 8)    0   
_________________________________________________________________ 
conv1d_5 (Conv1D)   (None, 4, 16)    1168  
_________________________________________________________________ 
up_sampling1d_2 (UpSampling1 (None, 16, 16)   0   
_________________________________________________________________ 
conv1d_6 (Conv1D)   (None, 8, 32)    4640  
_________________________________________________________________ 
up_sampling1d_3 (UpSampling1 (None, 64, 32)   0   
_________________________________________________________________ 
conv1d_7 (Conv1D)   (None, 64, 1)    289  
================================================================= 
Total params: 12,785 
Trainable params: 12,785 
Non-trainable params: 0 

mit der Dimensionalität Ihre Eingabe und Ausgabe stimmte überein, wie es sein sollte ...

+0

danke, es funktioniert jetzt. Das einzige Problem ist, dass es nicht konvergiert haha, bleibt nur bei einem Verlust von -700 oder so stecken. –

+0

@RussellButler könnten Sie verschiedene Umparametrisierungen versuchen – desertnaut

+0

Sie meinen Normalisierung der Eingabedaten? Ich habe alles mit dem Max geteilt und es scheint besser zu funktionieren (dh jetzt konvergiert es zu ~ 0,12 Verlust, aber ich bin mir nicht sicher, ob das nur weil die Eingabewerte viel kleiner sind. aber jede Iteration scheint den Verlust zu verringern Also nehme ich das als gutes Zeichen. –

Verwandte Themen