'NoneType'对象没有属性'_inbound_nodes'错误 [英] 'NoneType' object has no attribute '_inbound_nodes' error

查看:79
本文介绍了'NoneType'对象没有属性'_inbound_nodes'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须获取EfficientNet的最后一个conv层的输出,然后计算H = wT * x + b。我的w是[49,49]。之后,我必须在H上应用softmax,然后进行元素逐乘Xì= Hi * Xi。
这是我的代码:

I have to take the output of last conv layer of EfficientNet and then calculate H = wT*x+b. My w is [49,49]. After that I have to apply softmax on H and then do elementwise multiplication Xì = Hi*Xi. This is my code:

common_input = layers.Input(shape=(224, 224, 3))    
x=model0(common_input) #model0 terminate with last conv layer of EfficientNet (7,7,1280)
x = layers.BatchNormalization()(x)

W = tf.Variable(tf.random_normal([49,49], seed=0), name='weight')
b = tf.Variable(tf.random_normal([49], seed=0), name='bias')

x = tf.reshape(x, [-1, 7*7,1280])
H = tf.matmul(W, x,transpose_a=True)
H = tf.nn.softmax(H)
#print(H.shape) (?,49,1280)
#print(x.shape) (?,49,1280)

x=tf.multiply(H, x)

p=layers.Dense(768, activation="relu")(x)
p=layers.Dense(8, activation="softmax", name="fc_out")(p)

model = Model(inputs=common_input, outputs=p)

但是我遇到了这个错误:'NoneType'对象没有属性'_inbound_nodes'

But I got this error: 'NoneType' object has no attribute '_inbound_nodes'

<ipython-input-12-6ce3217f045c> in build_model()
     35     p=layers.Dense(8, activation="softmax", name="fc_out")(p)
     36 
---> 37     model = Model(inputs=common_input, outputs=p)
     38 
     39     return model

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'


推荐答案

我已将操作替换为 Lambda 层。请原谅我的破旧命名。试试这个代码。

I have replaced the operations with a Lambda layer in the following code. Please excuse my shabby naming. Give this code a try.

W = tf.Variable(tf.random_normal([49,49], seed=0), name='weight')
b = tf.Variable(tf.random_normal([49], seed=0), name='bias')

def all_operations(args):
    x = args[0]
    H = args[1]
    x = tf.reshape(x, [-1, 7*7,1280])
    H = tf.matmul(W, x, transpose_a=True)
    H = tf.nn.softmax(H)
    x = tf.multiply(H, x)
    x = tf.reshape(x, [-1, 49*1280])
    return x

common_input = layers.Input(shape=(224, 224, 3))    
x=model0(common_input) #model0 terminate with last conv layer of EfficientNet (7,7,1280)
x = layers.BatchNormalization()(x)

x = Lambda(all_operations)([x, H])

p=layers.Dense(768, activation="relu")(x)
p=layers.Dense(8, activation="softmax", name="fc_out")(p)

model = Model(inputs=common_input, outputs=p)

这篇关于'NoneType'对象没有属性'_inbound_nodes'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆