-
Notifications
You must be signed in to change notification settings - Fork 110
/
quantized_TF.py
51 lines (44 loc) · 1.94 KB
/
quantized_TF.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformer.decoder import Decoder
from transformer.multihead_attention import MultiHeadAttention
from transformer.positional_encoding import PositionalEncoding
from transformer.pointerwise_feedforward import PointerwiseFeedforward
from transformer.encoder_decoder import EncoderDecoder
from transformer.encoder import Encoder
from transformer.encoder_layer import EncoderLayer
from transformer.decoder_layer import DecoderLayer
from transformer.batch import subsequent_mask
from transformer.embeddings import Embeddings
from transformer.generator import Generator
import numpy as np
import scipy.io
import os
import copy
import math
class QuantizedTF(nn.Module):
def __init__(self, enc_inp_size, dec_inp_size, dec_out_size, N=6,
d_model=512, d_ff=2048, h=8, dropout=0.1):
super(QuantizedTF, self).__init__()
"Helper: Construct a model from hyperparameters."
c = copy.deepcopy
attn = MultiHeadAttention(h, d_model)
ff = PointerwiseFeedforward(d_model, d_ff, dropout)
position = PositionalEncoding(d_model, dropout)
self.model = EncoderDecoder(
Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N),
Decoder(DecoderLayer(d_model, c(attn), c(attn),
c(ff), dropout), N),
nn.Sequential(Embeddings(d_model,enc_inp_size), c(position)),
nn.Sequential(Embeddings(d_model,dec_inp_size), c(position)),
Generator(d_model, dec_out_size))
# This was important from their code.
# Initialize parameters with Glorot / fan_avg.
for p in self.model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, *input):
return self.model.generator(self.model(*input))
def predict(self,*input):
return F.softmax(self.model.generator(self.model(*input)), dim=-1)