コード例 #1
0
ファイル: test_model.py プロジェクト: Shmuma/pytorch_tests
    def test_train(self):
        """
        Perform train on very simple dataset, consisting only with single example
        """
        data = ['aa']
        encoder = input.InputEncoder(data)
        net = model.Model(len(encoder), hidden_size=10)
        objective = nn.CrossEntropyLoss()
        optimizer = optim.SGD(net.parameters(), lr=0.1)
        max_loss = min_loss = None

        for _ in range(100):
            net.zero_grad()
            packed_seq, true_indices = input.batch_to_train(data, encoder)
            out, _ = net(packed_seq)
            v_loss = objective(out, true_indices)
            v_loss.backward()
            optimizer.step()
            l = v_loss.data[0]
            if max_loss is None:
                max_loss = min_loss = l
            else:
                max_loss = max(max_loss, l)
                min_loss = min(min_loss, l)

        loss_drop = max_loss / min_loss
        self.assertGreater(loss_drop, 10.0)

        # try to predict letters
        out = model.inference_one(encoder.START_TOKEN, encoder, net)
        self.assertEquals(out, 'a')
        out = model.inference_one('a', encoder, net)
        self.assertEquals(out, 'a')
        out = model.inference_one('aa', encoder, net)
        self.assertEquals(out, encoder.END_TOKEN)
コード例 #2
0
    def test_simple(self):
        enc = input.InputEncoder(['abc'])
        self.assertEqual(5, len(enc))  # 3 + start + end

        d = enc.encode("aaa")
        self.assertEqual(d.shape, (5, len(enc)))
        self.assertEqual(d[0][0], 1.0)
        self.assertEqual(d[1][2], 1.0)

        d = enc.encode("a", width=4)
        self.assertEqual(d.shape, (6, len(enc)))
コード例 #3
0
ファイル: test_model.py プロジェクト: Shmuma/pytorch_tests
    def test_simple(self):
        encoder = input.InputEncoder(['abcde'])
        items = ["abc", "ab"]
        lens = list(map(len, items))

        dat = np.array([
            encoder.encode(s, width=max(lens))
            for s in items
        ])

        v_dat = Variable(torch.from_numpy(dat))
        seq = rnn_utils.pack_padded_sequence(v_dat, lens, batch_first=True)

        m = model.Model(len(encoder), 16)
        res = m.forward(seq)
コード例 #4
0
ファイル: test_model.py プロジェクト: Shmuma/pytorch_tests
    def test_generate_batch_2(self):
        data = ['aa', 'b']
        encoder = input.InputEncoder(data)
        net = model.Model(len(encoder), hidden_size=10)
        objective = nn.CrossEntropyLoss()
        optimizer = optim.SGD(net.parameters(), lr=0.1)
        max_loss = min_loss = None

        for _ in range(1000):
            net.zero_grad()
            packed_seq, true_indices = input.batch_to_train(data, encoder)
            out, _ = net(packed_seq)
            v_loss = objective(out, true_indices)
            v_loss.backward()
            optimizer.step()
            l = v_loss.data[0]
            if max_loss is None:
                max_loss = min_loss = l
            else:
                max_loss = max(max_loss, l)
                min_loss = min(min_loss, l)

        res = [model.generate_name(net, encoder) for _ in range(10)]
        print(res)
コード例 #5
0
ファイル: train.py プロジェクト: Shmuma/pytorch_tests
log = logging.getLogger("train")

if __name__ == "__main__":
    logging.basicConfig(
        format="%(asctime)-15s %(levelname)s %(name)-14s %(message)s",
        level=logging.INFO)
    parser = argparse.ArgumentParser()
    parser.add_argument("--cuda",
                        default=False,
                        action='store_true',
                        help="Enable cuda")
    args = parser.parse_args()

    data = input.read_data()
    input_encoder = input.InputEncoder(data)

    log.info("Read %d train samples, encoder len=%d, first 10: %s", len(data),
             len(input_encoder), ', '.join(data[:10]))

    net = model.Model(len(input_encoder), hidden_size=HIDDEN_SIZE)
    if args.cuda:
        net = net.cuda()
    objective = nn.CrossEntropyLoss()

    lr = 0.004
    # decay of LR every 100 epoches
    lr_decay = 0.9
    optimizer = optim.RMSprop(net.parameters(), lr=lr)

    for epoch in range(EPOCHES):
コード例 #6
0
 def test_batch_to_train(self):
     enc = input.InputEncoder(['abc'])
     packed_seq, true_vals = input.batch_to_train(['a', 'b', 'c'], enc)
     self.assertEqual(true_vals.data.numpy().tolist(),
                      [2, 1, 0, 3, 1, 0, 4, 1, 1])
コード例 #7
0
 def test_end_token(self):
     enc = input.InputEncoder(['abc'])
     r = enc.end_token()
     self.assertEqual(5, r.shape[0])