在自定义RNN单元中设置初始状态

时间:2019-10-26 12:46:06

标签: tensorflow keras tensorflow2.0

我正在处理自定义RNN单元。自定义部分是,在隐藏状态(shape=(units))旁边,我还携带了处于单元状态的矩阵(shape=(units, units))。 它工作正常,但现在我有兴趣将矩阵初始化为恒等矩阵。 我尝试了为调用方法提供initial_state的另一种方法,但每次似乎都无法提供它,并且出现类型错误

ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=ListWrapper([InputSpec(shape=(50,), ndim=1), InputSpec(shape=(50, 50), ndim=2)]); however `cell.state_size` is [TensorShape([50]), TensorShape([50, 50])]

被抛出。

这是我设置单元状态的方法:

self.state_size = NoDependency([TensorShape([self.units]),
                                        TensorShape([self.units, self.units])])

这是我在致电时尝试初始化它们的方式:

x = rnn_layer(x, initial_state = [tf.zeros(50), tf.eye(50)]).

0 个答案:

没有答案