max_len = 400 embedding_size = 32 vocab_size = 20000 head_num = 8 hopping_num = 1 max_epoch = 20 l2_penalty_coef = 1e-4 x_train, x_test, y_train, y_test = load_imdb(vocab_size) for i, sentence in enumerate(tqdm(x_train)): x_train[i] = [vocab_size] + sentence for i, sentence in enumerate(tqdm(x_test)): x_test[i] = [vocab_size] + sentence x_train = with_padding(x_train, padding_type='post', max_sequence_length=max_len) x_test = with_padding(x_test, padding_type='post', max_sequence_length=max_len) y_train = y_train[:, None] y_test = y_test[:, None] num_train_batch = len(x_train) // batch_size num_dev_batch = len(x_test) // batch_size def load_train_func(index): return x_train[index], y_train[index] def load_dev_func(index): return x_test[index], y_test[index]
from functions import time_distributed from functions import time_distributed_softmax_cross_entropy """cuda setting""" from nnabla.contrib.context import extension_context ctx = extension_context('cuda.cudnn', device_id=0) nn.set_default_context(ctx) """""" # nn.load_parameters('encdec_best.h5') from utils import load_data from utils import with_padding train_source, dev_source, test_source, w2i_source, i2w_source = load_data( './data', 'en') train_source = with_padding(train_source, padding_type='post')[:, ::-1].astype(np.int32) dev_source = with_padding(dev_source, padding_type='post')[:, ::-1].astype(np.int32) test_source = with_padding(test_source, padding_type='post')[:, ::-1].astype(np.int32) train_target, dev_target, test_target, w2i_target, i2w_target = load_data( './data', 'ja') train_target = with_padding(train_target, padding_type='post').astype(np.int32) dev_target = with_padding(dev_target, padding_type='post').astype(np.int32) test_target = with_padding(test_target, padding_type='post').astype(np.int32) vocab_size_source = len(w2i_source) vocab_size_target = len(w2i_target) sentence_length_source = train_source.shape[1] sentence_length_target = train_target.shape[1]
from parametric_functions import simple_rnn from functions import time_distributed from functions import time_distributed_softmax_cross_entropy """cuda setting""" from nnabla.contrib.context import extension_context ctx = extension_context('cuda.cudnn', device_id=1) nn.set_default_context(ctx) """""" from utils import load_data from utils import w2i, i2w, c2i, i2c, word_length from utils import with_padding train_data = load_data('./ptb/train.txt') train_data = with_padding(train_data, padding_type='post') valid_data = load_data('./ptb/valid.txt') valid_data = with_padding(valid_data, padding_type='post') vocab_size = len(w2i) sentence_length = 20 embedding_size = 128 hidden_size = 128 batch_size = 256 max_epoch = 100 x_train = train_data[:, :sentence_length].astype(np.int32) y_train = train_data[:, 1:sentence_length + 1].astype(np.int32) x_valid = valid_data[:, :sentence_length].astype(np.int32)