提交 b7ea971b 编写于 作者: A Aston Zhang

rnn lint

上级 bf9717bc
......@@ -85,14 +85,14 @@ ctx = gb.try_gpu()
def get_params():
def _one(shape):
return nd.random.normal(scale=0.01, shape=shape, ctx=ctx)
def _three():
return (_one((num_inputs, num_hiddens)),
_one((num_hiddens, num_hiddens)),
nd.zeros(num_hiddens, ctx=ctx))
W_xz, W_hz, b_z = _three() # 更新门参数。
W_xr, W_hr, b_r = _three() # 重置门参数。
nd.zeros(num_hiddens, ctx=ctx))
W_xz, W_hz, b_z = _three() # 更新门参数。
W_xr, W_hr, b_r = _three() # 重置门参数。
W_xh, W_hh, b_h = _three() # 候选隐藏状态参数。
# 输出层参数。
W_hq = _one((num_hiddens, num_outputs))
......@@ -120,7 +120,7 @@ def gru(inputs, state, params):
W_xz, W_hz, b_z, W_xr, W_hr, b_r, W_xh, W_hh, b_h, W_hq, b_q = params
H, = state
outputs = []
for X in inputs:
for X in inputs:
Z = nd.sigmoid(nd.dot(X, W_xz) + nd.dot(H, W_hz) + b_z)
R = nd.sigmoid(nd.dot(X, W_xr) + nd.dot(H, W_hr) + b_r)
H_tilda = nd.tanh(nd.dot(X, W_xh) + R * nd.dot(H, W_hh) + b_h)
......
......@@ -63,8 +63,11 @@ def data_iter_random(corpus_indices, batch_size, num_steps, ctx=None):
epoch_size = num_examples // batch_size
example_indices = list(range(num_examples))
random.shuffle(example_indices)
# 返回从 pos 开始的长为 num_steps 的序列。
_data = lambda pos: corpus_indices[pos: pos + num_steps]
def _data(pos):
return corpus_indices[pos: pos + num_steps]
for i in range(epoch_size):
# 每次读取 batch_size 个随机样本。
i = i * batch_size
......
......@@ -91,15 +91,15 @@ from mxnet.gluon import rnn
num_inputs, num_hiddens, num_outputs = vocab_size, 256, vocab_size
ctx = gb.try_gpu()
def get_params():
def get_params():
def _one(shape):
return nd.random.normal(scale=0.01, shape=shape, ctx=ctx)
def _three():
return (_one((num_inputs, num_hiddens)),
_one((num_hiddens, num_hiddens)),
nd.zeros(num_hiddens, ctx=ctx))
nd.zeros(num_hiddens, ctx=ctx))
W_xi, W_hi, b_i = _three() # 输入门参数。
W_xf, W_hf, b_f = _three() # 遗忘门参数。
W_xo, W_ho, b_o = _three() # 输出门参数。
......@@ -121,7 +121,7 @@ def get_params():
```{.python .input n=3}
def init_lstm_state(batch_size, num_hiddens, ctx):
return (nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx),
return (nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx),
nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx))
```
......@@ -133,7 +133,7 @@ def lstm(inputs, state, params):
W_hq, b_q] = params
(H, C) = state
outputs = []
for X in inputs:
for X in inputs:
I = nd.sigmoid(nd.dot(X, W_xi) + nd.dot(H, W_hi) + b_i)
F = nd.sigmoid(nd.dot(X, W_xf) + nd.dot(H, W_hf) + b_f)
O = nd.sigmoid(nd.dot(X, W_xo) + nd.dot(H, W_ho) + b_o)
......@@ -172,7 +172,7 @@ gb.train_and_predict_rnn(lstm, get_params, init_lstm_state, num_hiddens,
lstm_layer = rnn.LSTM(num_hiddens)
model = gb.RNNModel(lstm_layer, vocab_size)
gb.train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
corpus_indices, idx_to_char, char_to_idx,
corpus_indices, idx_to_char, char_to_idx,
num_epochs, num_steps, lr, clipping_theta,
batch_size, pred_period, pred_len, prefixes)
```
......
......@@ -101,9 +101,9 @@ predict_rnn_gluon('分开', 10, model, vocab_size, ctx, idx_to_char, char_to_idx
```{.python .input n=18}
# 本函数已保存在 gluonbook 包中方便以后使用。
def train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
corpus_indices, idx_to_char, char_to_idx,
num_epochs, num_steps, lr, clipping_theta,
def train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
corpus_indices, idx_to_char, char_to_idx,
num_epochs, num_steps, lr, clipping_theta,
batch_size, pred_period, pred_len, prefixes):
loss = gloss.SoftmaxCrossEntropyLoss()
model.initialize(ctx=ctx, force_reinit=True, init=init.Normal(0.01))
......@@ -134,7 +134,7 @@ def train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
epoch + 1, math.exp(loss_sum / (t + 1)), time.time() - start))
for prefix in prefixes:
print(' -', predict_rnn_gluon(
prefix, pred_len, model, vocab_size,
prefix, pred_len, model, vocab_size,
ctx, idx_to_char, char_to_idx))
```
......@@ -143,9 +143,9 @@ def train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
```{.python .input n=19}
num_epochs, batch_size, lr, clipping_theta = 200, 32, 1e2, 1e-2
pred_period, pred_len, prefixes = 50, 50, ['分开', '不分开']
train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
corpus_indices, idx_to_char, char_to_idx,
num_epochs, num_steps, lr, clipping_theta,
train_and_predict_rnn_gluon(model, num_hiddens, vocab_size, ctx,
corpus_indices, idx_to_char, char_to_idx,
num_epochs, num_steps, lr, clipping_theta,
batch_size, pred_period, pred_len, prefixes)
```
......
......@@ -102,7 +102,7 @@ len(outputs), outputs[0].shape, state_new[0].shape
def predict_rnn(prefix, num_chars, rnn, params, init_rnn_state,
num_hiddens, vocab_size, ctx, idx_to_char, char_to_idx):
state = init_rnn_state(1, num_hiddens, ctx)
output = [char_to_idx[prefix[0]]]
output = [char_to_idx[prefix[0]]]
for t in range(num_chars + len(prefix) - 1):
# 将上一时间步的输出作为当前时间步的输入。
X = to_onehot(nd.array([output[-1]], ctx=ctx), vocab_size)
......@@ -173,7 +173,7 @@ def train_and_predict_rnn(rnn, get_params, init_rnn_state, num_hiddens,
if is_random_iter:
data_iter_fn = gb.data_iter_random
else:
data_iter_fn = gb.data_iter_consecutive
data_iter_fn = gb.data_iter_consecutive
params = get_params()
loss = gloss.SoftmaxCrossEntropyLoss()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册