闲鱼安乐 2022-10-19 08:47 采纳率: 75%
浏览 52
已结题

pytorch搭建的cnn-lstm的Tensor问题


import torch
from torch import nn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from torch.nn import MaxPool2d, Conv2d, Dropout, ReLU
from torch.utils.data import DataLoader, Dataset

#准备数据集
df=pd.read_csv("train.csv",parse_dates=["Date"],index_col=[0])
print(df.shape)
train_data_size=round(len(df)*0.8)
test_data_size=round(len(df)*0.2)
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

# df[['Open']].plot()
# plt.ylabel("stock price")
# plt.xlabel("times")
# plt.show()

sel_col = ['Open', 'High', 'Low', 'Close']
df=df[sel_col]

df_close_max=df['Close'].max()
df_close_min=df['Close'].min()
print("最高价=", df_close_max)
print("最低价=", df_close_min)
print("波动值=", df_close_max-df_close_min)
print("上涨率=", (df_close_max-df_close_min)/df_close_min)
print("下跌率=", (df_close_max-df_close_min)/df_close_max)

df=df.apply(lambda x:(x-min(x))/(max(x)-min(x)))
print(df)

total_len=df.shape[0]
print("df.shape=",df.shape)
print("df_len=", total_len)

sequence=10
x=[]
y=[]

for i in range(total_len-sequence):

    x.append(np.array(df.iloc[i:(i+sequence),].values,dtype=np.float32))
    y.append(np.array(df.iloc[(i+sequence),1],dtype=np.float32))
print("train data  of item  0: \n", x[0])
print("train label of item  0: \n", y[0])

print("\n序列化后的数据形状:")
X = np.array(x)
Y = np.array(y)
Y = np.expand_dims(Y, 1)
print("X.shape =",X.shape)
print("Y.shape =",Y.shape)

train_x = X[:int(0.7 * total_len)]
train_y = Y[:int(0.7 * total_len)]


# 数据集前70%后的数据(30%)作为验证集
valid_x = X[int(0.7 * total_len):]
valid_y = Y[int(0.7 * total_len):]

print("训练集x的形状是:",train_x.shape)
print("测试集y的形状是:",train_y.shape)
print("测试集x的形状是:",valid_x.shape)
print("测试集y的形状是:",valid_y.shape)


class Mydataset(Dataset):

    def __init__(self, x, y, transform=None):
        self.x = x
        self.y = y

    def __getitem__(self, index):
        x1 = self.x[index]
        y1 = self.y[index]
        return x1, y1

    def __len__(self):
        return len(self.x)

dataset_train = Mydataset(train_x, train_y)
dataset_valid = Mydataset(valid_x, valid_y)

train_dataloader=DataLoader(dataset_train,batch_size=64)
valid_dataloader=DataLoader(dataset_valid,batch_size=64)
# print(train_dataloader)
# print(valid_dataloader)
class cnn_lstm(nn.Module):
    def __init__(self,window_size,feature_number):
        super(cnn_lstm, self).__init__()
        self.window_size=window_size
        self.feature_number=feature_number
        self.conv1 = Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=2)
        self.relu1 = ReLU()
        self.maxpooling1 = MaxPool2d(2, stride=1, padding="same")
        self.dropout1 = Dropout(0.3)
        self.lstm1 = nn.LSTM(input_size=64 * feature_number, hidden_size=128, num_layers=1, batch_first=True)
        self.lstm2 = nn.LSTM(input_size=128, hidden_size=64, num_layers=1, batch_first=True)
        self.fc = nn.Linear(in_features=64, out_features=32)
        self.relu2 = nn.ReLU()
        self.head = nn.Linear(in_features=32, out_features=1)

    def forward(self, x):

            # x = x.reshape([x.shape[0], 1, self.window_size, self.feature_number])
            x = x.transpose(-1, -2)
            x = self.conv1(x)
            x = self.relu1(x)
            x = self.pool(x)
            x = self.dropout(x)

            # x = x.reshape([x.shape[0], self.window_size, -1])
            x = x.transpose(-1, -2)  #
            x, (h, c) = self.lstm1(x)
            x, (h, c) = self.lstm2(x)
            x = x[:, -1, :]  # 最后一个LSTM只要窗口中最后一个特征的输出
            x = self.fc(x)
            x = self.relu2(x)
            x = self.head(x)

            return x

#创建网络模型
cnn_lstm=cnn_lstm(window_size=10,feature_number=4)

#定义损失函数
loss_fn=nn.MSELoss(size_average=True)

#定义优化器
learning_rate=0.01
opitmizer=torch.optim.Adam(cnn_lstm.parameters(),learning_rate)

#设置训练网络参数
total_train_step=0
total_valid_step=0

#训练论数
epoch=10

for i in range(epoch):
    print("______第{}轮训练开始________".format((i + 1)))
    y_train_pred=cnn_lstm(train_x)
    loss=loss_fn(train_x,train_y)

    #优化器优化模型
    opitmizer.zero_gard()
    loss.backward()
    opitmizer.step()

    total_train_step = total_train_step + 1
    if total_train_step % 100 == 0:
        print("训练次数:{},loss:{}".format(total_train_step, loss.item()))

请问在这个数据集划分的部分,在哪里可以添加 将数据类型转化为totensor的格式

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-19 09:13
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月31日
  • 已采纳回答 10月23日
  • 创建了问题 10月19日

悬赏问题

  • ¥15 selenium获取非固定位置的元素
  • ¥50 手写签名不能上传的问题
  • ¥30 linux odbc怎么添加gbase数据库
  • ¥20 电脑开机黑屏,只有一个鼠标,联想zj者y7000
  • ¥20 DXSDK_jun10
  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码