forked from FGiuliari/Trajectory-Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
individual_TF.py
73 lines (57 loc) · 2.42 KB
/
individual_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
import numpy as np
import scipy.io
import os
import copy
import math
class IndividualTF(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,mean=[0,0],std=[0,0]):
super(IndividualTF, 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.mean=np.array(mean)
self.std=np.array(std)
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(LinearEmbedding(enc_inp_size,d_model), c(position)),
nn.Sequential(LinearEmbedding(dec_inp_size,d_model), 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))
class LinearEmbedding(nn.Module):
def __init__(self, inp_size,d_model):
super(LinearEmbedding, self).__init__()
# lut => lookup table
self.lut = nn.Linear(inp_size, d_model)
self.d_model = d_model
def forward(self, x):
return self.lut(x) * math.sqrt(self.d_model)
class Generator(nn.Module):
"Define standard linear + softmax generation step."
def __init__(self, d_model, out_size):
super(Generator, self).__init__()
self.proj = nn.Linear(d_model, out_size)
def forward(self, x):
return self.proj(x)