-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
130 lines (111 loc) · 4.94 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import (
CategoricalDenseModel,
WeightedSum,
AutoEncoder,
SkipAutoEncoder,
Conv2dUnet,
TransformerEncoder,
)
class CUTAWAS(nn.Module):
def __init__(self, behavior_length, portrait_length, window_length, target_day, vocab_size_dict, **kwargs):
"""
PyTorch implementation of the CUTAWAS model.
Args:
behavior_length (int): Length of behavior features.
portrait_length (int): Length of portrait features.
window_length (int): Number of time steps.
target_day (int): Output dimensionality.
vocab_size_dict (dict).
"""
super(CUTAWAS, self).__init__()
self.behavior_length = behavior_length
self.portrait_length = portrait_length
self.window_length = window_length
self.target_day = target_day
self.vocab_size_dict = vocab_size_dict
self.dense_layers = [128, 64]
self.attention_num = 3
self.trans_output_dim = 64
# Build the user model.
self.user_model = self.build_user_model()
# Build the portrait static submodel
self.portrait_static_autoencoder = AutoEncoder(16)
# Build the portrait time-series submodels
self.portrait_conv_unet = self.get_conv_unet(self.portrait_length)
self.portrait_transformer = self.get_transformer_encoder(self.portrait_length)
# Use separate instances of WeightedSum for portrait and behavior streams.
self.weighted_sum_portrait = WeightedSum()
# Build the behavior time-series submodels
self.behavior_conv_unet = self.get_conv_unet(self.behavior_length)
self.behavior_transformer = self.get_transformer_encoder(self.behavior_length)
self.weighted_sum_behavior = WeightedSum()
# Build the final skip-autoencoder (the dense layers)
self.skip_autoencoder = SkipAutoEncoder(init_channel_dim=32, depth=2, output_dim=self.target_day)
def build_user_model(self):
# In TensorFlow, the user model was:
# CategoricalDenseModel()(self.vocab_size_dict)
return CategoricalDenseModel(self.vocab_size_dict)
def get_transformer_encoder(self, feature_dim):
return TransformerEncoder(
window_length=self.window_length,
feature_dim=feature_dim,
dense_layers=self.dense_layers,
trans_output_dim=self.trans_output_dim,
add_time2vec=True,
additional_dropout=False,
attention_layer_num=self.attention_num,
)
def get_conv_unet(self, feature_dim):
return Conv2dUnet(
window_length=self.window_length,
feature_dim=feature_dim,
init_channel_dim=16,
depth=2,
output_dim=1
)
def forward(self, user_inputs, portrait, behavior):
"""
Args:
user_inputs: Input(s) for the user model
portrait: Tensor of shape [batch_size, window_length, portrait_length]
behavior: Tensor of shape [batch_size, window_length, behavior_length]
Returns:
Tensor of shape [batch_size, target_day]
"""
# Process user features.
user_out = self.user_model(user_inputs)
# Process portrait static features.
# Take the last time step from the portrait input.
portrait_static_input = portrait[:, -1, :] # shape: [batch, portrait_length]
portrait_static_out = self.portrait_static_autoencoder(portrait_static_input)
# Process portrait time-series features.
portrait_conv_out = self.portrait_conv_unet(portrait)
portrait_transformer_out = self.portrait_transformer(portrait)
# Weighted ensemble of the two portrait outputs.
portrait_ts_out = self.weighted_sum_portrait([portrait_conv_out, portrait_transformer_out])
# Process behavior time-series features.
behavior_conv_out = self.behavior_conv_unet(behavior)
behavior_transformer_out = self.behavior_transformer(behavior)
behavior_ts_out = self.weighted_sum_behavior([behavior_conv_out, behavior_transformer_out])
# Concatenate all features along the last dimension.
concatenated = torch.cat(
[user_out, portrait_static_out, portrait_ts_out, behavior_ts_out],
dim=-1
)
# Pass through the final skip-autoencoder/dense layers.
output = self.skip_autoencoder(concatenated)
return output
def get_config(self):
return {
'behavior_length': self.behavior_length,
'portrait_length': self.portrait_length,
'window_length': self.window_length,
'target_day': self.target_day,
'vocab_size_dict': self.vocab_size_dict,
'dense_layers': self.dense_layers,
'attention_num': self.attention_num,
'trans_output_dim': self.trans_output_dim,
}