Exemplo n.º 1
0
        s = data[case]
        hidden = model.initHidden()
        t, o = makeForOneCase(s, one_hot_var_target)
        output, hidden = model(t.cuda(), hidden)
        loss += criterion(output, o.cuda())
        counts += 1
    loss = loss / counts
    print "=====", loss.data[0]


print "start training"
for epoch in range(epochNum):
    for batchIndex in range(int(TRAINSIZE / batch)):
        model.zero_grad()
        loss = 0
        counts = 0
        for case in range(batchIndex * batch,
                          min((batchIndex + 1) * batch, TRAINSIZE)):
            s = data[case]
            hidden = model.initHidden()
            t, o = makeForOneCase(s, one_hot_var_target)
            output, hidden = model(t.cuda(), hidden)
            loss += criterion(output, o.cuda())
            counts += 1
        loss = loss / counts
        loss.backward()
        print epoch, loss.data[0]
        optimizer.step()
    test()
torch.save(model, 'poetry-gen.pt')
Exemplo n.º 2
0
        output, hidden = model(s_in, hidden)
        loss += criterion(output, s_o)
        counts += 1
    loss = loss / counts
    return loss.item()


print("start training")
for epoch in range(epochs):
    for batchIndex in range(int(data_size / batch_size)):
        loss = 0
        counts = 0
        for case in range(batchIndex * batch_size,
                          min((batchIndex + 1) * batch_size, data_size)):
            s = data[case]
            hidden = model.initHidden(device=device)
            s_in, s_o = makeForOneCase(s, one_hot_var_target)
            s_in, s_o = s_in.to(device), s_o.to(device)
            output, hidden = model(s_in, hidden)
            loss += criterion(output, s_o)
            counts += 1
        loss = loss / counts
        print("epoch {}, batch {}, loss {}".format(epoch, batchIndex,
                                                   loss.item()))
        # compute accuracy and record loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    # every epoch evaluate the model
    valid_loss = evaluate()
    print("epoch {}, current valid loss {}".format(epoch, valid_loss))