我在pytorch中分裂得不好
I do not split well in pytorch
我想在 pytorch 中进行张量拆分。但是,我收到一条错误消息,因为我无法进行拆分。
我想要的行为是将输入数据拆分为两个完全连接的层。然后我想创建一个模型,将两个全连接层合二为一。
我认为错误是由于 x1, x2 = torch.tensor_split(x,2)
中的错误代码造成的
import torch
from torch import nn, optim
import numpy as np
from matplotlib import pyplot as plt
class Regression(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(1, 32)
self.linear2 = nn.Linear(32, 16)
self.linear3 = nn.Linear(16*2, 1)
def forward(self, x):
x1, x2 = torch.tensor_split(x,2)
x1 = nn.functional.relu(self.linear1(x1))
x1 = nn.functional.relu(self.linear2(x1))
x2 = nn.functional.relu(self.linear1(x2))
x2 = nn.functional.relu(self.linear2(x2))
cat_x = torch.cat([x1, x2], dim=1)
cat_x = self.linear3(cat_x)
return cat_x
def train(model, optimizer, E, iteration, x, y):
losses = []
for i in range(iteration):
optimizer.zero_grad() # 勾配情報を0に初期化
y_pred = model(x) # 予測
loss = E(y_pred.reshape(y.shape), y) # 損失を計算(shapeを揃える)
loss.backward() # 勾配の計算
optimizer.step() # 勾配の更新
losses.append(loss.item()) # 損失値の蓄積
print('epoch=', i+1, 'loss=', loss)
return model, losses
x = np.random.uniform(0, 10, 100) # x軸をランダムで作成
y = np.random.uniform(0.9, 1.1, 100) * np.sin(2 * np.pi * 0.1 * x) # 正弦波を作成
x = torch.from_numpy(x.astype(np.float32)).float() # xをテンソルに変換
y = torch.from_numpy(y.astype(np.float32)).float() # yをテンソルに変換
X = torch.stack([torch.ones(100), x], 1)
net = Regression()
optimizer = optim.RMSprop(net.parameters(), lr=0.01) # 最適化にRMSpropを設定
E = nn.MSELoss() # 損失関数にMSEを設定
net, losses = train(model=net, optimizer=optimizer, E=E, iteration=5000, x=X, y=y)
错误信息
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850
RuntimeError: mat1 and mat2 shapes cannot be multiplied (50x2 and 1x32)
Tl;博士
在torch.tensor_split(x,2)
中指定dim=1
。
说明
x
来自两个形状为[100,1]
的张量堆叠在dim 1处,所以它的形状是[100, 2]
。应用 tensor_split
后,您将得到两个形状均为 [50, 2]
.
的张量
print(x.shape) # torch.Size([100, 2])
print(torch.tensor_split(X,2)[0].shape) # torch.Size([50, 2])
错误发生是因为linear1
只接受形状为[BATCH_SIZE,1]
的张量作为输入,但传入了形状为[50, 2]
的张量。
如果您打算拆分随机数数组和全一数组,请将 torch.tensor_split(x,2)
更改为 torch.tensor_split(x,2,dim=1)
,这会产生两个形状为 [100,1]
的张量。
我想在 pytorch 中进行张量拆分。但是,我收到一条错误消息,因为我无法进行拆分。
我想要的行为是将输入数据拆分为两个完全连接的层。然后我想创建一个模型,将两个全连接层合二为一。
我认为错误是由于 x1, x2 = torch.tensor_split(x,2)
import torch
from torch import nn, optim
import numpy as np
from matplotlib import pyplot as plt
class Regression(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(1, 32)
self.linear2 = nn.Linear(32, 16)
self.linear3 = nn.Linear(16*2, 1)
def forward(self, x):
x1, x2 = torch.tensor_split(x,2)
x1 = nn.functional.relu(self.linear1(x1))
x1 = nn.functional.relu(self.linear2(x1))
x2 = nn.functional.relu(self.linear1(x2))
x2 = nn.functional.relu(self.linear2(x2))
cat_x = torch.cat([x1, x2], dim=1)
cat_x = self.linear3(cat_x)
return cat_x
def train(model, optimizer, E, iteration, x, y):
losses = []
for i in range(iteration):
optimizer.zero_grad() # 勾配情報を0に初期化
y_pred = model(x) # 予測
loss = E(y_pred.reshape(y.shape), y) # 損失を計算(shapeを揃える)
loss.backward() # 勾配の計算
optimizer.step() # 勾配の更新
losses.append(loss.item()) # 損失値の蓄積
print('epoch=', i+1, 'loss=', loss)
return model, losses
x = np.random.uniform(0, 10, 100) # x軸をランダムで作成
y = np.random.uniform(0.9, 1.1, 100) * np.sin(2 * np.pi * 0.1 * x) # 正弦波を作成
x = torch.from_numpy(x.astype(np.float32)).float() # xをテンソルに変換
y = torch.from_numpy(y.astype(np.float32)).float() # yをテンソルに変換
X = torch.stack([torch.ones(100), x], 1)
net = Regression()
optimizer = optim.RMSprop(net.parameters(), lr=0.01) # 最適化にRMSpropを設定
E = nn.MSELoss() # 損失関数にMSEを設定
net, losses = train(model=net, optimizer=optimizer, E=E, iteration=5000, x=X, y=y)
错误信息
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850
RuntimeError: mat1 and mat2 shapes cannot be multiplied (50x2 and 1x32)
Tl;博士
在torch.tensor_split(x,2)
中指定dim=1
。
说明
x
来自两个形状为[100,1]
的张量堆叠在dim 1处,所以它的形状是[100, 2]
。应用 tensor_split
后,您将得到两个形状均为 [50, 2]
.
print(x.shape) # torch.Size([100, 2])
print(torch.tensor_split(X,2)[0].shape) # torch.Size([50, 2])
错误发生是因为linear1
只接受形状为[BATCH_SIZE,1]
的张量作为输入,但传入了形状为[50, 2]
的张量。
如果您打算拆分随机数数组和全一数组,请将 torch.tensor_split(x,2)
更改为 torch.tensor_split(x,2,dim=1)
,这会产生两个形状为 [100,1]
的张量。