import torch x = torch.FloatTensor([[[1,2], [3,4], [5,6]], [[7,8], [9,10], [11,12], [[13,14], [15,16], [17,18]]]) y = torch.FloatTensor([[[1,2,2], [1,2,2]], [[1,3,3], [1,3,3]], [[1,4,4], [1,4,4]]]) z = torch.bmm(x,y) print(z.size())
torch.Size[[3, 3, 3]]
import torch W = torch.FloatTensor([[1,2], [3,4], [5,6]]) b = torch.FloatTensor([2,2]) def linear(x, W, b): y = torch.matmul(x, W) + b return y x = torch.FloatTensor([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) y= linear(x, W, b) print(y.size)
torch.Size([4,2])
import torch.nn as nn class MyLinear(nn.moduls): def __init__(self, input_dim=3, ouput_dim=2): self.input_dim = input_dim self.output_dim = output_dim super().__init__() # 가중치(weight) self.W = torch.FloatTensor(input_dim, output_dim) # 편향(bias) self.b = torch.FloatTensor(output_dim) def forward(self, x): # |x| = (batch_size, input_dim) y = torch.matmu;(x, self.w) + self.b # |y| = (batch_size, input_dim) * (input_dim, output_dim) # = (batch_size, output_dim) return y linear = MyLinear(3,2) y = linear(X) for p in linear.parameters(): print(p) 아무것도 안나온다(학습이 안되기 때문에)
import torch.nn as nn class MyLinear(nn.moduls): def __init__(self, input_dim=3, ouput_dim=2): self.input_dim = input_dim self.output_dim = output_dim super().__init__() # 가중치(weight), nn.parameter로 감싸기 self.W = nn.Parameter(torch.FloatTensor(input_dim, output_dim)) # 편향(bias), nn.parameter로 감싸기 self.b = nn.Parameter(torch.FloatTensor(output_dim)) def forward(self, x): # |x| = (batch_size, input_dim) y = torch.matmu;(x, self.w) + self.b # |y| = (batch_size, input_dim) * (input_dim, output_dim) # = (batch_size, output_dim) return y
linear = nn.Linear(3,2) y = linear(x) for p in linear.parameters(): print(p)