bool value of Tensor with more than one value is ambiguous
I'm writing a neural network to do regression and here is my codes:

class Model(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super().__init__()
        self.h1 = nn.Linear(input_size, hidden_size)
        self.h2 = nn.Linear(hidden_size, hidden_size)
        self.h3 = nn.Linear(hidden_size, num_classes)

    def forward(self, x):
        x = self.h1(x)
        x = Fuc.tanh(x)
        x = self.h2(x)
        x = Fuc.relu(x)
        x = self.h3(x)
        return x

model = Model(input_size=input_size, hidden_size=hidden_size, num_classes=num_classes)
opt = optim.Adam(params=model.parameters(), lr=learning_rate)


for epoch in range(1000):
    out = model(data)
    print('target', target)
    print('pred', out)
    loss = torch.nn.MSELoss(out, target)
    print('loss', loss)

    model.zero_grad()
    loss.backward()
    opt.step()


my input is of shape (numberOfSample X 2) and out put is of the form [[2],[3],...], namely a list of lists where each inner list contain one number. 

Ok so Now I train the neural network and got this error:

       ...
       [-0.1753],
        [-0.1753],
        [-0.1753]], grad_fn=&lt;AddmmBackward&gt;)
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1340: UserWarning: nn.functional.tanh is deprecated. Use torch.tanh instead.
  warnings.warn("nn.functional.tanh is deprecated. Use torch.tanh instead.")
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-26-38e8026bfe54&gt; in &lt;module&gt;()
     68     print('target', target)
     69     print('pred', out)
---&gt; 70     loss = torch.nn.MSELoss(out, target)
     71     print('loss', loss)
     72 

2 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/_reduction.py in legacy_get_string(size_average, reduce, emit_warning)
     34         reduce = True
     35 
---&gt; 36     if size_average and reduce:
     37         ret = 'mean'
     38     elif reduce:

RuntimeError: bool value of Tensor with more than one value is ambiguous


The issue originates from calling torch.nn.MSELoss(out, target) which is the constructor for the MSELoss which accepts size_average and reduce as the first and second optional positional arguments.

loss = torch.nn.MSELoss(out, target)


Instead, you need to create an MSELoss object first and pass the out and the target to that object.

criterion = torch.nn.MSELoss()

for epoch in range(1000):
    out = model(data)
    loss = criterion(out, target)
    loss.backward()

