def init(self, input_size):
super(RNN, self).init()
self.rnn = nn.GRU(
input_size=input_size,
hidden_size=64,
num_layers=1,
batch_first=True,
)
self.out = nn.Sequential(
nn.Linear(64, 1),
)
self.hidden = None
def forward(self, x):
r_out, self.hidden = self.rnn(x) # None 表示 hidden state 会用全0的 state
out = self.out(r_out)
return out
class TrainSet(Dataset):
def init(self, data):
定义好 image 的路径
self.data, self.label = data[:, :-1].float(), data[:, -1].float()
def getitem(self, index):
return self.data[index], self.label[index]
def len(self):
return len(self.data)
n为模型中的n
LR是模型的学习率
EPOCH是多次循环
train_end这个在之前的数据集中有提到。(注意是负数)
n = 30
LR = 0.0001
EPOCH = 100
train_end = -500
loss选用mse
预测的数据选择“收盘价”
n = 10
LR = 0.0001
EPOCH = 100
train_end = -500
数据集建立
df, df_all, df_index = readData(‘收盘价’, n=n, train_end=train_end)
df_all = np.array(df_all.tolist())
plt.plot(df_index, df_all, label=‘real-data’)
df_numpy = np.array(df)
df_numpy_mean = np.mean(df_numpy)
df_numpy_std = np.std(df_numpy)
df_numpy = (df_numpy - df_numpy_mean) / df_numpy_std
df_tensor = torch.Tensor(df_numpy)
trainset = TrainSet(df_tensor)
trainloader = DataLoader(trainset, batch_size=16, shuffle=True)
rnn = RNN(n)
optimizer = torch.optim.Adam(rnn.parameters(), lr=LR) # optimize all cnn parameters
loss_func = nn.MSELoss()
for step in ra