一、制作自己的数据集
- 源代码
import torch
from torch.utils.data import Dataset
import pandas as pd
import numpy as np
txt_path = 'G:/stock/path.txt'
class SocktData(Dataset):
dataset = []
def __init__(self,txt_path):
fh = open(txt_path)
for line in fh:
line = line.rstrip('\n')
line = line.split('\t')
if line[1] == 'Rising':
label = 0
elif line[1] == 'Falling':
label = 1
else:
label = 2
self.dataset.append([line[0],label])
def __len__(self):
return len(self.data)
def __getitem__(self, index):
data_path,label = self.dataset[index]
data = pd.read_csv(data_path)
data_array = np.array(data)
data_tensor = torch.Tensor(data_array)
data_tensor = data_tensor.unsqueeze(0)
print('----------------------getitem--------------------')
print('------------------打印data_tensor类型-------------')
print(data_tensor.size())
return data_tensor,label
train_set = SocktData(txt_path)
data,label = train_set[0]
print('-----------打印第一个数据内容和标签类型-------------')
print('type(data) = ',type(data))
print('type(label) = ',type(label))
- 执行结果:
![执行结果](https://img-blog.csdnimg.cn/202002172152057.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzU5Mzcw,size_16,color_FFFFFF,t_70)
- 评价:目前数据集构建好了~ 准备构建卷积神经网络。
二、构建卷积神经网络
- 源代码
import torch.nn as nn
import torch.nn.functional as F
depth = [4,8]
data_row = 32
data_col = 9
batch_size = 2
class ConvNet(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1,depth[0],5,padding=2)
self.pool = nn.MaxPool2d(2,2)
self.conv2 = nn.Conv2d(depth[0],depth[1],5,padding=2)
self.fc1 = nn.Linear(112,24)
self.fc2 = nn.Linear(24,3)
def forward(self,x):
x = F.relu(self.conv1(x))
x = self.pool(x)
x = F.relu(self.conv2(x))
x = self.pool(x)
x = x.view(-1,112)
x = F.relu(self.fc1(x))
x= F.dropout(x,training=self.training)
x = self.fc2(x)
x = F.log_softmax(x,dim = 0)
return x
"""计算预测正确率的函数,其中predictions是模型给出的一组预测结果,batch_size行num_classes列的矩阵,labels是数据之中的正确的答案"""
def accuracy(predictions,labels):
pred = torch.max(predictions.data,1)[1]
right_num = pred.eq(labels.data.view_as(pred)).sum()
return right_num,len(labels)
net = ConvNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(),lr=0.001,momentum = 0.9)
record = []
weights = []
num_epochs = 2
for epoch in range(num_epochs):
train_accuracy = []
for batch_id,(data,label) in enumerate(train_loader):
net.train()
output = net(data)
loss = criterion(output,label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
accuracies = accuracy(output,label)
train_accuracy.append(accuracies)
print('Epoch [{}/{}] \tLoss{:.0f}'.format(
batch_id*batch_size,len(train_loader.dataset),loss.item())
)
- 执行结果
损失函数打印错误
![损失函数](https://img-blog.csdnimg.cn/20200218220616604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzU5Mzcw,size_16,color_FFFFFF,t_70)
- 评价:
需要改进的地方:
(1)全连接层:参数是否定义错误?
(2)损失函数:损失函数记录target和output差别,并且进行纠偏;使得映射相对正确;
(3)样本数量问题:样本数量28是否太小?重复过多是否有影响?
(4)遇到问题,数据列数为9列,如何池化呢?数据都比较重要的情况下,应该不能够填0吧~
(5)简单卷积神经网络就构建好啦~ 还有待改进,明天加油!!!