<jupyter_start><jupyter_text>循环神经网络的简洁实现<jupyter_code>import sys
sys.path.append('..')
import mindspore
import numpy as np
import mindspore.nn as nn
import mindspore.ops as ops
import mindspore.numpy as mnp
from d2l import mindspore as d2l

batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)<jupyter_output><empty_output><jupyter_text>定义模型<jupyter_code>num_hiddens = 256
rnn_layer = nn.RNN(len(vocab), num_hiddens)<jupyter_output><empty_output><jupyter_text>使用张量来初始化隐状态<jupyter_code>state = mnp.zeros((1, batch_size, num_hiddens))
state.shape<jupyter_output><empty_output><jupyter_text>通过一个隐状态和一个输入，我们就可以用更新后的隐状态计算输出<jupyter_code>X = mindspore.Tensor(np.random.rand(num_steps, batch_size, len(vocab)), mindspore.float32)
Y, state_new = rnn_layer(X, state)
Y.shape, state_new.shape<jupyter_output><empty_output><jupyter_text>我们为一个完整的循环神经网络模型定义了一个`RNNModel`类<jupyter_code>from mindspore import Tensor

class RNNModel(nn.Cell):
    """循环神经网络模型。"""
    def __init__(self, rnn_layer, vocab_size, **kwargs):
        super(RNNModel, self).__init__(**kwargs)
        self.rnn = rnn_layer
        self.vocab_size = vocab_size
        self.num_hiddens = self.rnn.hidden_size
        if not self.rnn.bidirectional:
            self.num_directions = 1
            self.linear = nn.Dense(self.num_hiddens, self.vocab_size)
        else:
            self.num_directions = 2
            self.linear = nn.Dense(self.num_hiddens * 2, self.vocab_size)
        self.on_value = Tensor(1.0, mindspore.float32)
        self.off_value = Tensor(0.0, mindspore.float32)
        
    def construct(self, inputs, state):
        X = ops.OneHot()(inputs.T, self.vocab_size, self.on_value, self.off_value)
        Y, state = self.rnn(X, state)
        output = self.linear(Y.reshape((-1, Y.shape[-1])))
        return output, state

    def begin_state(self, batch_size=1):
        if not isinstance(self.rnn, nn.LSTM):
            return  mnp.zeros((self.num_directions * self.rnn.num_layers,
                                 batch_size, self.num_hiddens))
        else:
            return (mnp.zeros((
                        self.num_directions * self.rnn.num_layers,
                        batch_size, self.num_hiddens)),
                    mnp.zeros((
                        self.num_directions * self.rnn.num_layers,
                        batch_size, self.num_hiddens)))<jupyter_output><empty_output><jupyter_text>基于一个具有随机权重的模型进行预测<jupyter_code>net = RNNModel(rnn_layer, vocab_size=len(vocab), num_hiddens)
d2l.predict_ch8('time traveller', 10, net, vocab)<jupyter_output><empty_output><jupyter_text>使用高级API训练模型<jupyter_code>num_epochs, lr = 500, 1
d2l.train_ch8(net, train_iter, vocab, lr, num_epochs)<jupyter_output>困惑度 1.7, 36966.8 词元/秒
time traveller but now you begitand his fecundityyou must follow
traveller furee yhuref st befree three aid the cediral man
{'fraction_non_alphanumeric': 0.1081174438687392, 'fraction_numerical': 0.015544041450777202, 'mean_word_length': 3.5463108320251178, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '257209', 'license': 'MIT', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1187, 'n_words': 175, 'path': 'chapter_08_recurrent-neural-networks/4_rnn-concise.ipynb', 'repo_name': 'lvyufeng/d2l-mindspore', 'size': '2895'}