简体   繁体   English

Keras-输入层和嵌入层错误

[英]Keras- Input Layer and Embedding layer error

I am trying to make a model for Tv script generation and while running the following model, input layer and embedding layer error is occurring. 我正在尝试为电视脚本生成创建模型,并且在运行以下模型时,发生输入层和嵌入层错误。 I have tried running the model without these two lines and it works fine. 我试过在没有这两行的情况下运行模型,并且效果很好。 Can someone please help me with the error? 有人可以帮我解决这个错误吗?

embedding = 300
lstm_size = 128
vocab_size = len(vocab) #8420
seq_len = 100


model = Sequential()
model.add(Input((None, )))
model.add(Embedding(inp, input_dim = vocab_size, output_dim = embedding, 
input_length = 1000))
model.add(LSTM(lstm_size, return_sequences = True, return_state = True))
model.add(LSTM(lstm_size, return_sequences = True, return_state = True))
model.add(LSTM(lstm_size, return_sequences = True, return_state = True))
model.add(Flatten())
model.add(Dense(vocab_size))

TypeError                                 Traceback (most recent call last)
<ipython-input-66-695a9250515c> in <module>
 19 #model = Model(inp, out)
 20 model = Sequential()
---> 21 model.add(Input((None, )))
 22 model.add(Embedding(inp, input_dim = vocab_size, output_dim = embedding, input_length = 1000))
 23 model.add(LSTM(lstm_size, return_sequences = True, return_state = True))

~\Anaconda3\lib\site-packages\tensorflow\python\training\checkpointable\base.py in _method_wrapper(self, *args, **kwargs)
440     self._setattr_tracking = False  # pylint: disable=protected-access
441     try:
--> 442       method(self, *args, **kwargs)
443     finally:
444       self._setattr_tracking = previous_value  # pylint: disable=protected-access

~\Anaconda3\lib\site- packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
143       raise TypeError('The added layer must be '
144                       'an instance of class Layer. '
--> 145                       'Found: ' + str(layer))
146     self.built = False
147     set_inputs = False

TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_37:0", shape=(?, ?), dtype=float32)


This is coming for the Input layer
and,

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-67-3c663f8df357> in <module>
 20 model = Sequential()
 21 #model.add(Input((None, )))
---> 22 model.add(Embedding(inp, input_dim = vocab_size, output_dim = embedding, input_length = 1000))
 23 model.add(LSTM(lstm_size, return_sequences = True, return_state = True))
 24 model.add(LSTM(lstm_size, return_sequences = True, return_state = True))

TypeError: __init__() got multiple values for argument 'input_dim'

this comes for embedding layer.

Input is not a Layer Object. 输入不是图层对象。 Which is why you get the first error. 这就是为什么您会遇到第一个错误的原因。 You do not need to pass something like that with a call to Sequential(). 您无需通过调用Sequential()传递类似的信息。 Embedding() can be your first layer. Embedding()可以是您的第一层。

And the second error is because you are passing inp to it. 第二个错误是因为您将inp传递给它。 The first value should either be inp or vocab_size but it cannot be both. 第一个值应该不inpvocab_size ,但不能同时。

Basically, 基本上,

embedding = 300
lstm_size = 128
vocab_size = len(vocab) #8420
seq_len = 100


model = Sequential()
model.add(Embedding(vocab_size, embedding, input_length = 1000))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM