예제 #1
0
def train(team_raw_data, opt):

    train_x = []
    train_y = []
    train_data = train_data_func()
    if opt.dataset == "all":
        test_data = test_data_func()
        train_data += test_data_func
    for x in train_data:
        home = x[1]
        away = x[0]
        home_vector = team_raw_data[home]
        away_vector = team_raw_data[away]

        away_state = x[2:4]
        home_state = x[4:6]

        input_vector = home_vector.tolist() + home_state + away_vector.tolist() + away_state

        train_x.append(input_vector)
        train_y.append(x[-1])

    __svm = svm.svm_model(train_x, train_y)
    
    if opt.dataset == "train":
        svm.save_model(__svm, 'train_svm_%d.pkl.pkl')
    elif opt.dataset == "all":
        svm.save_model(__svm, 'all_svm_%d.pkl.pkl')
    else:
        print("choose dataset error")
def train(team_raw_data, opt):

    train_x = []
    train_y = []
    train_data = train_data_func()
    if opt.dataset == "all":
        test_data = test_data_func()
        train_data += test_data_func

    bayes_model = bayes.Bayes()
    for x in train_data:
        home = x[1]
        away = x[0]
        home_vector = team_raw_data[home]
        away_vector = team_raw_data[away]
        away_state = np.array(x[2:4])
        home_state = np.array(x[4:6])

        input_vector = home_vector.tolist() + away_vector.tolist(
        ) + home_state.tolist() + away_state.tolist()
        input_vector = np.array(input_vector)
        train_x.append(input_vector)
        train_y.append(x[-1])

    bayes_model.train(train_x, train_y)
    bayes_model.save_model()
    test(team_raw_data, opt)
def train(team_raw_data, opt):

    train_x = []
    train_y = []
    train_data = train_data_func()
    if opt.dataset == "all":
        test_data = test_data_func()
        train_data += test_data
    __boost = boost.Boost()
    for x in train_data:
        home = x[1]
        away = x[0]
        home_vector = team_raw_data[home]
        away_vector = team_raw_data[away]

        away_state = x[2:4]
        home_state = x[4:6]
        input_vector = (home_vector -
                        away_vector).tolist() + home_state + away_state

        train_x.append(input_vector)
        train_y.append(x[-1])

    __boost.train(train_x, train_y)
    __boost.save_model()
예제 #4
0
def train_dnn_batch(epoches, team_data, opt):
    """
    train mini batch dnn here
    :return: 
    :rtype: 
    """

    batch_size = opt.batch_size
    LEARNING_RATE = 0.0005
    dnn = SimDNN(TEAM_VECTOR_SIZE)
    if opt.cuda == 1:
        dnn.cuda()
    dnn_optimizer = optimizer.Adamax(dnn.parameters(), lr=LEARNING_RATE)
    prob_criterion = torch.nn.CrossEntropyLoss()
    score_criterion = torch.nn.MSELoss()
    train_data = train_data_func()
    if opt.dataset == "all":
        test_data = test_data_func()
        train_data += test_data
    print("Starting to train with DNN")
    for epoch in range(epoches):
        random.shuffle(train_data)
        if epoch == 15:
            LEARNING_RATE = LEARNING_RATE / 5.0
            for param_group in dnn_optimizer.param_groups:
                param_group['lr'] = LEARNING_RATE
        if epoch == 25:
            LEARNING_RATE = LEARNING_RATE / 10.0
            for param_group in dnn_optimizer.param_groups:
                param_group['lr'] = LEARNING_RATE
        for i in range(0, len(train_data) - batch_size, batch_size):
            batch_home_current_state = Variable(
                torch.zeros((batch_size, CURRENT_COMP_VECTOR_SIZE)))
            batch_away_current_state = Variable(
                torch.zeros((batch_size, CURRENT_COMP_VECTOR_SIZE)))

            batch_home_vector = Variable(
                torch.zeros((batch_size, TEAM_VECTOR_SIZE)))
            batch_away_vector = Variable(
                torch.zeros((batch_size, TEAM_VECTOR_SIZE)))

            batch_score = Variable(torch.zeros((batch_size, 2)))
            batch_result = []

            for p in range(batch_size):
                away_id = train_data[i + p][0]
                home_id = train_data[i + p][1]

                away_current_state = train_data[i + p][2:4]
                home_current_state = train_data[i + p][4:6]
                score = [train_data[i + p][7], train_data[i + p][6]]
                away_vector = team_data[away_id]
                home_vector = team_data[home_id]
                result = train_data[i + p][8]

                batch_home_current_state[p] = Variable(
                    torch.FloatTensor(home_current_state))
                batch_away_current_state[p] = Variable(
                    torch.FloatTensor(away_current_state))
                batch_away_vector[p] = Variable(torch.FloatTensor(away_vector))
                batch_home_vector[p] = Variable(torch.FloatTensor(home_vector))
                batch_score[p] = Variable(torch.FloatTensor(score))
                batch_result.append(result)
            batch_result = Variable(torch.LongTensor(batch_result))

            if opt.cuda == 1:
                # Use GPU
                batch_home_current_state = batch_home_current_state.cuda()
                batch_away_current_state = batch_home_current_state.cuda()
                batch_home_vector = batch_home_vector.cuda()
                batch_away_vector = batch_away_vector.cuda()
                batch_result = batch_result.cuda()
                batch_score = batch_score.cuda()

            output_prob = dnn.forward(home_vector=batch_home_vector,
                                      home_state=batch_home_current_state,
                                      away_vector=batch_away_vector,
                                      away_state=batch_away_current_state)
            loss = prob_criterion(output_prob, batch_result)

            dnn_optimizer.zero_grad()
            loss.backward()
            dnn_optimizer.step()

            if i % 100 == 0:
                print("Epoches: %s Sample: %s Loss: %s" %
                      (epoch, i + 1, loss.data[0]))

        # Test after training
        if opt.dataset == "train":
            save_model(dnn, 'train_dnn_%d.pkl' % epoch)
            opt.model_name = 'train_dnn_%d.pkl' % epoch
            test(team_data, opt)
        elif opt.dataset == "all":
            save_model(dnn, 'all_dnn_%d.pkl' % epoch)
        else:
            print("dataset error")
예제 #5
0
def train_dnn(epoches, team_data, opt):
    """
    train dnn without Mini Batch here
    :return: 
    :rtype: 
    """
    LEARNING_RATE = 0.0001
    dnn = SimDNN(TEAM_VECTOR_SIZE)
    if opt.cuda == 1:
        dnn.cuda()
    dnn_optimizer = optimizer.RMSprop(dnn.parameters(), lr=LEARNING_RATE)
    prob_criterion = torch.nn.CrossEntropyLoss()
    score_criterion = torch.nn.MSELoss()
    train_data = train_data_func()
    if opt.dataset == "all":
        test_data = test_data_func()
        train_data += test_data

    print("Starting to train with DNN")
    for epoch in range(epoches):
        random.shuffle(train_data)
        if epoch == 35:
            LEARNING_RATE = LEARNING_RATE / 5.0
            for param_group in dnn_optimizer.param_groups:
                param_group['lr'] = LEARNING_RATE
        if epoch == 100:
            LEARNING_RATE = LEARNING_RATE / 10.0
            for param_group in dnn_optimizer.param_groups:
                param_group['lr'] = LEARNING_RATE
        for i in range(len(train_data)):
            away_id = train_data[i][0]
            home_id = train_data[i][1]

            away_current_state = train_data[i][2:4]
            home_current_state = train_data[i][4:6]
            score = [train_data[i][7], train_data[i][6]]
            away_vector = team_data[away_id]
            home_vector = team_data[home_id]
            result = [train_data[i][8]]

            if opt.cuda == 1:
                home_current_state = Variable(
                    torch.FloatTensor(home_current_state).cuda())
                away_current_state = Variable(
                    torch.FloatTensor(away_current_state).cuda())

                away_vector = Variable(torch.FloatTensor(away_vector).cuda())
                home_vector = Variable(torch.FloatTensor(home_vector).cuda())
                prob = Variable(torch.LongTensor(result).cuda())
                score = Variable(torch.FloatTensor(score).cuda())
            else:
                home_current_state = Variable(
                    torch.FloatTensor(home_current_state))
                away_current_state = Variable(
                    torch.FloatTensor(away_current_state))

                away_vector = Variable(torch.FloatTensor(away_vector))
                home_vector = Variable(torch.FloatTensor(home_vector))
                prob = Variable(torch.LongTensor(result))

                score = Variable(torch.FloatTensor(score))

            home_current_state = home_current_state.unsqueeze(0)
            away_current_state = away_current_state.unsqueeze(0)
            home_vector = home_vector.unsqueeze(0)
            away_vector = away_vector.unsqueeze(0)

            output_prob = dnn.forward(home_vector=home_vector,
                                      home_state=home_current_state,
                                      away_vector=away_vector,
                                      away_state=away_current_state)

            loss = prob_criterion(output_prob, prob)

            dnn_optimizer.zero_grad()
            loss.backward()
            dnn_optimizer.step()

            if i % 100 == 0:
                print("Epoches: %s Sample: %s Loss: %s" %
                      (epoch, i + 1, loss.data[0]))

        if opt.dataset == "train":
            save_model(dnn, 'train_dnn_%d.pkl' % epoch)
            opt.model_name = 'train_dnn_%d.pkl' % epoch
            test(team_data, opt)
        elif opt.dataset == "all":
            save_model(dnn, 'all_dnn_%d.pkl' % epoch)
        else:
            print("dataset error")