我正试图得到一个嵌入为暹罗网络写在keras和我一直有下面的问题。有人知道如何解决这个问题吗?
以下是网络:
input = layers.Input((40, 1))
x = layers.Conv1D(8, 64, activation="relu", padding='same', kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),)(input)
x = layers.Conv1D(8, 128, activation="relu", padding='same', kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),)(x)
x = layers.AveragePooling1D(pool_size= 2, padding='same')(x)
x = layers.Flatten()(x)
x = layers.Dense(100, activation="relu")(x)
embedding_network = keras.Model(input, x)
input_1 = layers.Input((40, 1))
input_2 = layers.Input((40, 1))
cnn_1 = embedding_network(input_1)
cnn_2 = embedding_network(input_2)
merge_layer_1 = layers.Lambda(euclidean_distance)([cnn_1, cnn_2])
output_layer = layers.Dense(1, activation="sigmoid")(merge_layer_1)
siamese = keras.Model(inputs=[input_1, input_2], outputs=output_layer)
下面是如何获得嵌入:
get_layer_output = tf.keras.backend.function([siamese.layers[0].input],[siamese.layers[-2].output])
下面是错误:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 40, 1), dtype=tf.float32, name='input_3'), name='input_3', description="created by layer 'input_3'") at layer "model". The following previous layers were accessed without issue: ['model']
1条答案
按热度按时间z4iuyo4d1#
我试着重现你的代码,因为一些组件,例如
euclidean_distance
函数,丢失了。下面的代码在我的系统上运行良好:输出:
同样的
然后,对模型进行简单测试:
输出: