def __init__(self, n_units, seq_len):
        self.seq_len = seq_len

        self.input_vocab, self.label_vocab = self.load_data()
        self.input_ivocab = {i: c for c, i in self.input_vocab.items()}
        self.label_ivocab = {i: c for c, i in self.label_vocab.items()}

        lm = net.RNNLM(len(self.input_vocab),
                       len(self.label_vocab),
                       n_units,
                       train=False)
        self.model = L.Classifier(lm)
        serializers.load_npz(self.TRAINED_MODEL_FILE, self.model)
Ejemplo n.º 2
0
np.random.seed(args.seed)

xp = cuda.cupy if args.gpu >= 0 else np

# load vocabulary
with open(args.vocabulary, 'rb') as f:
    vocab = pickle.load(f)
ivocab = {}
for c, i in vocab.items():
    ivocab[i] = c

# should be same as n_units , described in train_ptb.py
n_units = args.unit

lm = net.RNNLM(len(vocab), n_units, train=False)
model = L.Classifier(lm)

serializers.load_npz(args.model, model)

if args.gpu >= 0:
    cuda.get_device(args.gpu).use()
    model.to_gpu()

model.predictor.reset_state()

primetext = args.primetext
if isinstance(primetext, six.binary_type):
    primetext = primetext.decode('utf-8')

if primetext in vocab:
Ejemplo n.º 3
0
    words = open(filename).read().replace('\n', '<eos>').strip().split()
    dataset = np.ndarray((len(words),), dtype=np.int32)
    for i, word in enumerate(words):
        if word not in vocab:
            vocab[word] = len(vocab)
        dataset[i] = vocab[word]
    rdict = dict((v,k) for k,v in vocab.iteritems())
    return dataset

train_data = load_data('ptb.train.txt')
valid_data = load_data('ptb.valid.txt')
test_data = load_data('ptb.test.txt')
print('#vocab =', len(vocab))

# Prepare RNNLM model, defined in net.py
lm = net.RNNLM(len(vocab), n_units)
model = L.Classifier(lm)
model.compute_accuracy = True  # we only want the perplexity
for param in model.params():
    data = param.data
    data[:] = np.random.uniform(-0.1, 0.1, data.shape)
if args.gpu >= 0:
    cuda.get_device(args.gpu).use()
    model.to_gpu()

# Setup optimizer
optimizer = optimizers.SGD(lr=1.)
optimizer.setup(model)
optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))

# Init/Resume
Ejemplo n.º 4
0
    global vocab2id, id2vocab, n_vocab
    words = open(filename).read().replace('\n', '<eos>').strip().split()
    for i, word in enumerate(words):
        if word not in vocab2id:
            vocab2id[word] = len(vocab2id)
            id2vocab[vocab2id[word]] = word

    dataset = np.ndarray((len(words),), dtype=np.int32)
load_data('ptb.train.txt')
load_data('ptb.valid.txt')
load_data('ptb.test.txt')

# train_ptb.pyと同じ設定にする
n_units = 650

lm = net.RNNLM(len(vocab2id), n_units, False)
model = L.Classifier(lm)

# モデルデータの読み込み
serializers.load_hdf5(args.model, model)

# 文の適当な生成
for i in range(0,1):
    print(i+1, end=": ")
    # モデルの状態をいったんリセット
    model.predictor.reset_state()
    word = "<eos>"
    while True:
        # RNNLMへの入力を準備
        x = chainer.Variable(np.array([vocab2id[word]],dtype=np.int32))
        # RNNLMの出力のsoftmaxを取得
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', '-m', type=str, required=True,
                        help='model data, saved by train.py')
    parser.add_argument('--file', '-f', type=str, required=True,
                        help='input text file, used for reaction analysis')
    parser.add_argument('--label', type=str, default=None,
                        help='label file for calculating accuracy')
    parser.add_argument('--input_vocab', '-i', type=str, default='data/input_vocab.bin',
                        help='input text vocaburaly dictionary')
    parser.add_argument('--label_vocab', '-l', type=str, default='data/label_vocab.bin',
                        help='input label vocaburaly dictionary')
    parser.add_argument('--seqlen', type=int, required=True,
                        help='sequence length')
    parser.add_argument('-n', type=int, default=3,
                        help='number of candidates')
    parser.add_argument('--fraction', type=float, default=0.0,
                        help='split ratio of dataset (0 means all data goes to test)')
    parser.add_argument('--unit', '-u', type=int, default=650,
                        help='number of units')
    parser.add_argument('--gpu', type=int, default=-1,
                        help='GPU ID (negative value indicates CPU)')

    args = parser.parse_args()
    xp = cuda.cupy if args.gpu >= 0 else np

    # load dataset and vocabulary
    seq_len = args.seqlen

    # For testing with labels
    # ds = dataset.Dataset(args.file, label=args.label, input_vocab=args.input_vocab, label_vocab=args.label_vocab, seq_len=seq_len)

    ds = dataset.Dataset(args.file, label=args.label, input_vocab=args.input_vocab, label_vocab=args.label_vocab, seq_len=seq_len, fraction=args.fraction)
    _, test = ds.get_inputs_and_labels()

    input_vocab, label_vocab = ds.get_vocab()

    input_ivocab = {i: c for c, i in input_vocab.items()}
    label_ivocab = {i: c for c, i in label_vocab.items()}

    # should be same as n_units, described in train.py
    n_units = args.unit

    lm = net.RNNLM(len(input_vocab), len(label_vocab), n_units, train=False)
    model = L.Classifier(lm)

    serializers.load_npz(args.model, model)

    if args.gpu >= 0:
        cuda.get_device(args.gpu).use()
        model.to_gpu()

    n_top = args.n
    n_match = 0
    n_total = 0
    has_label = (len(test[0]) == len(test[1]))
    sys.stdout.write('\n')

    for i, data in enumerate(test[0]):
        print('[input {0}/{1}]'.format(i + 1, len(test[0])), ' '.join(iconv(data, input_ivocab)))

        model.predictor.reset_state()
        for j in six.moves.range(seq_len):
            word = chainer.Variable(xp.array([data[j]]), volatile='on')
            pred = F.softmax(model.predictor(word))
            if j == seq_len - 1:
                if args.gpu >= 0:
                    pred_data = cuda.to_cpu(pred.data)
                else:
                    pred_data = pred.data
                indice = pred_data[0].argsort()[-n_top:][::-1]
                probs = pred_data[0][indice]

                result = [(label_ivocab[idx], prob) for (idx, prob) in zip(indice, probs)]
                if has_label:
                    y = test[1][i]
                    print('[suggested reactions] %s' % result)
                    n_total += 1
                    if indice[0] == y:
                        print(label_ivocab[indice[0]], '(prediction)', '==', label_ivocab[y], '(actual)', '? => MATCH')
                        n_match += 1
                    else:
                        print(label_ivocab[indice[0]], '(prediction)', '==', label_ivocab[y], '(actual)', '? => NOT MATCH')
                else:
                    print('[suggested reactions] %s' % result)
        if has_label:
            print('cumulative accuracy=%f' % (n_match / n_total))
        sys.stdout.write('\n')
Ejemplo n.º 6
0
print('#vocab size =', len(input_vocab), len(label_vocab))

if args.test:
    print('TEST mode!')
    train = (train[0][0:100], train[1][0:100])
    test = (test[0][0:100], test[1][0:100:])
    print(train[0].shape)

with open(os.path.join(os.path.dirname(args.input), 'input_vocab.bin'), 'wb') as f:
    pickle.dump(input_vocab, f)
with open(os.path.join(os.path.dirname(args.input), 'label_vocab.bin'), 'wb') as f:
    pickle.dump(label_vocab, f)
print('save vocabulary dictionary')

# Prepare RNNLM model, defined in net.py
lm = net.RNNLM(len(input_vocab), len(label_vocab), n_units)
model = L.Classifier(lm)
if args.gpu >= 0:
    cuda.get_device(args.gpu).use()
    model.to_gpu()

# Setup optimizer
#optimizer = optimizers.SGD(lr=1.)
optimizer = optimizers.Adam()
optimizer.setup(model)
optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))

# Init/Resume
if args.initmodel:
    print('Load model from', args.initmodel)
    serializers.load_npz(args.initmodel, model)