Пример #1
0
import pandas as pd

from ivory.common.context import np

np.context = "gpu"

from ivory.common.dataset import Seq2seqDataset, Dataset  # isort:skip
from ivory.core.optimizer import Adam  # isort:skip
from ivory.utils.repository import repo_directory  # isort:skip

sys.path.append(repo_directory("scratch2"))
from dataset import sequence  # isort:skip

(x_train, t_train), (x_test, t_test) = sequence.load_data("addition.txt")
x_train = np.asarray(x_train)
t_train = np.asarray(t_train)
is_reversed = True
if is_reversed:
    x_train = x_train[:, ::-1]
    x_test = x_test[:, ::-1]
data = Seq2seqDataset([x_train, t_train])
data_val = Dataset([x_test, t_test])

for x in data[0]:
    print(x.shape, type(x), x.dtype)

char_to_id, id_to_char = sequence.get_vocab()

# モデルの形状を設定します。
vocab_size = len(char_to_id)
Пример #2
0
 def __post_init__(self):
     self.data = [np.asarray(x) for x in self.data]
     self.length = self.data[0].shape[0]
affine = model.layers[-2]
affine.W.share_variable(em.W, transpose=True)  # type:ignore
model.build()

# 学習済みの重みを読み出します。
import os  # isort:skip
import pickle  # isort:skip
import ivory  # isort:skip

directory = os.path.dirname(ivory.__file__)
directory = os.path.join(directory, "../docs/script")
with open(os.path.join(directory, 'better_rnnlm.pkl'), 'rb') as f:
    weights = pickle.load(f)

for v, weight in zip(model.weight_variables, weights):
    v.data = np.asarray(weight)

# start文字とskip文字を設定します。
start_word = 'you'
start_id = word_to_id[start_word]
skip_words = ['N', '<unk>', '$']
skip_ids = [word_to_id[w] for w in skip_words]


# Softmax関数を定義します。
def softmax(x):
    y = np.exp(x - x.max())
    return y / y.sum()


# 文章ジェネレータを定義します。