-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
53 lines (43 loc) · 1.46 KB
/
Model.py
File metadata and controls
53 lines (43 loc) · 1.46 KB
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
50
51
52
53
"""
Module containing Pytorch machine learning models.
"""
import torch
import torch.nn as nn
# Select CPU or GPU for Pytorch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# device = torch.device("cpu") # Force CPU
class RNN(nn.Module):
"""
Pytorch RNN model to be used for predictions on Python code token sequences.
Parameters
----------
input_size : int
Size of the input vocabulary.
hidden_size : int
Size of the hidden field to be used by the GRU layer.
output_size : int
Size of the output vector.
Attributes
----------
hidden_size : int
Size of the hidden field to be used by the GRU layer.
embedding : Embedding
Embedding layer for input embeddings.
gru : GRU
GRU NN layer.
linear : Linear
Linear NN layer.
"""
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.embedding = nn.Embedding(input_size, hidden_size)
self.gru = nn.GRU(hidden_size, hidden_size, dropout=0.1)
self.linear = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden):
embedded = self.embedding(input).view(1, 1, -1)
x, hidden = self.gru(embedded, hidden)
output = self.linear(x)
return output, hidden
def initHidden(self):
return torch.zeros(1, 1, self.hidden_size, device=device)