mrgloom
mrgloom

Reputation: 21652

How to add constant tensor in Keras?

What I'm trying to do is to add a constant tensor to output of network:

inputs = Input(shape=(config.N_FRAMES_IN_SEQUENCE, config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
cnn = VGG16(include_top=False, weights='imagenet', input_shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
x = TimeDistributed(cnn)(inputs)
x = TimeDistributed(Flatten())(x)
x = LSTM(256)(x)
x = Dense(config.N_LANDMARKS * 2, activation='linear')(x)

mean_landmarks = np.array(config.MEAN_LANDMARKS, np.float32)
mean_landmarks = mean_landmarks.flatten()
mean_landmarks_tf = tf.convert_to_tensor(mean_landmarks)
x = x + mean_landmarks_tf

model = Model(inputs=inputs, outputs=x)
optimizer = Adadelta()
model.compile(optimizer=optimizer, loss='mae')

But I get error:

ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). Found: Tensor("add:0", shape=(?, 136), dtype=float32)

It's trivial in tensorflow, but how to do it in Keras?

Upvotes: 4

Views: 4486

Answers (2)

dennlinger
dennlinger

Reputation: 11488

In addition to your own answer, I would heavily favor a native implementation of addition, as for example Keras provides Keras.layers.Add.

The reason for this is that I am unsure how your own lambda function is pushed down to the lower layers: Essentially, in TensorFlow (or whatever other backend you use), the internal operations make use of the heavily optimized computational graph, whereas custom operations tend to be translated to a heavier-weight (or, in the worst case, bloated) low level execution.

The correct way to do it with Keras.layers.Add would be simply doing

x = keras.layers.Add()([x, add_mean_landmarks])

Upvotes: -1

mrgloom
mrgloom

Reputation: 21652

Seems it can be done with Lamda layer:

from keras.layers import Lambda

def add_mean_landmarks(x):
    mean_landmarks = np.array(config.MEAN_LANDMARKS, np.float32)
    mean_landmarks = mean_landmarks.flatten()
    mean_landmarks_tf = tf.convert_to_tensor(mean_landmarks)
    x = x + mean_landmarks_tf
    return x


x = Lambda(add_mean_landmarks)(x)

Upvotes: 5

Related Questions