Skip to content

Instantly share code, notes, and snippets.

@singhay
Last active July 4, 2018 19:23
Show Gist options
  • Save singhay/5974ad493e9049cf9efa00d9b21bf26f to your computer and use it in GitHub Desktop.
Save singhay/5974ad493e9049cf9efa00d9b21bf26f to your computer and use it in GitHub Desktop.
Timedistribued Flatten after Timedistributed add throws symbolic tensor instance error
def get_model():
input_image_seqs = Input(shape=(None, 32, 32, 1), name='input_image_seqs')
bn1 = BatchNormalization()(input_image_seqs)
ac1 = Activation('elu')(bn1)
conv1 = TimeDistributed(Conv2D(32, (3, 3), strides=(1, 1), activation='elu', padding='same'))(ac1)
bn2 = BatchNormalization()(conv1)
ac2 = Activation('elu')(bn2)
conv2 = TimeDistributed(Conv2D(32, (3, 3), strides=(1, 1), activation='elu', padding='same'))(ac2)
residual_shape = K.int_shape(conv2)
ROW_AXIS = 1
COL_AXIS = 2
CHANNEL_AXIS = 3
shortcut = TimeDistributed(Conv2D(filters=residual_shape[CHANNEL_AXIS],
kernel_size=(1, 1),
strides=(1, 1),
padding="valid",
kernel_initializer="he_normal", kernel_regularizer=l2(0.0001)))(input_image_seqs)
skip1 = TimeDistributed(add([shortcut, conv2]))
flat1 = TimeDistributed(Flatten())(skip1)
conv_dense1 = TimeDistributed(Dense(512, activation='elu'))(flat1)
lstm1 = Bidirectional(LSTM(512, return_sequences=True, activation='elu'), merge_mode='ave')(conv_dense1)
dense1 = Dense(3, activation=Pitanh)(lstm1)
model = Model(inputs=[input_image_seqs], outputs=dense1)
return model
'''
ERROR
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
278 try:
--> 279 K.is_keras_tensor(x)
280 except ValueError:
/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
470 raise ValueError('Unexpectedly found an instance of type `' +
--> 471 str(type(x)) + '`. '
472 'Expected a symbolic tensor instance.')
ValueError: Unexpectedly found an instance of type `<class 'keras.layers.wrappers.TimeDistributed'>`. Expected a symbolic tensor instance.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-85-fe5b9b618138> in <module>()
----> 1 model = get_model()
2 model.compile(optimizer=Nadam(lr=0.002, beta_1=0.9, beta_2=0.999,
3 epsilon=None, schedule_decay=0.004, clipnorm=0.5),
4 loss='mse')
5 model.summary()
<ipython-input-84-1cb8f2a91a9d> in get_model()
19 kernel_initializer="he_normal", kernel_regularizer=l2(0.0001)))(input_image_seqs)
20 skip1 = TimeDistributed(add([shortcut, conv2]))
---> 21 flat1 = TimeDistributed(Flatten())(skip1)
22 conv_dense1 = TimeDistributed(Dense(512, activation='elu'))(flat1)
23 # embed1 = Embedding(input_dim=10, output_dim=100)(flat1)
/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
412 # Raise exceptions in case the input is not compatible
413 # with the input_spec specified in the layer constructor.
--> 414 self.assert_input_compatibility(inputs)
415
416 # Collect input shapes to build layer.
/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
283 'Received type: ' +
284 str(type(x)) + '. Full input: ' +
--> 285 str(inputs) + '. All inputs to the layer '
286 'should be tensors.')
287
ValueError: Layer time_distributed_159 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.wrappers.TimeDistributed'>. Full input: [<keras.layers.wrappers.TimeDistributed object at 0x7f9dfe91bdd8>]. All inputs to the layer should be tensors.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment