Exemplo n.º 1
0
def train(data_loader, model, epoch_num, batch_size):
    # generate data
    xtrain, ytrain, xtest, ytest = data_loader

    # training bar
    process_bar = sqdm()

    for epoch in range(epoch_num):
        print(f"Epoch [{epoch + 1}/{epoch_num}]")
        for xdata, ydata in data_iter(batch_size, xtrain, ytrain):
            model.fit(xdata, ydata)

            # train
            train_pred = model.predict_prob(xdata)
            train_loss = model.entropy_loss(train_pred,
                                            ydata.reshape(train_pred.shape))
            train_acc = model.score(xdata, ydata)

            # test
            test_pred = model.predict_prob(xtest)
            test_loss = model.entropy_loss(test_pred,
                                           ytest.reshape(test_pred.shape))
            test_acc = model.score(xtest, ytest)

            process_bar.show_process(len(ytrain),
                                     batch_size,
                                     train_loss=train_loss,
                                     test_loss=test_loss,
                                     train_score=train_acc,
                                     test_score=test_acc)

        print("\n")
    return model
def train(x, y, model, epoch_num, batch_size, alpha, weight_decay):
    model = model(alpha=alpha, weight_decay=weight_decay)
    for epoch in range(epoch_num):
        print(f"Epoch [{epoch+1}/{epoch_num}]")
        for xdata, ydata in data_iter(batch_size, x, y):
            model.fit(xdata, ydata)
            mse = model.score(xdata, ydata)
            process_bar.show_process(len(y), batch_size, mse)
        print("\n")
    return model
Exemplo n.º 3
0
def train(epoch_num, net, loss, batch_size, lr):
    """train function"""
    for epoch in range(epoch_num):
        print(f"Epoch [{epoch+1}/{epoch_num}]")
        for xdata, ydata in data_iter(batch_size, x, y):
            l = loss(net(xdata, w, b), ydata)
            l.backward()
            sgd([w, b], lr)

            # clear grad, aviod grad accumulate
            w.grad.data.zero_()
            b.grad.data.zero_()

            # training bar
            mse = np.round(loss(net(xdata, w, b), ydata).item(), 5)
            process_bar.show_process(len(y), batch_size, mse)
        print("\n")
Exemplo n.º 4
0
    # load iris data
    iris = load_iris()
    x = iris.data
    y = np.array(list(map(lambda x: label_dict[x], list(iris.target))))

    # define model
    model = MLP(0.03, 4, 3, 5)
    # split dataset
    xtrain, xtest, ytrain, ytest = train_test_split(x, y)

    # training bar
    process_bar = sqdm()
    for epoch in range(params["epoch_num"]):
        print(f"Epoch [{epoch + 1}/{params['epoch_num']}]")
        for xdata, ydata in data_iter(params["batch_size"], xtrain, ytrain):
            # data fit
            model.fit(xdata, ydata)
            # training
            train_pred = model.predict(xdata)
            train_loss = model.entropy_loss(train_pred, ydata)
            train_acc = model.score(xdata, ydata.argmax(axis=1))

            # test
            test_pred = model.predict(xtest)
            test_loss = model.entropy_loss(test_pred, ytest)
            test_acc = model.score(xtest, ytest.argmax(axis=1))
            process_bar.show_process(len(xtrain), params["batch_size"], train_loss=train_loss,
                                     train_score=train_acc, test_loss=test_loss, test_score=test_acc)

        print("\n")
x = torch.normal(0, 1, size=(input_num, len(true_w)))
error = torch.normal(0, 0.01, size=(input_num, ))
y = torch.mv(x, true_w) + true_b + error
"""model training"""
params = {"epoch_num": 10, "lr": 0.01, "weight_decay": 0.05, "batch_size": 128}
# parameter init
torch.manual_seed(100)
w = torch.normal(0, 0.01, size=true_w.shape, requires_grad=True)
b = torch.zeros(1, requires_grad=True)

# training bar
process_bar = sqdm()

for epoch in range(params["epoch_num"]):
    print(f"Epoch [{epoch+1}/{params['epoch_num']}]")
    for xdata, ydata in data_iter(params["batch_size"], x, y):
        y_pred = linreg(xdata, w, b)
        l = squared_loss(y_pred, ydata.reshape(y_pred.shape))
        l.backward()
        sgd2([w, b], len(ydata), params["lr"], params["weight_decay"])

        # clear grad, aviod grad accumulate
        _ = w.grad.data.zero_()
        _ = b.grad.data.zero_()

        process_bar.show_process(input_num, params["batch_size"], l.item())
    print("\n")

print(f"w before update is {true_w}, w after update is {w}")
print(f"b before update is {true_b}, b after update is {b}")