【问题标题】:'NoneType' object has no attribute '_inbound_nodes' error“NoneType”对象没有属性“_inbound_nodes”错误
【发布时间】:2020-08-26 18:03:44
【问题描述】:

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

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”

<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'

【问题讨论】:

  • 会不会和this有关?
  • 我不这么认为。他试图将三个通道合并为一个,我试图修改conv层的输出矩阵,然后将其放入fc层中。
  • 这个错误出现在哪一行?
  • 如何导入Model
  • 我用这条线修改了我的帖子。放的太长了。最后一个错误点是: ValueError:如果检测到循环。 1392 """ -> 1393 node = layer._inbound_nodes[node_index] 1394 1395 # 防止循环。

标签: python tensorflow keras tf.keras conv-neural-network


【解决方案1】:

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

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)

【讨论】:

  • 谢谢,我会尽快给你反馈
  • 好的,我只修改了 H,因为它没有在 Lambda 层之前定义。只有一件事:密集层的输出是 (None, 49, 768) 而 fc_out 的输出是 (None, 49, 8)。如何修改代码以输出 (None, 768) 和 (None, 8)?
  • @EliaFabbris 在输入密集层之前,您可以将 x 重塑为 [-1, 49*1280]。我已经改变了我的答案以反映这一点。此外,如果您认为解决方案可以接受,请按答案旁边的灰色复选标记。
  • 非常感谢你,你是我的救星!你真是太好了!
  • 我正在尝试拟合我的模型,但出现错误。无效参数:reshape 的输入是一个具有 40960 个值的张量,但请求的形状需要 62720 的倍数。[[{{node lambda_3/Reshape}}]] 我把我的代码作为答案放在下面
猜你喜欢
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2019-09-23
  • 2019-04-25
相关资源
最近更新 更多