记录写的第一个深度学习网络–pytorch做回归任务
数据格式
用open-change7个变量预测label
代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch
from sklearn import preprocessing
data = pd.read_csv(r'stock_train.csv',encoding='utf-8')
data_test1 = pd.read_csv(r'stock_test.csv',encoding='utf-8')
y_train = np.array(data.label)
y_train = y_train.reshape(-1, 1)
y_train = preprocessing.StandardScaler().fit_transform(y_train)
data_train = data.drop('label', axis = 1)
data_train = data_train.drop(columns = 'date', axis = 1)
x_train = np.array(data_train)
x_train = preprocessing.StandardScaler().fit_transform(x_train)
y_test = np.array(data_test1.label)
y_test = y_test.reshape(-1, 1)
y_test = preprocessing.StandardScaler().fit_transform(y_test)
data_test = data_test1.drop('label', axis = 1)
data_test = data_test.drop(columns = 'date', axis = 1)
x_test = np.array(data_test)
x_test = preprocessing.StandardScaler().fit_transform(x_test)
input_size = 7
hidden_size = 128
output_size = 1
batch_size = 16
model = torch.nn.Sequential(
torch.nn.Linear(input_size, hidden_size),
torch.nn.Sigmoid(),
torch.nn.Linear(hidden_size, output_size)
)
cost = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.0001)
losses = []
train_loss = []
for i in range(500):
batch_loss = []
pre = []
for start in range(0, len(data_train), batch_size):
end = start + batch_size if start + batch_size < len(data_train) else len(data_train)
xx = torch.tensor(x_train[start:end], dtype = torch.float, requires_grad = True)
yy = torch.tensor(y_train[start:end], dtype = torch.float, requires_grad = True)
prediction = model(xx)
loss = cost(prediction, yy)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
batch_loss.append(loss.data.numpy())
train_loss=batch_loss
print(np.mean(train_loss))
x = torch.tensor(x_test, dtype = torch.float)
predict = model(x).data.numpy()
plt.plot(data_test1['date'], y_test, label = 'truth_ground')
plt.plot(data_test1['date'], predict, label = 'test_pre')
plt.legend()
plt.show()
x = [210.34,211.86,209.71,211.97,32650000,22263100,0.008520969]
x = x.reshape(1, -1)
x = preprocessing.StandardScaler().fit_transform(x)
x_vertify = torch.tensor(x, dtype = torch.float)
predict = model(x_vertify).data.numpy()
print(predict)
一些记录
- x.reshape(1,-1)含义是将x改变成一行,任意列大小的数组,-1的含义是不用指定具体个数
- torch.nn.MSELoss() reduction = ‘none’则返回一组向量,reduction ≠ ‘none’则返回一个数,有sum和mean(默认)两种方法
- torch.nn.X和torch.nn.functional.X区别:前面的是类用之前需要实例化,后面的是函数直接调用
|