-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimplement
49 lines (35 loc) · 1.22 KB
/
implement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#RNN
import torch
import torchvision
from torch import nn
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
'''
Data preprocessing
'''
-----
class RnnModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size, layer_num):
super(RnnModel, self).__init__()
self.hidden_size = hidden_size
self.layer_num = layer_num
#batch_first – If True, then the input and output tensors are provided as (batch, seq, feature).
self.rnn = nn.RNN(input_size, hidden_size, layer_num, batch_first=True)
self.full_connect = nn.Linear(hidden_size, output_size)
def forward(self, x):
batch_size = x.size(0)
# Initializing hidden state for the first input
hidden = torch.zeros(self.layer_num, batch_size, self.hidden_size)
# hidden_new = F(input_new,hidden)
out, hidden = self.rnn(x, hidden)
out = self.full_connect(out)
return out, hidden
model = RnnModel(input_size=, output_size=, hidden_size =, layer_num = )
‘’‘
Training model
’‘’
epoch = 1000
lr=0.01
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
----