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

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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测