Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras:带卷积层的自动编码器_Python_Tensorflow_Keras_Conv Neural Network_Autoencoder - Fatal编程技术网

Python Keras:带卷积层的自动编码器

Python Keras:带卷积层的自动编码器,python,tensorflow,keras,conv-neural-network,autoencoder,Python,Tensorflow,Keras,Conv Neural Network,Autoencoder,我正在尝试在MNIST数据库上执行此处提出的CAE,但有一个大小为2的瓶颈。 当我做模型总结时,我在卷积层中得到了一个错误,形状不匹配 Model: "model_11" _________________________________________________________________ Layer (type) Output Shape Param # ======================

我正在尝试在MNIST数据库上执行此处提出的CAE,但有一个大小为2的瓶颈。 当我做模型总结时,我在卷积层中得到了一个错误,形状不匹配

Model: "model_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_20 (InputLayer)        (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_58 (Conv2D)           (None, 14, 14, 32)        6304      
_________________________________________________________________
conv2d_59 (Conv2D)           (None, 7, 7, 64)          100416    
_________________________________________________________________
conv2d_60 (Conv2D)           (None, 4, 4, 128)         73856     
_________________________________________________________________
flatten_15 (Flatten)         (None, 2048)              0         
_________________________________________________________________
dense_38 (Dense)             (None, 1152)              2360448   
_________________________________________________________________
dense_39 (Dense)             (None, 2)                 2306      
_________________________________________________________________
dense_40 (Dense)             (None, 1152)              3456      
_________________________________________________________________
reshape_16 (Reshape)         (None, 3, 3, 128)         0         
_________________________________________________________________
conv2d_transpose_31 (Conv2DT (None, 6, 6, 64)          401472    
_________________________________________________________________
conv2d_transpose_32 (Conv2DT (None, 12, 12, 32)        401440    
_________________________________________________________________
conv2d_transpose_33 (Conv2DT (None, 24, 24, 1)         25089     
=================================================================
Total params: 3,374,787
Trainable params: 3,374,787
Non-trainable params: 0
_________________________________________________________________
这是完整的代码

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_images = x_train.reshape(x_train.shape[0], 28, 28)
input_img = Input(shape=(28, 28, 1))
encoded = Convolution2D(32, 14, 14, activation = "relu", border_mode="same",subsample = (2,2))(input_img)
encoded = Convolution2D(64, 7, 7, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Convolution2D(128, 3, 3, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Flatten()(encoded)
encoded = Dense(1152)(encoded)

encoded = Dense(2)(encoded)

decoded = Dense(1152)(encoded)
decoded = Reshape((3,3,128))(decoded)
decoded = Deconvolution2D(64, 7, 7, activation = "relu",border_mode="same", subsample = (2,2))(decoded)
decoded = Deconvolution2D(32, 14, 14, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
decoded = Deconvolution2D(1, 28, 28, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
autoencoder = Model(input=input_img, output=decoded)` 


这似乎是填充的问题(不确定,但快速搜索后) 那么,把下面两行加起来怎么样

decoded = Flatten()(decoded)
decoded = Dense(3136)(decoded)
decoded = Reshape((7,7,64))(decoded)
最终代码如下所示

encoded = Convolution2D(32, 14, 14, activation = 
"relu", border_mode="same",subsample = (2,2))(input_img)
encoded = Convolution2D(64, 7, 7, activation = "relu", border_mode="same",subsample = (2,2))(encoded)
encoded = Convolution2D(128, 3, 3, activation = "relu", border_mode="valid",subsample = (2,2))(encoded)
encoded = Flatten()(encoded)
encoded = Dense(1152)(encoded)

encoded = Dense(2)(encoded)

decoded = Dense(1152)(encoded)
decoded = Reshape((3,3,128))(decoded)
decoded = Flatten()(decoded)
decoded = Dense(3136)(decoded)
decoded = Reshape((7,7,64))(decoded)
# decoded = Deconvolution2D(64, 7, 7, activation = "relu",border_mode="same", subsample = (2,2))(decoded)
decoded = Deconvolution2D(32, 14, 14, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
decoded = Deconvolution2D(1, 28, 28, activation = "relu",border_mode="same",subsample = (2,2))(decoded)
autoencoder = Model(input=input_img, output=decoded)