如何解决值错误:图形断开连接:无法获取TensorKerasTensor的值

rkue9o1l  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(105)

我正试图得到一个嵌入为暹罗网络写在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']
z4iuyo4d

z4iuyo4d1#

我试着重现你的代码,因为一些组件,例如euclidean_distance函数,丢失了。下面的代码在我的系统上运行良好:

import tensorflow as tf
import keras.backend as K

def euclidean_distance(x):
    return tf.expand_dims(K.sqrt(K.sum(K.square(x[0] - x[1]), axis=-1)), axis=1)

model_input = tf.keras.layers.Input((40, 1))
x = tf.keras.layers.Conv1D(8, 64, activation="relu", padding='same', kernel_regularizer=tf.keras.regularizers.L1L2(l1=1e-5, l2=1e-4),)(model_input)
x = tf.keras.layers.Conv1D(8, 128, activation="relu", padding='same', kernel_regularizer=tf.keras.regularizers.L1L2(l1=1e-5, l2=1e-4),)(x)
x = tf.keras.layers.AveragePooling1D(pool_size= 2, padding='same')(x)
x = tf.keras.layers.Flatten()(x)

x = tf.keras.layers.Dense(100, activation="relu")(x)
embedding_network = tf.keras.Model(model_input, x)

input_1 = tf.keras.layers.Input((40, 1))
input_2 = tf.keras.layers.Input((40, 1))
 
cnn_1 = embedding_network(input_1)
cnn_2 = embedding_network(input_2)

merge_layer_1 = tf.keras.layers.Lambda(euclidean_distance)([cnn_1, cnn_2])

output_layer = tf.keras.layers.Dense(1, activation="sigmoid")(merge_layer_1)
siamese = tf.keras.Model(inputs=[input_1, input_2], outputs=output_layer)

embedding_network.summary()

输出:

Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 40, 1)]           0         
                                                                 
 conv1d (Conv1D)             (None, 40, 8)             520       
                                                                 
 conv1d_1 (Conv1D)           (None, 40, 8)             8200      
                                                                 
 average_pooling1d (AverageP  (None, 20, 8)            0         
 ooling1D)                                                       
                                                                 
 flatten (Flatten)           (None, 160)               0         
                                                                 
 dense (Dense)               (None, 100)               16100     
                                                                 
=================================================================
Total params: 24,820
Trainable params: 24,820
Non-trainable params: 0
_________________________________________________________________

同样的

siamese.summary()

Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_2 (InputLayer)           [(None, 40, 1)]      0           []                               
                                                                                                  
 input_3 (InputLayer)           [(None, 40, 1)]      0           []                               
                                                                                                  
 model (Functional)             (None, 100)          24820       ['input_2[0][0]',                
                                                                  'input_3[0][0]']                
                                                                                                  
 lambda (Lambda)                (None, 1)            0           ['model[0][0]',                  
                                                                  'model[1][0]']                  
                                                                                                  
 dense_1 (Dense)                (None, 1)            2           ['lambda[0][0]']                 
                                                                                                  
==================================================================================================
Total params: 24,822
Trainable params: 24,822
Non-trainable params: 0
__________________________________________________________________________________________________

然后,对模型进行简单测试:

batch_size=5
inp1=tf.random.uniform((batch_size, 40, 1))
inp2=tf.random.uniform((batch_size, 40, 1))
siamese([inp1, inp2])

输出:

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[0.5550248 ],
       [0.55784535],
       [0.54480696],
       [0.54240334],
       [0.54322207]], dtype=float32)>

相关问题