利用卷积神经网络对双色球数据进行预测

利用卷积神经网络对双色球数据进行预测,第1张

承接上一期,利用爬到的双色球数据首先做预处理 *** 作,之后将处理之后的数据放入搭建好的神经网络结构中,从而达到预测下一期开奖号的目的,具体思路如下:

1.爬取数据

爬取2003-2022年共2827组,将每11个数据分为一组,共257组。


其中一组的11个数据中,利用前10期的数据作为训练数据,后一期的数据作为标签。


具体可以见我的之前博客:python爬取双色球2003-2022年所有数据_心之所向521的博客-CSDN博客

2.数据预处理

 利用python对得到的双色球数据预处理_心之所向521的博客-CSDN博客

3.网络搭建与训练

定义网络:

##定义网络
class Rich_Block(nn.Module):
    def __init__(self, input_c, output_c):
        super(Rich_Block, self).__init__()
        self.conv_1x1 = nn.Conv2d(in_channels=input_c,out_channels=16,kernel_size=1)
        self.convh_3x3 = nn.Conv2d(in_channels=16,out_channels=16,kernel_size=(1,3),padding=1)
        self.convv_3x3 = nn.Conv2d(in_channels=16,out_channels=16,kernel_size=(3,1))
        self.conv_last = nn.Conv2d(in_channels=32,out_channels=output_c,kernel_size=1)
        self.relu = nn.ReLU(inplace=True)
    def forward(self,x):
        x = self.conv_1x1(x)
        x1 = self.convh_3x3(x)
        x1 = self.relu(x1)
        x1 = self.convv_3x3(x1)
        x1 = self.relu(x1)
        x_cat = torch.cat((x1,x),dim=1)
        x_out = self.conv_last(x_cat)
        return x_out

class Get_Rich(nn.Module):
    def __init__(self):
        super(Get_Rich, self).__init__()
        self.block1 = Rich_Block(1, 16)
        self.block2 = Rich_Block(16, 32)
        self.block3 = Rich_Block(32, 32)
        self.block4 = Rich_Block(32, 32)
        self.block5 = Rich_Block(32, 64)
        self.block6 = Rich_Block(64, 64)
        self.block7 = Rich_Block(64, 64)
        self.block8 = Rich_Block(64, 128)
        self.block9 = Rich_Block(128, 128)
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
        self.line1 = nn.Linear(128, 128)
        self.line2 = nn.Linear(128, 64)
        self.line3 = nn.Linear(64, 64)
        self.line4 = nn.Linear(64, 7)

    def forward(self, x):
        x = self.block1(x)
        x = self.block2(x)
        x = self.block3(x)
        x = self.block4(x)
        x = self.block5(x)
        x = self.block6(x)
        x = self.block7(x)
        x = self.block8(x)
        x = self.block9(x)
        x = self.avg_pool(x)
        x = x.view(x.size(0), -1)
        x = self.line1(x)
        x = self.line2(x)
        x = self.line3(x)
        x = self.line4(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                n = m.weight.size(1)
                m.weight.data.normal_(0, 0.01)
                m.bias.data.zero_()

网络训练:

def train(EPOCH):

    mlp = Get_Rich()
    print(mlp)  # net architecture
    optimizer = torch.optim.Adam(mlp.parameters(), lr=LR)  # optimize all logistic parameters
    
    loss_func = nn.MSELoss()  # MSE损失函数

    logger = get_logger('./exp.log')  ##添加日志

    logger.info('start training!')

    ##训练
    for epoch in range(EPOCH):

        ##准备数据(训练数据和标签)
        train_path = "data/train/"
        filelist1 = os.listdir(train_path)  ##获取当前路径下所有的子文件

        for item in filelist1:
            dir1 = os.path.join(train_path, item)
            print(dir1)
            ##将txt文件读取为二维矩阵形式
            file = open(dir1, "r")
            row = file.readlines()
            list_text = []
            for line in row:
                line = list(line.strip().split(' '))
                # print(line)
                s = []
                for i in line:
                    s.append(int(i))
                s.pop(0)
                list_text.append(s)
            data = list_text[1: 11]
            label = list_text[0]
            print(data)
            print(label)


            ##转tensor
            data = torch.tensor(data)
            label = torch.tensor(label)
            print(data.shape)
            print(label.shape)

        # for step, (b_x, b_y) in enumerate(train_loader):

            # b_x = b_x.view(-1, 3)
            output = mlp(data.view(1, 1, 10, 7))  # logistic output
            loss = loss_func(output, label)  # 修改后的损失函数
            # loss = Loss(output, b_y)  # 修改后的损失函数
            optimizer.zero_grad()  # clear gradients for this training step
            loss.backward(torch.ones_like(output))  # backpropagation, compute gradients
            optimizer.step()  # apply gradients

            if item.split(".")[0] % 10 == 0:
                test_output = mlp(data)
            
                print('Epoch: ', round(epoch), '| train loss: %.4f' % loss[0].data.numpy())
                

                # 保存模型示例代码
                print('===> Saving models...')
                state = {
                    'state': mlp.state_dict(),
                    'epoch': epoch,  # 将epoch一并保存
                    # 'a': a ##将准确率一并保存
                }
                if not os.path.isdir('checkpoint'):
                    os.mkdir('checkpoint')
                torch.save(state, './checkpoint/autoencoder.t')
    print("数据训练完毕,开始预测!!")

reference:利用深度学习模型预测双色球_超超爱AI的博客-CSDN博客_人工智能大数据预测双色球

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存