pytorch中tensor进行reshape操作后原始数据的顺序

在pytorch中,经常需要对tensor进行reshape操作,使其符合特定网络的输入格式。在将网络的输

出重新reshape回输入前的形状时,tensor的特征是否还是按输入的顺序进行排列?

带着疑问做了下面的实验

x1 = torch.randn(2, 3)

x2 = torch.randn(2, 3)

x3 = torch.randn(2, 3)


x4 = torch.stack((x1, x2, x3), 0)


shape = x4.shape

print("x4:", x4.shape)

print("x4:\n", x4)

x4 = x4.reshape(x4.shape[0]*x4.shape[1], x4.shape[-1])

print("reshaped x4:", x4.shape)

print("reshaped x4:\n", x4)

x4 = x4.reshape(shape[0], shape[1], shape[-1])

print("recovered x4:\n", x4, x4.shape)
# print("x5:\n", x5)

输出

x4: torch.Size([3, 2, 3])
x4:
 tensor([[[-1.2061,  0.0617,  1.1632],
         [-1.5008, -1.5944, -0.0187]],

        [[-2.1325, -0.5270, -0.1021],
         [ 0.0099, -0.4454, -1.4976]],

        [[-0.9475, -0.6130, -0.1291],
         [-0.4107,  1.3931, -0.0984]]])
reshaped x4: torch.Size([6, 3])
reshaped x4:
 tensor([[-1.2061,  0.0617,  1.1632],
        [-1.5008, -1.5944, -0.0187],
        [-2.1325, -0.5270, -0.1021],
        [ 0.0099, -0.4454, -1.4976],
        [-0.9475, -0.6130, -0.1291],
        [-0.4107,  1.3931, -0.0984]])
recovered x4:
 tensor([[[-1.2061,  0.0617,  1.1632],
         [-1.5008, -1.5944, -0.0187]],

        [[-2.1325, -0.5270, -0.1021],
         [ 0.0099, -0.4454, -1.4976]],

        [[-0.9475, -0.6130, -0.1291],
         [-0.4107,  1.3931, -0.0984]]]) torch.Size([3, 2, 3])

将x1, x2和x3三个tensor通过stack操作堆到一起后通过reshape操作改变维度的形状

接着再将reshape完的tensor变回原来的形状,发现输出数据的顺序和改变形状之前相同

表明在reshape过程中,tensor能够保持数据的顺序

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个基于PyTorch的LSTM时间序列数据预测程序,这里以预测某股票的价格为例: ```python import numpy as np import pandas as pd import torch import torch.nn as nn from torch.autograd import Variable # 数据预处理 def create_dataset(data, look_back=1): dataX, dataY = [], [] for i in range(len(data)-look_back): dataX.append(data[i:(i+look_back)]) dataY.append(data[i+look_back]) return np.array(dataX), np.array(dataY) # 导入数据 df = pd.read_csv('data.csv') df = df.dropna() data = df['close'].values.astype('float32') # 数据归一化 from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler(feature_range=(0, 1)) data = scaler.fit_transform(data.reshape(-1, 1)) # 创建训练集和测试集 train_size = int(len(data) * 0.7) test_size = len(data) - train_size train, test = data[0:train_size,:], data[train_size:len(data),:] # 创建输入输出数据 look_back = 5 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # 转换为PyTorch tensor trainX = torch.from_numpy(trainX).type(torch.Tensor) trainY = torch.from_numpy(trainY).type(torch.Tensor) testX = torch.from_numpy(testX).type(torch.Tensor) testY = torch.from_numpy(testY).type(torch.Tensor) # 定义LSTM模型 class LSTM(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(LSTM, self).__init__() self.hidden_size = hidden_size self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x): h0 = torch.zeros(1, x.size(0), self.hidden_size).requires_grad_() c0 = torch.zeros(1, x.size(0), self.hidden_size).requires_grad_() out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach())) out = self.fc(out[:, -1, :]) return out # 训练模型 input_size = 1 hidden_size = 32 output_size = 1 num_epochs = 1000 learning_rate = 0.01 lstm = LSTM(input_size, hidden_size, output_size) criterion = nn.MSELoss() optimizer = torch.optim.Adam(lstm.parameters(), lr=learning_rate) for epoch in range(num_epochs): outputs = lstm(trainX) optimizer.zero_grad() loss = criterion(outputs, trainY) loss.backward() optimizer.step() if epoch % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # 测试模型 lstm.eval() train_predict = lstm(trainX) test_predict = lstm(testX) # 反归一化 train_predict = scaler.inverse_transform(train_predict.detach().numpy()) trainY = scaler.inverse_transform(trainY.detach().numpy()) test_predict = scaler.inverse_transform(test_predict.detach().numpy()) testY = scaler.inverse_transform(testY.detach().numpy()) # 计算MSE from sklearn.metrics import mean_squared_error trainScore = mean_squared_error(trainY, train_predict) testScore = mean_squared_error(testY, test_predict) print('Train Score: {:.2f} MSE'.format(trainScore)) print('Test Score: {:.2f} MSE'.format(testScore)) ``` 在这个程序,我们首先对原始数据进行了数据归一化,并将数据分为训练集和测试集。然后,我们使用create_dataset函数将时间序列数据转换为有监督学习的格式,并使用PyTorch的Variable来存储数据。接着,我们定义了一个LSTM模型,并使用均方误差作为损失函数进行训练。最后,我们使用训练好的模型对训练集和测试集进行预测,并计算了预测的均方误差。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值