pytorch, How can i make same size of tensor model(x) and answer(x)?
I'm try to make a simple linear model to predict parameters of formula.

y = 3*x1 + x2 - 2*x3 

Unfortunately, there are some problem when i try to compute loss.

def answer(x):
return 3 * x[:,0] + x[:,1] - 2 * x[:,2]


def loss_f(x):  

y = answer(x)
y_hat = model(x)    
loss = ((y - y_hat).pow(2)).sum() / x.size(0)

return loss


When i set batch_size = 3, the size of each result is different

x = torch.randn(3,3)
answer(x)
tensor([ 2.0201, -3.8354,  2.0059])

model(x)
tensor([[ 0.2085],
    [-0.0670],
    [-1.3635]], grad_fn=&lt;ThAddmmBackward&gt;)

answer(x.data).size()
torch.Size([3])
model(x.data).size()
torch.Size([3, 1])


I think the broadcast applied automatically.

loss = ((y - y_hat).pow(2)).sum() / x.size(0)


How can i make same size of two tensors? Thanks
This is my code

import torch
import torch.nn as nn
import torch.optim as optim

class model(nn.Module):

    def __init__(self, input_size, output_size):
        super(model, self).__init__()

        self.linear = nn.Linear(input_size, output_size)

        def forward(self, x):

        y = self.linear(x)

        return y

model = model(3,1)
optimizer = optim.SGD(model.parameters(), lr = 0.001, momentum=0.1)

print('Parameters : ')
for p in model.parameters():
    print(p)

print('')
print('Optimizer : ')
print(optimizer)

def generate_data(batch_size):
    x = torch.randn(batch_size, 3)

    return x

def answer(x):

    return 3 * x[:,0] + x[:,1] - 2 * x[:,2]

def loss_f(x):

    y = answer(x)
    y_hat = model(x)

    loss = ((y - y_hat).pow(2)).sum() / x.size(0)

    return loss

x = torch.randn(3,3)
print(x)
x = torch.FloatTensor(x)

batch_size = 3
epoch_n = 1000
iter_n = 100

for epoch in range(epoch_n):
    avg_loss = 0

    for i in range(iter_n):
        x = torch.randn(batch_size, 3)

        optimizer.zero_grad()
        loss = loss_f(x.data)
        loss.backward()
        optimizer.step()

        avg_loss += loss

    avg_loss = avg_loss / iter_n

    x_valid = torch.FloatTensor([[1,2,3]])
    y_valid = answer(x_valid)

    model.eval()
    y_hat = model(x_valid)
    model.train()

    print(avg_loss, y_valid.data[0], y_hat.data[0])

    if avg_loss &lt; 0.001:
        break


You can use Tensor.view

https://pytorch.org/docs/stable/tensors.html#torch.Tensor.view

So something like

answer(x.data).view(-1, 1)


should do the trick.
