A.Razavi
A.Razavi

Reputation: 509

How to change dimensions of a layer in keras while running the compiled model?

I have a simple keras model as follow, the model summary is as I expect and the model complied, however, I can't figure out how to match the input and output dimensions to avoid the error.

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))

I get this error:

enter image description here

I have a simple keras model as follow, the model summary is as I expect and the model complied, however, I can't figure out how to match the input and output dimensions to avoid the error.

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)

Any ideas what's going on here?

Upvotes: 1

Views: 3602

Answers (1)

BGraf
BGraf

Reputation: 627

Looks like you want to implement an autoencoder since you pass the same values for inputs and targets. The lstm at the input is expecting 3 dimensions as (batch_size,sequence_length,features)and the output is just two dimensions (batch_size,features)because of return_sequences=False. Only the last element of the sequence is returned by the lstm. If you want a output of the whole sequence use:

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()

This should work. Note, that since the lstm is now returning sequences you need something like TimeDistributed layers to copy the Dense layers along time. Another solution would be using Lambda layers to reshape the lstm output. In the end it depends on what you want to achieve. If you give more details I can comment on which structure to use.

Upvotes: 1

Related Questions