设置表示为 Keras 序列模型的 RNN 的初始状态 [英] Setting the initial state of an RNN represented as a Keras sequential model
问题描述
如何设置下面构造的循环神经网络rnn
的初始状态?
How do I set the initial state of the recurrent neural network rnn
constructed below?
from tensorflow.keras.layers import Dense, SimpleRNN
from tensorflow.keras.models import Sequential
rnn = Sequential([SimpleRNN(3), Dense(1)])
我想在使用 model.fit
拟合模型之前指定第一层的初始状态.
I'd like to specify the initial state of the first layer before fitting the model with model.fit
.
推荐答案
根据 tf.keras.layers.RNN 文档,您可以使用参数initial_state
或数字符号指定初始状态> 通过调用函数 reset_states
.
According to the tf.keras.layers.RNN documentation, you can specify the initial states symbolically using the argument initial_state
or numerically by calling the function reset_states
.
符号规范意味着您需要将初始状态作为输入添加到模型中.这是我改编自 的示例:
Symbolic specification means you need to add the initial states as a input to your model. Here is an example I adapted from the Keras tests:
from tensorflow.keras.layers import Dense, SimpleRNN, Input
from tensorflow.keras.models import Model
import numpy as np
import tensorflow as tf
timesteps = 3
embedding_dim = 4
units = 3
inputs = Input((timesteps, embedding_dim))
# initial state as Keras Input
initial_state = Input((units,))
rnn = SimpleRNN(units)
hidden = rnn(inputs, initial_state=initial_state)
output = Dense(1)(hidden)
model = Model([inputs] + [initial_state], output)
model.compile(loss='categorical_crossentropy',
optimizer=tf.compat.v1.train.AdamOptimizer())
一旦您的模型被定义,您就可以执行如下训练:
And once your model is defined, you can perform training as follows:
num_samples = 2
inputs = np.random.random((num_samples, timesteps, embedding_dim))
# random initial state as additional input
some_initial_state = np.random.random((num_samples, units))
targets = np.random.random((num_samples, units))
model.train_on_batch([inputs] + [some_initial_state], targets)
请注意,此方法要求您使用 Functional API.对于序列模型,您需要使用 stateful RNN,指定 batch_input_shape
,并调用 reset_states
方法:
Note that this approach requires you to use the Functional API. For Sequential models, you will need to use a stateful RNN, specify a batch_input_shape
, and call the reset_states
method:
input_shape = (num_samples, timesteps, embedding_dim)
model = Sequential([
SimpleRNN(3, stateful=True, batch_input_shape=input_shape),
Dense(1)])
some_initial_state = np.random.random((num_samples, units))
rnn = model.layers[0]
rnn.reset_states(states=some_initial_state)
这篇关于设置表示为 Keras 序列模型的 RNN 的初始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!