如何在Keras中使用平铺功能? [英] How to use tile function in Keras?

查看:223
本文介绍了如何在Keras中使用平铺功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Keras建立一个神经网络,但出现一个错误: AttributeError: 'NoneType' object has no attribute '_inbound_nodes',这是我的示例代码:

I want to build a neural network with Keras,but I got a error: AttributeError: 'NoneType' object has no attribute '_inbound_nodes',and this is my example code:

from keras.layers.merge import concatenate

img = Input(shape=(64,64,3))
text_input = Input(shape=(192,))
text_emb = Reshape(target_shape=(1, 1, 256))(Dense(256, activation='relu')(text_input))
tiled_emb = keras.backend.tile(text_emb, (-1, 64, 64, 1))
img_feat = Conv2D(400,4,padding='same')(img)
con = concatenate([tiled_emb,img_feat])
conv4 = Conv2D(512, 1)(con)

flat = Flatten()(conv4)
validity = Dense(1, activation='sigmoid')(flat)

Model([img, text_input], validity)

推荐答案

发生此错误是因为keras.backend.tile是一个函数而不是一个图层,从而使tiled_emb成为了张量.然后,当尝试构建网络并在仅预期张量的层上遇到张量时,就会生成错误(因此,未定义attr _inbound_nodes).

This error occurs because keras.backend.tile is a function and not a layer, making tiled_emb a tensor. The error is then generated when trying to construct the network and encountering just a tensor where it expects a layer (so the attr _inbound_nodes is not defined).

您可以使用 keras.layers.lambda 将任何功能转换为层.层,例如:

You can turn any function into a layer by using the keras.layers.lambda layer, eg:

tiled_emb = Lambda(keras.backend.tile, arguments={'n':(-1, 64, 64, 1)})(text_emb)

这篇关于如何在Keras中使用平铺功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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