1
votes

I have met an error message when I tried to concatenate outcomes from two layers.

def cnn_model_fn(learning_rate):
    """Model function for CNN."""
    model1=Sequential()

      # Convolutional Layer #1
    model1.add(tf.keras.layers.Conv2D(
          filters=20,
          kernel_size=[10, 1],
          kernel_initializer='he_uniform',
          bias_initializer=keras.initializers.Constant(value=0),
          padding="same",
          activation=tf.nn.relu, input_shape=(410,1,3)))
    model1.add(Flatten())

    model2=Sequential()

    model2.add(tf.keras.layers.Conv2D(
          filters=20,
          kernel_size=[10, 1],
          kernel_initializer='he_uniform',
          bias_initializer=keras.initializers.Constant(value=0),
          padding="same",
          activation=tf.nn.relu, input_shape=(410,1,3)))
    model2.add(Flatten())

    model4=Sequential()
    model4.add(keras.layers.Concatenate(axis=-1)([model1, model2]))

    optimizer = tf.train.AdamOptimizer(learning_rate)
    model4.compile(loss='mean_squared_error',
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])

    return model4

model4=cnn_model_fn(0.1) 
model4.summary()

"/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape) 377 # Used purely for shape validation. 378 if not isinstance(input_shape, list) or len(input_shape) < 2: --> 379 raise ValueError('A Concatenate layer should be called ' 380 'on a list of at least 2 inputs') 381 if all([shape is None for shape in input_shape]):

ValueError: A Concatenate layer should be called on a list of at least 2 inputs"

1
change this line model4.add(keras.layers.Concatenate(axis=-1)([model1, model2])) to model4.add(keras.layers.concatenate(axis=-1)([model1.output, model2.output]))mlneural03
Thanks. It raised another error: "TypeError: The added layer must be an instance of class Layer. Found: Tensor("concatenate_20/concat:0", shape=(?, 16400), dtype=float32)"derec

1 Answers

2
votes

You are trying to concatenate 2 models but what you want is to concatenate 2 layers. Try the following code.

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Flatten, Input

def cnn_model_fn(learning_rate):
    """Model function for CNN."""
    input_layer=Input(shape=(410,1,3))

    x1 = (tf.keras.layers.Conv2D(
          filters=20,
          kernel_size=[10, 1],
          kernel_initializer='he_uniform',
          bias_initializer=keras.initializers.Constant(value=0),
          padding="same",
          activation=tf.nn.relu ))(input_layer)
    x1 = Flatten()(x1)

    x2 = (tf.keras.layers.Conv2D(
          filters=20,
          kernel_size=[10, 1],
          kernel_initializer='he_uniform',
          bias_initializer=keras.initializers.Constant(value=0),
          padding="same",
          activation=tf.nn.relu))(input_layer)
    x2 = Flatten()(x2)

    x = (keras.layers.Concatenate(axis=-1)([x1,x2]))

    model = Model(input_layer, x)
    optimizer = tf.train.AdamOptimizer(learning_rate)
    model.compile(loss='mean_squared_error',
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])

    return model