gpt4 book ai didi

machine-learning - BatchNorm1d 需要 2d 输入?

转载 作者:行者123 更新时间:2023-11-30 09:16:16 24 4
gpt4 key购买 nike

我想解决 PyTorch 中的问题。我编写了以下学习正弦函数的代码作为教程。

import torch
from torch import nn
from torch import optim
from torch.autograd import Variable as V
from torch.utils.data import TensorDataset, DataLoader
import numpy as np

# y=sin(x1)
numTrain = 512
numTest = 128
noiseScale = 0.01
PI2 = 3.1415 * 2
X_train = np.random.rand(numTrain,1) * PI2
y_train = np.sin(X_train) + np.random.randn(numTrain,1) * noiseScale + 1.5
X_test = np.random.rand(numTest,1) * PI2
y_test = np.sin(X_test) + np.random.randn(numTest,1) * noiseScale

# Construct DataSet
X_trainT = torch.Tensor(X_train)
y_trainT = torch.Tensor(y_train)
X_testT = torch.Tensor(X_test)
y_testT = torch.Tensor(y_test)
ds_train = TensorDataset(X_trainT, y_trainT)
ds_test = TensorDataset(X_testT, y_testT)

# Construct DataLoader
loader_train = DataLoader(ds_train, batch_size=64, shuffle=True)
loader_test = DataLoader(ds_test, batch_size=64, shuffle=False)

# Construct network
net = nn.Sequential(
nn.Linear(1,10),
nn.ReLU(),
nn.BatchNorm1d(10),
nn.Linear(10,5),
nn.ReLU(),
nn.BatchNorm1d(5),
nn.Linear(5,1),
)
optimizer = optim.Adam(net.parameters())
loss_fn = nn.SmoothL1Loss()

# Training
losses = []
net.train()
for epoc in range(100):
for data, target in loader_train:
y_pred = net(data)
loss = loss_fn(target,y_pred)
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.data)


# evaluation
%matplotlib inline
from matplotlib import pyplot as plt

#plt.plot(losses)
plt.scatter(X_train, y_train)

net.eval()
sinsX = []
sinsY = []
for t in range(128):
x = t/128 * PI2
output = net(V(torch.Tensor([x])))
sinsX.append(x)
sinsY.append(output.detach().numpy())
plt.scatter(sinsX,sinsY)

训练完成,没有错误,但下一行导致错误,“预期的 2D 或 3D 输入(得到 1D 输入)”

output = net(V(torch.Tensor([x])))

如果不使用 BatchNorm1d(),则不会出现此错误。我感觉很奇怪,因为输入是一维的。

如何解决?

谢谢。

更新:我是如何修复的

arr = np.array([x])
output = net(V(torch.Tensor(arr[None,...])))

最佳答案

当处理一维信号时,pyTorch 实际上需要一个二维张量:第一个维度是“小批量”维度。因此,您应该使用一个一维信号批量评估您的网络:

output - net(V(torch.Tensor([x[None, ...]]))

在评估之前,请确保将网络设置为“评估”模式:

net.eval()

关于machine-learning - BatchNorm1d 需要 2d 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55320883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com