[英]merging 2 CNNs into an autoencoder in Keras
I am trying to merge 2 CNNs and then un-merge them through an autoencoder but I always get the same error message saying "ValueError: total size of new array must be unchanged", not sure what is wrong with my network. 我正在尝试合并2个CNN,然后通过自动编码器取消合并它们,但是我总是收到相同的错误消息,提示“ ValueError:新数组的总大小必须保持不变”,不确定我的网络出了什么问题。
I am replacing the LSTM and CNN by Dense layers and it works. 我将DSTM层替换为LSTM和CNN,它可以工作。
Thanks for helping! 感谢您的帮助!
from keras.layers import Input, Dense, Conv1D, MaxPooling1D, UpSampling1D, Embedding, Dropout, Flatten, Concatenate, LSTM, BatchNormalization, Reshape
from keras.models import Model
from keras import backend as K
from keras.callbacks import TensorBoard
input_address = Input(shape=(38,))
x_address = Reshape((38, 1))(input_address)
x_address = LSTM(125, activation="tanh", return_sequences=True)(x_address)
x_address = Dropout(0.4)(x_address)
x_address = LSTM(125, activation="tanh", return_sequences=True)(x_address)
x_address = Dropout(0.4)(x_address)
x_address = Dense(100, activation = 'relu')(x_address)
x_address = Conv1D(100, 2, activation='relu', padding='same')(x_address)
x_address = MaxPooling1D(2, padding='same')(x_address)
x_address = Dropout(0.5)(x_address)
x_address = Flatten()(x_address)
input_name = Input(shape=(29,))
x_name = Reshape((29, 1))(input_name)
x_name = LSTM(125, activation="tanh", return_sequences=True)(x_name)
x_name = Dropout(0.4)(x_name)
x_name = LSTM(125, activation="tanh", return_sequences=True)(x_name)
x_name = Dropout(0.4)(x_name)
x_name = Dense(100, activation = 'relu')(x_name)
x_name = Conv1D(100, 2, activation='relu', padding='same')(x_name)
x_name = MaxPooling1D(2, padding='same')(x_name)
x_name = Dropout(0.5)(x_name)
x_name = Flatten()(x_name)
merge = Concatenate(name="concat", axis=1)([x_name, x_address])
encoder = Dense(1, activation = 'relu')(merge)
xd_address = Reshape((19, 100))(encoder)
xd_address = UpSampling1D(2)(xd_address)
xd_address = Conv1D(100, 2, activation='relu', padding='same')(xd_address)
xd_address = Dropout(0.4)(xd_address)
xd_address = LSTM(125, activation="tanh", return_sequences=True)(xd_address)
xd_address = Dropout(0.4)(xd_address)
xd_address = LSTM(125, activation="tanh", return_sequences=True)(xd_address)
xd_address = Flatten()(xd_address)
xd_name = Reshape((15, 100))(encoder)
xd_name = UpSampling1D(2)(xd_name)
xd_name = Conv1D(100, 2, activation='relu', padding='same')(xd_name)
xd_name = Dropout(0.4)(xd_name)
xd_name = LSTM(125, activation="tanh", return_sequences=True)(xd_name)
xd_name = Dropout(0.4)(xd_name)
xd_name = LSTM(125, activation="tanh", return_sequences=True)(xd_name)
xd_name = Flatten()(xd_name)
autoencoder = Model(inputs=[input_name, input_address], outputs=[xd_name, xd_address])
autoencoder.compile(optimizer='adam', loss=['mse', 'mse'])
autoencoder.summary()
autoencoder.fit([name_train, address_train], [name_train, address_train]
,epochs=10
,batch_size=120
,shuffle=True
,validation_data=([name_test, address_test], [name_test, address_test])
,callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
Here is the error message I get 这是我收到的错误消息
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-132-19e453d673b6> in <module>()
33
34
---> 35 xd_address = Reshape((19, 100))(encoder)
36 xd_address = UpSampling1D(2)(xd_address)
37 xd_address = Conv1D(100, 2, activation='relu', padding='same')(xd_address)
~\Anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
472 if all([s is not None
473 for s in to_list(input_shape)]):
--> 474 output_shape = self.compute_output_shape(input_shape)
475 else:
476 if isinstance(input_shape, list):
~\Anaconda3\lib\site-packages\keras\layers\core.py in compute_output_shape(self, input_shape)
392 # input shape known? then we can compute the output shape
393 return (input_shape[0],) + self._fix_unknown_dimension(
--> 394 input_shape[1:], self.target_shape)
395
396 def call(self, inputs):
~\Anaconda3\lib\site-packages\keras\layers\core.py in _fix_unknown_dimension(self, input_shape, output_shape)
380 output_shape[unknown] = original // known
381 elif original != known:
--> 382 raise ValueError(msg)
383
384 return tuple(output_shape)
ValueError: total size of new array must be unchanged
You are reshaping into a size that changes the number elements in the original encoder
tensor. 您正在重塑大小,以更改原始
encoder
张量中的数量元素。 You need to: 你需要:
print(encoder)
and that will give you a hint on what the original shape is. print(encoder)
,这将给您关于原始形状的提示。 Reshape
while preserving the number of elements. Reshape
。 Here is a short example, (100,)
cannot be reshaped to (2,45)
because you omitted 10 elements, ie "total size of new array must be unchanged". 这是一个简短的示例,因为您省略了10个元素,所以
(100,)
不能重塑为(2,45)
,即“新数组的总大小必须保持不变”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.