如何在 Keras 中正确连接 Flatten 层和特征向量
How to correctly concatenate a Flatten layer and a feature vector in Keras
我只需要在 Keras 中连接一个展平层和一个特征向量。这是代码:
#custom parameters
n_features = 38
vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))
last_layer = vgg_model.get_layer('pool5').output
x = Flatten(name='flatten')(last_layer)
# feature vector
feature_vector = Input(shape = (n_features,))
conc = concatenate(([x, feature_vector]), axis=1)
layer_intermediate = Dense(128, activation='relu', name='fc6')(conc)
layer_intermediate1 = Dense(32, activation='relu', name='fc7')(layer_intermediate)
out = Dense(5, activation='softmax', name='fc8')(layer_intermediate1)
custom_vgg_model = Model(vgg_model.input, out)
但是我收到这个错误:
---> 20 custom_vgg_model = 模型(vgg_model.input, out)
ValueError:图已断开连接:无法获取层 "input_88" 处张量 Tensor("input_88:0",shape=(?, 38),dtype=float32) 的值。可以毫无问题地访问以下先前的层:['input_87'、'conv1_1'、'conv1_2'、'pool1'、'conv2_1'、'conv2_2'、'pool2', 'conv3_1', 'conv3_2', 'conv3_3', 'pool3', 'conv4_1', 'conv4_2', 'conv4_3', 'pool4', 'conv5_1'、'conv5_2'、'conv5_3'、'pool5'、'flatten']
顺便说一句,展平层的形状是 (None, 25088)
因为你的 feature_vector
也是 Input
。定义模型时,尝试将 feature_vector
添加到输入中。
custom_vgg_model = Model([vgg_model.input,feature_vector], out)
我只需要在 Keras 中连接一个展平层和一个特征向量。这是代码:
#custom parameters
n_features = 38
vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))
last_layer = vgg_model.get_layer('pool5').output
x = Flatten(name='flatten')(last_layer)
# feature vector
feature_vector = Input(shape = (n_features,))
conc = concatenate(([x, feature_vector]), axis=1)
layer_intermediate = Dense(128, activation='relu', name='fc6')(conc)
layer_intermediate1 = Dense(32, activation='relu', name='fc7')(layer_intermediate)
out = Dense(5, activation='softmax', name='fc8')(layer_intermediate1)
custom_vgg_model = Model(vgg_model.input, out)
但是我收到这个错误:
---> 20 custom_vgg_model = 模型(vgg_model.input, out)
ValueError:图已断开连接:无法获取层 "input_88" 处张量 Tensor("input_88:0",shape=(?, 38),dtype=float32) 的值。可以毫无问题地访问以下先前的层:['input_87'、'conv1_1'、'conv1_2'、'pool1'、'conv2_1'、'conv2_2'、'pool2', 'conv3_1', 'conv3_2', 'conv3_3', 'pool3', 'conv4_1', 'conv4_2', 'conv4_3', 'pool4', 'conv5_1'、'conv5_2'、'conv5_3'、'pool5'、'flatten']
顺便说一句,展平层的形状是 (None, 25088)
因为你的 feature_vector
也是 Input
。定义模型时,尝试将 feature_vector
添加到输入中。
custom_vgg_model = Model([vgg_model.input,feature_vector], out)