48 lines
No EOL
1.2 KiB
Python
48 lines
No EOL
1.2 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
|
|
class RNN(nn.Module):
|
|
|
|
def __init__(self, input_size, hidden_size, output_size):
|
|
|
|
super(RNN, self).__init__()
|
|
|
|
self.hidden_size = hidden_size
|
|
|
|
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
|
|
|
|
self.h2o = nn.Linear(hidden_size, output_size)
|
|
|
|
self.softmax = nn.LogSoftmax(dim=1)
|
|
|
|
def forward(self, input, hidden):
|
|
compbined = torch.cat((input, hidden), 1)
|
|
|
|
hidden = self.i2h(compbined)
|
|
|
|
output = self.h2o(hidden)
|
|
output = self.softmax(output)
|
|
|
|
return output, hidden
|
|
|
|
def initHidden(self):
|
|
return torch.zeros(1, self.hidden_size)
|
|
|
|
|
|
|
|
|
|
class Net(nn.Module):
|
|
|
|
def __init__(self,input_size,hidden_size,output_size):
|
|
super(Net, self).__init__()
|
|
self.fc1 = nn.Linear(input_size, hidden_size)
|
|
self.fc2 = nn.Linear(hidden_size, hidden_size)
|
|
self.fc3 = nn.Linear(hidden_size, output_size)
|
|
|
|
def forward(self, x):
|
|
x = F.sigmoid(self.fc1(x))
|
|
x = F.sigmoid(self.fc2(x))
|
|
x = self.fc3(x)
|
|
|
|
return F.log_softmax(x, dim=-1) |