import torch
from torch import nn
x = torch.Tensor([[1, 2],[3,4],[5,6]])
w = torch.Tensor([[1], [-1]])
print(x)
print(w)
out = torch.mm(x,w)
print("---\n",out,"\n---")
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[ 1.],
[-1.]])
---
tensor([[-1.],
[-1.],
[-1.]])
---
# y = x * w.T + b
# nn.Linear(input,neurons)
lin0 = nn.Linear(2, 1, bias = False)
print(lin0.state_dict())
lin0.load_state_dict( {'weight': torch.tensor( [[1, -1]] )})
print(lin0)
out = lin0(x)
print(x)
print(lin0.weight.T)
print(lin0.state_dict())
print("---\n",out,"\n---")
OrderedDict([('weight', tensor([[-0.6404, -0.5404]]))])
Linear(in_features=2, out_features=1, bias=False)
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[ 1.],
[-1.]], grad_fn=<PermuteBackward>)
OrderedDict([('weight', tensor([[ 1., -1.]]))])
---
tensor([[-1.],
[-1.],
[-1.]], grad_fn=<MmBackward>)
---
#serialize(save/restore)
##https://pytorch.org/tutorials/beginner/saving_loading_models.html
PATH = "lin0.txt"
torch.save(lin0.state_dict(), PATH)
#net = TheModelClass(*args, **kwargs)
lin0.load_state_dict(torch.load(PATH))
print(lin0.state_dict())
OrderedDict([('weight', tensor([[ 1., -1.]]))])
net = nn.Sequential(nn.Linear(2, 1, bias=False),
)
print(net)
weight_dict = net.state_dict()
print(weight_dict)
net.load_state_dict( {'0.weight': torch.tensor( [[1, -1]] )})
print(x)
print(net[0].weight.data.T)
print("---\n",net(x),"\n---")
Sequential(
(0): Linear(in_features=2, out_features=1, bias=False)
)
OrderedDict([('0.weight', tensor([[-0.5359, -0.3547]]))])
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[ 1.],
[-1.]])
---
tensor([[-1.],
[-1.],
[-1.]], grad_fn=<MmBackward>)
---
#serialize(save/restore)
##https://pytorch.org/tutorials/beginner/saving_loading_models.html
PATH = "net.txt"
torch.save(net.state_dict(), PATH)
#net = TheModelClass(*args, **kwargs)
net.load_state_dict(torch.load(PATH))
print(net.state_dict())
OrderedDict([('0.weight', tensor([[ 1., -1.]]))])
torch.manual_seed(1);
# Define the size of each layer in our network
n_input = 2 # Number of input units, must match number of input features
n_hidden = 1 # Number of hidden units
n_output = 1 # Number of output units
# Weights for inputs to hidden layer
w = torch.randn(n_input, n_hidden, dtype=torch.float, requires_grad=True)
# and bias terms for hidden and output layers
#b = torch.randn(1, n_hidden, dtype=torch.float, requires_grad=True)
out = torch.mm(x,w)
print(x)
print(w)
print(b)
print("---\n",out,"\n---")
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]], requires_grad=True)
tensor([[0.0617]], requires_grad=True)
---
tensor([[1.1952],
[3.0518],
[4.9083]], grad_fn=<MmBackward>)
---
torch.manual_seed(1);
# y = x * w.T + b
# nn.Linear(input,neurons)
lin0 = nn.Linear(2, 1, bias = False)
print(lin0.state_dict())
#see how are initialized by default weights:
#https://pytorch.org/docs/stable/generated/torch.nn.Linear.html
lin0.load_state_dict( {'weight': w.T})
print(lin0)
out = lin0(x)
print(x)
print(lin0.weight.T)
print(lin0.state_dict())
print("---\n",out,"\n---")
OrderedDict([('weight', tensor([[ 0.3643, -0.3121]]))])
Linear(in_features=2, out_features=1, bias=False)
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]], grad_fn=<PermuteBackward>)
OrderedDict([('weight', tensor([[0.6614, 0.2669]]))])
---
tensor([[1.1952],
[3.0518],
[4.9083]], grad_fn=<MmBackward>)
---
net = nn.Sequential(nn.Linear(2, 1, bias=False),
)
print(net)
weight_dict = net.state_dict()
print(weight_dict)
net.load_state_dict( {'0.weight': w.T })
print(x)
print(net[0].weight.data.T)
print("---\n",net(x),"\n---")
Sequential(
(0): Linear(in_features=2, out_features=1, bias=False)
)
OrderedDict([('0.weight', tensor([[-0.1455, 0.3597]]))])
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]])
---
tensor([[1.1952],
[3.0518],
[4.9083]], grad_fn=<MmBackward>)
---
torch.manual_seed(1);
# Define the size of each layer in our network
n_input = 2 # Number of input units, must match number of input features
n_hidden = 1 # Number of hidden units
n_output = 1 # Number of output units
# Weights for inputs to hidden layer
w = torch.randn(n_input, n_hidden, dtype=torch.float, requires_grad=True)
# and bias terms for hidden and output layers
b = torch.randn(1, n_hidden, dtype=torch.float, requires_grad=True)
out = torch.mm(x,w) + b
print(x)
print(w)
print(b)
print("---\n",out,"\n---")
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]], requires_grad=True)
tensor([[0.0617]], requires_grad=True)
---
tensor([[1.2569],
[3.1134],
[4.9700]], grad_fn=<AddBackward0>)
---
torch.manual_seed(1);
# y = x * w.T + b
# nn.Linear(input,neurons)
lin0 = nn.Linear(2, 1)
print(lin0.state_dict())
#see how are initialized by default weights:
#https://pytorch.org/docs/stable/generated/torch.nn.Linear.html
lin0.load_state_dict( {'weight': w.T, 'bias': b[0]})
print(lin0)
out = lin0(x)
print(x)
print(lin0.weight.T)
print(lin0.bias)
print(lin0.state_dict())
print("---\n",out,"\n---")
OrderedDict([('weight', tensor([[ 0.3643, -0.3121]])), ('bias', tensor([-0.1371]))])
Linear(in_features=2, out_features=1, bias=True)
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]], grad_fn=<PermuteBackward>)
Parameter containing:
tensor([0.0617], requires_grad=True)
OrderedDict([('weight', tensor([[0.6614, 0.2669]])), ('bias', tensor([0.0617]))])
---
tensor([[1.2569],
[3.1134],
[4.9700]], grad_fn=<AddmmBackward>)
---
net = nn.Sequential(nn.Linear(2, 1),
)
print(net)
weight_dict = net.state_dict()
print(weight_dict)
net.load_state_dict( {'0.weight': w.T, '0.bias': b[0]} )
print(x)
print(net[0].weight.data.T)
print(net[0].bias)
print("---\n",net(x),"\n---")
Sequential(
(0): Linear(in_features=2, out_features=1, bias=True)
)
OrderedDict([('0.weight', tensor([[-0.1455, 0.3597]])), ('0.bias', tensor([0.0983]))])
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
tensor([[0.6614],
[0.2669]])
Parameter containing:
tensor([0.0617], requires_grad=True)
---
tensor([[1.2569],
[3.1134],
[4.9700]], grad_fn=<AddmmBackward>)
---
lin0 = nn.Linear(2, 1)
lin0.weight.data.fill_(1)
lin0.bias.data.fill_(1)
print(lin0.state_dict())
OrderedDict([('weight', tensor([[1., 1.]])), ('bias', tensor([1.]))])
net = nn.Sequential(nn.Linear(2, 1),
)
net[0].weight.data.fill_(1)
net[0].bias.data.fill_(1)
print(net.state_dict())
OrderedDict([('0.weight', tensor([[1., 1.]])), ('0.bias', tensor([1.]))])