我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用model.RNNModel()。
def test_rnn(): np.random.seed(0) num_layers = 50 seq_length = num_layers * 2 batchsize = 2 vocab_size = 4 data = np.random.randint(0, vocab_size, size=(batchsize, seq_length), dtype=np.int32) source, target = make_source_target_pair(data) model = RNNModel(vocab_size, ndim_embedding=100, num_layers=num_layers, ndim_h=3, kernel_size=3, pooling="fo", zoneout=False, wgain=1, densely_connected=True) with chainer.using_config("train", False): np.random.seed(0) model.reset_state() Y = model(source).data model.reset_state() np.random.seed(0) for t in range(source.shape[1]): y = model.forward_one_step(source[:, :t+1]).data target = np.swapaxes(np.reshape(Y, (batchsize, -1, vocab_size)), 1, 2) target = np.reshape(np.swapaxes(target[:, :, t, None], 1, 2), (batchsize, -1)) assert np.sum((y - target) ** 2) == 0 print("t = {} OK".format(t))
def test_rnn(): np.random.seed(0) num_blocks = 10 num_layers_per_block = 5 seq_length = num_layers_per_block * num_blocks * 2 batchsize = 2 vocab_size = 4 data = np.random.randint(0, vocab_size, size=(batchsize, seq_length), dtype=np.int32) source, target = make_source_target_pair(data) model = RNNModel(vocab_size, ndim_embedding=3, num_blocks=num_blocks, num_layers_per_block=num_layers_per_block, ndim_h=3, kernel_size=3, wgain=1) with chainer.using_config("train", False): np.random.seed(0) model.reset_state() Y = model(source).data model.reset_state() np.random.seed(0) for t in xrange(source.shape[1]): y = model.forward_one_step(source[:, :t+1]).data target = np.swapaxes(np.reshape(Y, (batchsize, -1, vocab_size)), 1, 2) target = np.reshape(np.swapaxes(target[:, :, t, None], 1, 2), (batchsize, -1)) assert np.sum((y - target) ** 2) == 0 print("t = {} OK".format(t))