Problem with simple network

username: derektorch
cooked: <pre><code class="lang-python">import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F

# Define network


class Network(nn.Module):
	def __init__(self, input_dim):
		super(Network, self).__init__()
		self.first_layer = nn.Linear(input_dim, 1)
	
	def forward(self, x):
		out = self.first_layer(x)
                out = f.relu(out)
		return out


# Check for cuda device
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)


# Create dataset

x = np.array([[1, 1], [1, 0], [0, 1], [0, 0]], dtype=float)

# Create tensor from numpy array
x = torch.from_numpy(x)

y = np.array([[1], [0], [0], [0]], dtype=float)
y = torch.from_numpy(y)

# Initialize network

net = Network(2)
net.to(device)

# Setup criterion and optimizer
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(net.parameters(), lr=1e-2)

# Print network parameters
for parameter in net.parameters():
	print(parameter)

for i in range(100):
	print('Epoch:[{}\{}]'.format(i+1, 100))
	for input, target in zip(x, y):

		# Clear gradients
		optimizer.zero_grad()

        # Send input, and expected output to the device
		input = input.to(device)	
		target = target.to(device)

		output = net(input)
		
		# Loss
		loss = criterion(output, target)

		# Backpropagate 
		loss.backward()

		# Update weights
		optimizer.step()

</code></pre>
<p>I have problem with this simple network. I was trying to use the DataLoader and DataTensor but still get the same result as below.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "help.py", line 60, in &lt;module&gt;
    output = net(input)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "help.py", line 16, in forward
    out = self.first_layer(x)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/functional.py", line 1371, in linear
    output = input.matmul(weight.t())
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2'
</code></pre>

username: dhpollack
cooked: <p>Your inputs are float64 (Double) tensors, but the network expects a float32 tensor.  You should either convert the tensor to float with <code>.float()</code> before putting it into the network.  Also, you haven’t really given us enough information about your setup but this is probably because you are creating your inputs with numpy which uses float64 by default.  You could use <code>dtype=np.float32</code> or <code>astype(np.float32)</code> if you are using numpy to create your inputs.</p>

username: derektorch
cooked: <p>Wow I didn’t think about that! Putting <code>.float()</code> after input and target solve the problem, thanks.</p>