带有siamese网络错误'NoneType'对象的keras没有属性'_inbound_nodes'

问题描述

我想通过使用孪生网络获得相似度得分。

我正在尝试解决错误,所以我使用了 keras Lambda。

'nonetype' 对象没有属性 '_inbound_nodes'

但问题不会解决

这是我的代码。如何解决这个错误...

def get_siamese_model(input_shape):
left_input = Input(input_shape)
right_input = Input(input_shape)

# Convolutional Neural Network
model = Sequential()
model.add(Conv1D(64,3,activation='relu',input_shape=input_shape))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(200))
          
# Generate the encodings (feature vectors) for the two images
encoded_l = model(left_input)
encoded_r = model(right_input)
          
def init_weights(shape):
          return tf.Variable(tf.random.normal(shape,stddev=0.01))
num_of_filters = 200
# calculate pairwise similarity with similarity_matrix
similarity_matrix = init_weights([num_of_filters,num_of_filters])
          
mat_layer = Lambda(lambda x: tf.matmul(x[0],x[1]))([encoded_l,similarity_matrix])
sim_layer = Lambda(lambda x: tf.reduce_sum(x,axis=1))(mat_layer*encoded_r)
sim1 = Lambda(lambda x: tf.reshape(x,[-1,1]))(sim_layer)
feature = layers.Concatenate(axis=1)([encoded_l,encoded_r,sim1])

# Add a dense layer with a softmax unit to generate the similarity score
prediction = layers.Dense(2,activation='softmax')(feature)

# Connect the inputs with the outputs
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)

# return the model
return siamese_net

model = get_siamese_model((300,128))
model.summary()

这是错误代码

AttributeError                            Traceback (most recent call last)
<ipython-input-168-756c0299db8b> in <module>
----> 1 model = get_siamese_model((300,128))
      2 model.summary()

<ipython-input-166-8e80da616698> in get_siamese_model(input_shape)
     29 
     30     # Connect the inputs with the outputs
---> 31     siamese_net = Model(inputs=[left_input,outputs=prediction)
     32 
     
AttributeError: 'nonetype' object has no attribute '_inbound_nodes'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)