Keras GRUCell 缺少 1 个必需的位置参数:'states' [英] Keras GRUCell missing 1 required positional argument: 'states'

查看:61
本文介绍了Keras GRUCell 缺少 1 个必需的位置参数:'states'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Keras构建3层RNN.部分代码在这里:

I try to build a 3-layer RNN with Keras. Part of the code is here:

    model = Sequential()
    model.add(Embedding(input_dim = 91, output_dim = 128, input_length =max_length))
    model.add(GRUCell(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))
    model.add(GRUCell(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))
    model.add(GRUCell(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))
    model.add(TimeDistributed(Dense(target.shape[2])))

然后我遇到此错误:

call() missing 1 required positional argument: 'states'

错误详细信息如下:

~/anaconda3/envs/hw3/lib/python3.5/site-packages/keras/models.py in add(self, layer)
487                           output_shapes=[self.outputs[0]._keras_shape])
488         else:
--> 489             output_tensor = layer(self.outputs[0])
490             if isinstance(output_tensor, list):
491                 raise TypeError('All layers in a Sequential model '

 ~/anaconda3/envs/hw3/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
601 
602             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 603             output = self.call(inputs, **kwargs)
604             output_mask = self.compute_mask(inputs, previous_mask)
605 

推荐答案

  1. 请勿在Keras中直接使用Cell类(即 GRUCell LSTMCell ).它们是计算单元,由相应的层包裹.而是使用Layer类(即 GRU LSTM ):

  1. Don't use Cell classes (i.e. GRUCell or LSTMCell) in Keras directly. They are computation cells which are wrapped by the corresponding layers. Instead use the Layer classes (i.e. GRU or LSTM):

model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))
model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))
model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))

LSTM GRU 使用它们相应的单元在所有时间步长上执行计算.阅读此 SO答案以了解有关它们的区别的更多信息.

The LSTM and GRU use their corresponding cells to perform computations over the all timesteps. Read this SO answer to learn more about their difference.

将多个RNN层彼此堆叠时,需要将其 return_sequences 参数设置为 True ,以产生每个时间步的输出,然后由下一个RNN层使用.请注意,您可能会或可能不会在最后一个RNN层上执行此操作(这取决于您的体系结构和您要解决的问题):

When you are stacking multiple RNN layers on top of each other you need to set their return_sequences argument to True in order to produce the output of each timestep, which in turn is used by the next RNN layer. Note that you may or may not do this on the last RNN layer (it depends on your architecture and the problem you are trying to solve):

model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias, return_sequences=True))
model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias, return_sequences=True))
model.add(GRU(units = self.neurons, dropout = self.dropval,  bias_initializer = bias))

这篇关于Keras GRUCell 缺少 1 个必需的位置参数:'states'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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