gpt4 book ai didi

python - 连接两层

转载 作者:行者123 更新时间:2023-12-01 00:37:01 25 4
gpt4 key购买 nike

当我尝试连接两层的结果时,我遇到了错误消息。

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"

最佳答案

您正在尝试连接 2 个模型,但您想要的是连接 2 个层。尝试以下代码。

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

关于python - 连接两层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57665342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com