Reputation: 815
I have a simple regression model as below. The layers layer_abc
and layer_efg
both have (None, 5)
as output, so their output have same dimension and can be added. Thus I want to unhide the code #keras.layers.Add()(['layer_abc', 'layer_efg'])
. But whenever I do this, I got an error AttributeError: 'str' object has no attribute 'get_shape'
. If I didn't unhide this line, then the code is fine.
How can I add the two layers without having error? Many thanks!
from __future__ import absolute_import, division, print_function
from scipy import misc
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Activation
import numpy as np
import matplotlib.pyplot as plt
train_images=np.array([[[0],[1],[2]],[[0],[0],[2]],[[1],[1],[1]],[[1],[0],[1]]])
train_labels=np.array([[1],[0],[1],[0]])
model = keras.Sequential([
keras.layers.Flatten(input_shape=(3, 1)),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_abc'),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_efg'),
#keras.layers.Add()(['layer_abc', 'layer_efg']),
keras.layers.Dense(1, activation=tf.nn.softmax),
])
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy','mean_squared_error'])
print(model.summary())
model.fit(train_images, train_labels, epochs=2)
Upvotes: 0
Views: 473
Reputation: 6034
You can use the functional API like this to do the Add, for single output between 0 and 1, use the sigmoid activation for the output:
input = keras.layers.Input((3,1))
x1 = keras.layers.Dense(5, activation=tf.nn.relu, name='layer_abc')(input)
x2 = keras.layers.Dense(5, activation=tf.nn.relu, name='layer_efg')(input)
x = keras.layers.Add()([x1, x2])
x = keras.layers.Flatten()(x)
output = keras.layers.Dense(1, activation=tf.nn.sigmoid)(x)
model = keras.models.Model(input, output)
This might work:
model = keras.Sequential([
keras.layers.Flatten(input_shape=(3, 1)),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_abc'),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_efg')])
model.add(keras.layers.Lambda(lambda x: tf.add(model.layers[1].output, x)))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
Upvotes: 1