通过输入是个指标对每天的涨跌进行相关预测,实现的准确率达到93%,加入交叉熵进行相关损失函数,尽量减小过拟合现象,但是在参数的最有参数选择的时候,并没有加入最优适应,需要后期进行相关的模型优化,代码如下
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.autograd import Variable
data_csv = pd.read_csv('D:/Python/gs/data.csv')
data_csv = data_csv.dropna(axis=0, how='any')
data_csv = data_csv.values
data_x = data_csv[:, :10].astype('float32')
data_y = data_csv[:, 10].astype('int32')
data_x_normed = (data_x - np.min(data_x, axis=0)) / (np.max(data_x, axis=0)-np.min(data_x, axis=0))
#data_x_normed=(data_x-np.mean(data_x,axis=0))/np.std(data_x,axis=0)
data_x = np.array(data_x_normed)
data_y = np.array(data_y)
train_size = int(len(data_x) * 0.9)
test_size = len(data_x) - train_size
train_x = data_x[:train_size]
train_y = data_y[:train_size]
test_x = data_x[train_size:]
test_y = data_y[train_size:]
train_x = train_x.reshape(-1, 1, 10)
#train_y = train_y.reshape(-1, 1, 1)
train_x = torch.from_numpy(train_x)
train_y = torch.from_numpy(train_y)
class NET(nn.Module):
def __init__(self,input_size=10,hidden_size=40,output_size=2,num_layer=2):
super(NET,self).__init__()
self.rnn=nn.LSTM(input_size,hidden_size,num_layer)
self.out=nn.Linear(hidden_size,output_size)
def forward(self,x):
out,_=self.rnn(x)
out=self.out(out[:,-1,:])
return out
net = NET()
optimizer = torch.optim.Adam(net.parameters(), lr=0.08, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
loss_func = torch.nn.CrossEntropyLoss()
for epoch in range(1000):
var_x=Variable(train_x).type(torch.FloatTensor)
var_y=Variable(train_y).type(torch.LongTensor)
out = net(var_x)
loss = loss_func(out, var_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print('Epoch: {}, Loss: {:.5f}'.format(epoch + 1, loss.data.numpy()))
test_x = test_x.reshape(-1, 1, 10)
test_x = torch.from_numpy(test_x)
var_data = Variable(test_x)
pred_test = net(var_data)
#pred_test = pred_test.view(-1).data.numpy()
pred_test=torch.max(pred_test,1)[1].data.numpy().squeeze()
plt.plot(pred_test, 'r', label='prediction')
plt.plot(test_y, 'b', label='real')
plt.legend(loc='best')
plt.show()
print(pred_test,'prediction number')
print(test_y,'real number')
j=0
for i in range(test_size):
if(pred_test[i] == test_y[i]):
j=j+1;
j=j/test_size
print('Identification:',j)