Guest User

Untitled

a guest
Jun 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. def __init__(self, weight):
  2. super(Net, self).__init__()
  3. # initializes the weights of the convolutional layer to be the weights of the 4 defined filters
  4. k_height, k_width = weight.shape[2:]
  5. # assumes there are 4 grayscale filters
  6. self.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False)
  7. self.conv.weight = torch.nn.Parameter(weight)
  8.  
  9. import torch
  10. import torch.nn as nn
  11. from torch.optim import Adam
  12.  
  13. class NN_Network(nn.Module):
  14. def __init__(self,in_dim,hid,out_dim):
  15. super(NN_Network, self).__init__()
  16. self.linear1 = nn.Linear(in_dim,hid)
  17. self.linear2 = nn.Linear(hid,out_dim)
  18. self.linear1.weight = torch.nn.Parameter(torch.zeros(in_dim,hid))
  19. self.linear1.bias = torch.nn.Parameter(torch.ones(hid))
  20. self.linear2.weight = torch.nn.Parameter(torch.zeros(in_dim,hid))
  21. self.linear2.bias = torch.nn.Parameter(torch.ones(hid))
  22.  
  23. def forward(self, input_array):
  24. h = self.linear1(input_array)
  25. y_pred = self.linear2(h)
  26. return y_pred
  27.  
  28. in_d = 5, hidn = 2, out_d = 3
  29. net = NN_Network(in_d, hidn, out_d)
  30.  
  31. for param in net.parameters():
  32. print(type(param.data), param.size())
  33.  
  34. """ Output
  35. <class 'torch.FloatTensor'> torch.Size([5, 2])
  36. <class 'torch.FloatTensor'> torch.Size([2])
  37. <class 'torch.FloatTensor'> torch.Size([5, 2])
  38. <class 'torch.FloatTensor'> torch.Size([2])
  39. """
  40.  
  41. list(net.parameters())
  42.  
  43. opt = Adam(net.parameters(), learning_rate=0.001)
Add Comment
Please, Sign In to add comment