Python 使用不是符号张量 keras 的输入调用的层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44852153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:31:53  来源:igfitidea点击:

Layer called with an input that isn't a symbolic tensor keras

pythonneural-networkkerasrecurrent-neural-network

提问by tryingtolearn

I'm trying to pass the output of one layer into two different layers and then join them back together. However, I'm being stopped by this error which is telling me that my input isn't a symbolic tensor.

我试图将一层的输出传递到两个不同的层,然后将它们重新连接在一起。但是,我被这个错误阻止了,它告诉我我的输入不是符号张量。

Received type: <class 'keras.layers.recurrent.LSTM'>. All inputs to the layers should be tensors.

However, I believe I'm following the documentation quite closely: https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models

但是,我相信我正在密切关注文档:https: //keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models

and am not entirely sure why this is wrong?

并且不完全确定为什么这是错误的?

net_input = Input(shape=(maxlen, len(chars)), name='net_input')
lstm_out = LSTM(128, input_shape=(maxlen, len(chars)))

book_out = Dense(len(books), activation='softmax', name='book_output')(lstm_out)
char_out = Dense(len(chars-4), activation='softmax', name='char_output')(lstm_out)

x = keras.layers.concatenate([book_out, char_out])
net_output = Dense(len(chars)+len(books), activation='sigmoid', name='net_output')

model = Model(inputs=[net_input], outputs=[net_output])

Thanks

谢谢

回答by Aditya Gune

It looks like you're not actually giving an input to your LSTM layer. You specify the number of recurrent neurons and the shapeof the input, but do not provide an input. Try:

看起来您实际上并未向 LSTM 层提供输入。您指定循环神经元的数量和输入的形状,但不提供输入。尝试:

lstm_out = LSTM(128, input_shape=(maxlen, len(chars)))(net_input)

回答by Alessio Mauro

I know, documentation can be confusing, but Concatenate actually only requires "axis" as parameter, while you passed the layers. The layers need to be passed as argument to the result of it as follow:

我知道,文档可能会令人困惑,但 Concatenate 实际上只需要“轴”作为参数,而您传递图层。这些层需要作为参数传递给它的结果,如下所示:

Line to modify:

x = keras.layers.concatenate([book_out, char_out])

How it should be:

x = keras.layers.Concatenate()([book_out, char_out])

要修改的行:

x = keras.layers.concatenate([book_out, char_out])

应该如何:

x = keras.layers.Concatenate()([book_out, char_out])

回答by Tina

I think you need to add axis=1 to concatenate, Try:

我认为您需要添加 axis=1 来连接,尝试:

x = keras.layers.concatenate([book_out, char_out], axis=1)