Keras: Concatenate Layer Errors

Created on 10 Apr 2017  路  6Comments  路  Source: keras-team/keras

Keras = 2.0.2
TensorFlow = 1.0.0
Python3
Mac OSX

Error:

Using tensorflow backend here is my network:

 convolutionInput = Input(shape=(1, maxRow, cols), name='convolutional_input')
 x = Conv2D(32, (3, cols), input_shape=(1, maxRow, cols), data_format='channels_first')(convolutionInput)
 x = MaxPooling2D(pool_size=(2, 1))(x)
 x = Dropout(.5)(x)
 convolutionOutput = Flatten()(x)
 additionalInput = Input(shape=(1,), name='additional_input')
 x = Concatenate([convolutionOutput, additionalInput], axis=1)
 x = Dense(64, activation='relu')(x)
 x = Dense(64, activation='relu')(x)
 finalOutput = Dense(2, activation='softmax')(x)
 convoNet = Model(inputs=[convolutionInput, additionalInput], outputs=finalOutput)
 convoNet.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
 convoNet.fit(x={'convolutional_input': trainingSet[0], 'additional_input': trainingSet[1]}, y=trainLabels, epochs=20, batch_size=10)

And I get this error:

  x = Concatenate([convolutionOutput, additionalInput], axis=1)
 TypeError: __init__() got multiple values for argument 'axis'

I also tried not including the axis keyword argument at all and got this error:

 Traceback (most recent call last):
   File "/Users/bl755p/Documents/WRT_NLP.py", line 683, in <module>
     x = Dense(64, activation='relu')(x)
   File "/Users/bl755p/anaconda/envs/ATT_NLP-Keras2/lib/python3.5/sitepackages/keras/engine/topology.py", line 511, in __call__
     self.assert_input_compatibility(inputs)
   File "/Users/bl755p/anaconda/envs/ATT_NLP-Keras2/lib/python3.5/site-packages/keras/engine/topology.py", line 423, in assert_input_compatibility
     ndim = K.ndim(x)
   File "/Users/bl755p/anaconda/envs/ATT_NLP-Keras2/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 437, in ndim
     dims = x.get_shape()._dims
 AttributeError: 'Concatenate' object has no attribute 'get_shape'

Most helpful comment

Concatenate -> concatenate

All 6 comments

Concatenate -> concatenate

I changed it to this and it worked:

 concat = Concatenate(axis=1)
 x = concat([convolutionOutput, additionalInput])

Changing to lowercase gave this error though:

 NameError: name 'concatenate' is not defined

I guess is there a different place I need to import that maybe?

from keras.layers import concatenate

import tensorflow as tf

c1 = tf.constant([[1,2,3], [4,5,6]], dtype=tf.float32)
c2 = tf.constant([[1,2,3], [4,5,6]], dtype=tf.float32)
l1 = tf.keras.layers.Dense(10)(c1)
l2 = tf.keras.layers.Dense(10)(c2)
concat = tf.keras.layers.Concatenate(axis=1)([l1, l2])
out = tf.keras.layers.Dense(10)(concat)

print("-"*30)
print(out)
print(out.shape)

I ran into this too, even though I've used it before. @SpikingNeuron has it right - the key thing is that the merged layers are in the second function call, just like you would for other normal layers. Otherwise you get things like:

'Concatenate' object has no attribute 'shape'

Had me scratching my head for a bit.

The syntax is

from tensorflow.keras.layers import Concatenate
# prev_layer1 = Dense(...)
# prev_layer2 = Dense(...)
merged = Concatenate()([prev_layer1, prev_layer2])
Was this page helpful?
0 / 5 - 0 ratings