Problem with simple network

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')

# 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)

# 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():

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

		# Clear gradients

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

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

		# Backpropagate 

		# Update weights

I have problem with this simple network. I was trying to use the DataLoader and DataTensor but still get the same result as below.

Traceback (most recent call last):
  File "", line 60, in <module>
    output = net(input)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "", line 16, in forward
    out = self.first_layer(x)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/modules/", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/hyperscypion/.local/lib/python3.7/site-packages/torch/nn/", 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'

Your inputs are float64 (Double) tensors, but the network expects a float32 tensor. You should either convert the tensor to float with .float() 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 dtype=np.float32 or astype(np.float32) if you are using numpy to create your inputs.

Wow I didn’t think about that! Putting .float() after input and target solve the problem, thanks.

1 Like