2016-06-21 3 views
4

Ich bekomme einen Fehler in Keras, wo die Dimension der Ausgabe ist anders als die Dimension der Eingabe. Ich verstehe nicht, woher die 20 kommt. Alle meine Dimensionen scheinen 18 zu zeigen. Auch die Output Shape für convolution2d_70 sagt einfach "multiple", also bin ich mir nicht sicher, was das bedeutet. Irgendwelche Ideen?Keras hat die Ausgabe-Layer-Form nicht übereinstimmend mit der Eingabe für CNN

Exception: Fehler bei der Überprüfung Modellziel: erwartete convolution2d_70 Form haben (keine, 1, 36L, 20L) bekam aber Array mit Form (49L, 1L, 36L, 18L)

from keras.layers import Dense, Input, Convolution2D, MaxPooling2D, UpSampling2D 
from keras.models import Model 

from os import listdir 
import os.path 
import numpy as np 
import re 

versions = !pip freeze 
for line in versions: 
    if re.search('Keras', line): 
      print line 


samples = 100 
x = np.ndarray([samples,36,18]) 
i=0 

for i in range(samples): 
    x[i] = np.random.randint(15, size=(36, 18)) 
    i+=1 

#TODO: CREATE A REAL TEST SET 
x_train = x[:49] 
x_test = x[50:] 
print x_train.shape 
print x_test.shape 

#INPUT LAYER 
input_img = Input(shape=(1,x_train.shape[1],x_train.shape[2])) 

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

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

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

#MODEL 
autoencoder = Model(input=input_img, output=decoded) 

#SEPERATE ENCODER MODEL 
encoder = Model(input=input_img, output=encoded) 

# create a placeholder for an encoded (32-dimensional) input 
encoded_input = Input(shape=(8, 4, 4)) 

# retrieve the last layer of the autoencoder model 
decoder_layer1 = autoencoder.layers[-3] 
decoder_layer2 = autoencoder.layers[-2] 
decoder_layer3 = autoencoder.layers[-1] 

print decoder_layer3.get_config() 

# create the decoder model 
decoder = Model(input=encoded_input, output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input)))) 

#COMPILER 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 

autoencoder.summary() 

x_train = x_train.astype('float32')/15. 
x_test = x_test.astype('float32')/15. 
x_test = np.reshape(x_test, (len(x_test), 1, x_test.shape[1], x_test.shape[2])) 
x_train = np.reshape(x_train, (len(x_train), 1, x_train.shape[1], x_train.shape[2])) 

autoencoder.fit(x_train, 
       x_train, 
       nb_epoch=5, 
       batch_size=1, 
       verbose=True, 
       shuffle=True, 
       validation_data=(x_test,x_test)) 

Antwort

1

Beachten Sie, dass Sie in der dritten Faltungsschicht im Decoder border_mode='same' fehlen. Dies führt dazu, dass Ihre Dimensionalität nicht übereinstimmt.

Verwandte Themen