我有一个简单的keras模型如下,模型摘要正如我所期望的那样并且模型已经完成,但是,我无法弄清楚如何匹配输入和输出维度以避免错误。
x = Input(shape=(784,1,),name='input')
h = LSTM(128,return_sequences=False,name='lstm')(x)
z = Dense(2,name='z_mu')(h)
x_pred = Dense(128, input_dim=latent_dim, activation='relu',name='seq_1')(z)
x_pred = Dense(original_dim, activation='sigmoid',name='seq_2')(x_pred)
model = Model(inputs=[x, eps], outputs=x_pred)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
vae.summary()
n_samples = 1000
imgs = io.loadh('all_digital_digits_4.h5')['arr_0'] # ((10,n_samples,28,28))
x_train = np.reshape(imgs,(10*n_samples,28,28))
x_train = np.reshape(imgs,(-1,original_dim,1,))
x_test = np.reshape(imgs,(-1,original_dim,1,))
model.fit(x_train,
x_train,
shuffle=True,
epochs=epochs,
batch_size=batch_size,
validation_data=(x_test, x_test))
我收到此错误:
我有一个简单的keras模型如下,模型摘要正如我所期望的那样并且模型已经完成,但是,我无法弄清楚如何匹配输入和输出维度以避免错误。
Layer (type) Output Shape Param #
=================================================================
input (InputLayer) (None, 784, 1) 0
_________________________________________________________________
lstm (LSTM) (None, 128) 66560
_________________________________________________________________
z_mu (Dense) (None, 2) 258
_________________________________________________________________
seq_1 (Dense) (None, 128) 384
_________________________________________________________________
seq_2 (Dense) (None, 784) 101136
=================================================================
Total params: 168,338
Trainable params: 168,338
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-b1befb367cba> in <module>()
72 epochs=epochs,
73 batch_size=batch_size,
---> 74 validation_data=(x_test, x_test))
75
76 encoder = Model(x, z_mu)
/Users/asr2031/programs/anaconda/lib/python2.7/site- packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1572 class_weight=class_weight,
1573 check_batch_axis=False,
-> 1574 batch_size=batch_size)
1575 # Prepare validation data.
1576 do_validation = False
/Users/asr2031/programs/anaconda/lib/python2.7/site- packages/keras/engine/training.pyc in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1409 output_shapes,
1410 check_batch_axis=False,
-> 1411 exception_prefix='target')
1412 sample_weights = _standardize_sample_weights(sample_weight,
1413 self._feed_output_names)
/Users/asr2031/programs/anaconda/lib/python2.7/site-packages/keras/engine/training.pyc in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
139 ' to have ' + str(len(shapes[i])) +
140 ' dimensions, but got array with shape ' +
--> 141 str(array.shape))
142 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
143 if not j and not check_batch_axis:
ValueError: Error when checking target: expected seq_2 to have 2 dimensions, but got array with shape (10000, 784, 1)
任何想法在这里发生了什么?
答案 0 :(得分:1)
看起来您想要实现自动编码器,因为您为输入和目标传递了相同的值。由于(batch_size,sequence_length,features)
,输入处的lstm期望3维作为(batch_size,features)
,输出只是两维return_sequences=False
。 lstm只返回序列的最后一个元素。如果您想要输出整个序列,请使用:
x = Input(shape=(784,1,),name='input')
h = LSTM(128,return_sequences=True,name='lstm')(x)
z = TimeDistributed(Dense(2),name='z_mu')(h)
x_pred = TimeDistributed(Dense(128, input_dim=latent_dim, activation='relu'),name='seq_1')(z)
x_pred = TimeDistributed(Dense(original_dim, activation='sigmoid'),name='seq_2')(x_pred)
model = Model(inputs=[x, eps], outputs=x_pred)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
vae.summary()
这应该有效。请注意,由于lstm现在返回序列,因此需要像TimeDistributed图层一样来复制Dense图层。另一种解决方案是使用Lambda层来重塑lstm输出。最终,这取决于你想要达到的目标。如果您提供更多详细信息,我可以评论要使用的结构。