我正在嘗試使用 pytorch 庫(kù)構(gòu)建一個(gè)帶有一個(gè)神經(jīng)元的神經(jīng)網(wǎng)絡(luò)。這是我的代碼(錯(cuò)誤在底部)import numpy as npimport randomimport matplotlib.pyplot as pltx_train = np.array([random.randint(1,1000) for x in range(1000)], dtype = np.float32)y_train = np.array([int(num*3+1) for num in x_train], dtype = np.float32)x_test = np.array([random.randint(1,1000) for x in range(1000)], dtype = np.float32)y_test = np.array([int(num*3+1) for num in x_train], dtype = np.float32)X_train = torch.from_numpy(x_train)Y_train = torch.from_numpy(y_train)plt.figure(figsize = (8,8))plt.scatter(X_train, Y_train)plt.show()X_test = torch.from_numpy(x_test)Y_test = torch.from_numpy(y_test)input_size = 1hidden_size = 1output_size = 1learning_rate = 0.1w1 = torch.rand(input_size, hidden_size, requires_grad = True)b1 = torch.rand(hidden_size, output_size, requires_grad = True)for i in range(100): y_pred = X_train.mm(w1).clamp(min = 0).add(b1) loss = (Y_train-y_pred).pow(2).sum() loss.backward() with torch.no_grad(): w1-=w1.grad*learning_rate b1 -= b1.grad*learning_rate w1.grad.zero_() b1.grad.zero_()當(dāng)我運(yùn)行此代碼時(shí),它給了我一個(gè)運(yùn)行時(shí)錯(cuò)誤:RuntimeError Traceback (most recent call last)<ipython-input-84-5142b17ecfff> in <module> 32 33 for i in range(100):---> 34 y_pred = X_train.mm(w1).clamp(min = 0).add(b1) 35 loss = (Y_train-y_pred).pow(2).sum() 36 RuntimeError: matrices expected, got 1D, 2D tensors at C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:192那行代碼有什么問(wèn)題以及如何使其按計(jì)劃工作。
1 回答

烙印99
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
torch
需要 2D 輸入,因此您需要為輸入張量添加一個(gè)新維度。
X_train = torch.from_numpy(x_train[..., np.newaxis]) X_test = torch.from_numpy(x_test[..., np.newaxis])
正如上面有人評(píng)論的那樣,您還可以使用torch.unsqueeze
:
for i in range(100): y_pred = torch.unsqueeze(X_train, 1).mm(w1).clamp(min = 0).add(b1) loss = (Y_train-y_pred).pow(2).sum()
兩者都做同樣的事情。前者將其應(yīng)用于numpy
數(shù)組,后者將其應(yīng)用于torch
張量。兩者都會(huì)產(chǎn)生這種形狀,這是正確的格式torch
:
Out[13]: torch.Size([1000, 1])
添加回答
舉報(bào)
0/150
提交
取消