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()
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 "help.py", line 60, in <module>
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'