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 =
Solution 1:
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)
Solution 2:
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])
Post a Comment for "How To Add Constant Tensor In Keras?"