x = torch.tensor([np.array(df.iloc[:,15]),np.array(df.iloc[:,14]),np.array(df.iloc[:,13]),
np.array(df.iloc[:,12]),np.array(df.iloc[:,11]),np.array(df.iloc[:,10]),
np.array(df.iloc[:,9]),np.array(df.iloc[:,8]),np.array(df.iloc[:,7]),
np.array(df.iloc[:,6]),np.array(df.iloc[:,5]),np.array(df.iloc[:,4]),
np.array(df.iloc[:,3]),np.array(df.iloc[:,2]),np.array(df.iloc[:,1]),
                      np.array(df.iloc[:,30]),np.array(df.iloc[:,29]),np.array(df.iloc[:,28]),
                      np.array(df.iloc[:,27]),np.array(df.iloc[:,26]),np.array(df.iloc[:,25]),
                      np.array(df.iloc[:,24]),np.array(df.iloc[:,23]),np.array(df.iloc[:,22]),
                      np.array(df.iloc[:,21]),np.array(df.iloc[:,20]),np.array(df.iloc[:,19]),
                      np.array(df.iloc[:,18]),np.array(df.iloc[:,17]),np.array(df.iloc[:,16])]
                     , dtype=torch.float32)

y = torch.tensor(np_y_buy, dtype=torch.float32)
============================================================
D_in, H1, H2, D_out = 30, 10, 5, 3
class TwoLayerNet(torch.nn.Module):
def init(self, D_in, H1, H2, D_out):
# define the model architechture
super(TwoLayerNet, self).init()
self.linear1 = torch.nn.Linear(D_in, H1, bias = True)
self.linear2 = torch.nn.Linear(H1, H2, bias = True)
self.linear3 = torch.nn.Linear(H2, D_out, bias = True)
def forward(self, x):
y_pred = self.linear3(self.linear2(self.linear1(x).sigmoid()).sigmoid()).sigmoid()
return y_pred
model = TwoLayerNet(D_in, H1, H2, D_out)
loss_fn = nn.BCELoss()
learning_rate = 1e-4
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
for it in range(100):
# forward propagation
y_pred = model(x) # 等价于用 y_pred = model.forward(x)
# compute loss
loss = loss_fn(y_pred, y)
print(it, loss)

optimizer.zero_grad()

# backward propagation
loss.backward()

# update the parameters
optimizer.step()

It said I got a size mismatch error. I do not know why is that