我正在尝试合并三层并将其添加到模型中,但是我从Tensor
得到了tf.keras.layers.concatenate
,而不是一层?该如何解决?
...
ipt = tf.keras.Input(shape=[10, 5])
convs = []
fs= [1, 2, 3]
for f in fs:
conv = tf.keras.layers.Conv1D(activation='tanh', kernel_size=f, filters=200)(ipt)
pool = tf.keras.layers.MaxPooling1D(10 - fsz + 1, padding="same")(conv)
pool = tf.keras.layers.Flatten()(pool)
convs.append(pool)
merge = tf.keras.layers.concatenate(convs, axis=1)
model = tf.keras.models.Sequential()
model.add(ipt)
model.add(merge)
...
TypeError: The added layer must be an instance of class Layer. Found: Tensor("concatenate/Identity:0", shape=(None, 600), dtype=float32)
答案 0 :(得分:1)
我认为您使用的模型不正确。尝试通过以下方式更改代码。
from tensorflow.keras import layers, models
ipt = layers.Input(shape=[10, 5])
convs = []
fsz = 8
fs= [1, 2, 3]
for f in fs:
conv = layers.Conv1D(activation='tanh', kernel_size=f, filters=200)(ipt)
pool = layers.MaxPooling1D(10 - fsz + 1, padding="same")(conv)
pool = layers.Flatten()(pool)
convs.append(pool)
merge = layers.Concatenate(axis=1)(convs)
model = models.Model(inputs=ipt, outputs=merge)
model.summary()