示例#1
0
    input_size = train_data.shape[2]
    n_filters = 64
    hidden_size = 64
    EPOCH = 100
    lr = 1e-2

    model = CNN_LSTM(input_size, n_filters, hidden_size, time_step).to(DEVICE)

    if not TEST:
        print('training model...')
        optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
        loss_func = nn.CrossEntropyLoss()
        x = torch.from_numpy(train_data).float()
        y = torch.from_numpy(train_label).long()
        for epoch in range(EPOCH):
            batches = gen_batch(x, y, batch_size)
            loss_sum = 0
            for var_x, var_y in batches:
                pred = model(var_x)
                loss = loss_func(pred, var_y)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                loss_sum += loss.item() * pred.shape[0]
            print('epoch %d loss: %f' % (epoch, loss_sum / x.shape[0]))
            if (epoch + 1) % 50 == 0:
                torch.save(model.state_dict(), tmp_save_path % (epoch + 1))
        torch.save(model.state_dict(), model_save_path)
    else:
        print('loading model...')
        model.load_state_dict(torch.load(model_save_path))
示例#2
0
    print(train_data.shape, test_data.shape, sum(train_label), sum(test_label))

    batch_size = 512
    EPOCH = 500
    lr = 0.001
    model_save_path = './models/nn.model'
    TEST = True
    x = torch.from_numpy(train_data).float()
    y = torch.from_numpy(train_label).long()
    model = NN().to(DEVICE)
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)
    loss_func = torch.nn.CrossEntropyLoss()

    if not TEST:
        for epoch in range(EPOCH):
            batches = gen_batch(x, y, batch_size)
            loss_sum = 0
            for var_x, var_y in batches:
                pred = model(var_x)
                loss = loss_func(pred, var_y)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                loss_sum += loss.item()
            print('epoch %d loss: %f' % (epoch, loss_sum))
        torch.save(model.state_dict(), model_save_path)
    else:
        model.load_state_dict(torch.load(model_save_path))

    var_test_x = torch.from_numpy(test_data).float().to(DEVICE)
    var_test_y = torch.from_numpy(test_label).long().to(DEVICE)