Exemple #1
0
def run(model, loss, epochs=30):
    for epoch in range(epochs):
        model.train()

        run_epoch(data_generator(V, 8, 20), model, loss)

        model.eval()

        run_epoch(data_generator(V, 8, 5), model, loss)

    # 模型进入测试模式
    model.eval()

    # 假定的输入张量
    source = Variable(torch.LongTensor([[1, 3, 2, 5, 4, 6, 7, 8, 9, 10]]))

    # 定义源数据掩码张量, 因为元素都是1, 在我们这里1代表不遮掩
    # 因此相当于对源数据没有任何遮掩.
    source_mask = Variable(torch.ones(1, 1, 10))

    # 最后将model, src, src_mask, 解码的最大长度限制max_len, 默认为10
    # 以及起始标志数字, 默认为1, 我们这里使用的也是1
    result = greedy_decode(model,
                           source,
                           source_mask,
                           max_len=10,
                           start_symbol=1)
    print(result)
Exemple #2
0
def run(model, loss, V, epochs=10):
    start = time.time()
    for epoch in range(epochs):
        model.train()

        # 每批次8个,一个批次循环20次
        run_epoch(data_generator(V, 8, 20), model, loss)

        model.eval()
        print("epochs:%d || time:%s " %(epoch+1, timeSince(start)))

        run_epoch(data_generator(V, 8, 5), model, loss)

    print("--------------------训练完毕,保存模型-----------------")
    torch.save(model.state_dict(), "./model/transform_"+TYPE+".pt")

    # 模型进入测试模式
    model.eval()

    # 假定的输入张量
    source = Variable(torch.LongTensor([[1,3,2,5,4,6,7,8,9,10]])).to(device)

    # 定义源数据掩码张量, 因为元素都是1, 在我们这里1代表不遮掩
    # 因此相当于对源数据没有任何遮掩.
    source_mask = Variable(torch.ones(1, 1, 10)).to(device)

    # 最后将model, src, src_mask, 解码的最大长度限制max_len, 默认为10
    # 以及起始标志数字, 默认为1, 我们这里使用的也是1
    result = greedy_decode(model, source, source_mask, max_len=10, start_symbol=1)
    print("输入的数据:", source)
    print("预测结果是:",result)
Exemple #3
0
def predict(model, sources):
    # 模型进入测试模式
    # print(sources)
    checkpoint = torch.load("./model/transform_"+TYPE+".pt",map_location=device)
    model.load_state_dict(checkpoint)
    model.eval()
    source_mask = Variable(torch.ones(1, 1, 10)).to(device)
    num = 1
    acc_count = 0
    for i in sources:
        source = Variable(torch.LongTensor([[int(j) for j in i.strip("\n").strip(" ").split(",")]])).to(device)
        result = greedy_decode(model, source, source_mask, max_len=10, start_symbol=1)

        if torch.equal(source, result):
            acc_count += 1
        print("第{}     输入数据为:{}".format(num,source))
        print("第{}数据预测的结果是:{}".format(num,result))
        num += 1

    print("准确率为:",acc_count/ len(sources))
Exemple #4
0
def run(model, loss, epochs=10):
    for epoch in range(epochs):
        model.train()

        run_epoch(data_generator(V, 8, 20), model, loss)

        model.eval()

        run_epoch(data_generator(V, 8, 5), model, loss)

    model.eval()

    source = Variable(torch.LongTensor([[1, 3, 2, 5, 4, 6, 7, 8, 9, 10]]))

    source_mask = Variable(torch.ones(1, 1, 10))

    result = greedy_decode(model,
                           source,
                           source_mask,
                           max_len=10,
                           start_symbol=1)
    print(result)