forked from Angel-ML/PyTorch-On-Angel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnn.py
165 lines (143 loc) · 6.22 KB
/
pnn.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Tencent is pleased to support the open source community by making Angel available.
#
# Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# https://opensource.org/licenses/Apache-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
#
# !/usr/bin/env python
from __future__ import print_function
import argparse
import torch
import torch.nn.functional as F
from torch import Tensor
from typing import List
class PNN(torch.nn.Module):
def __init__(self, input_dim=-1, n_fields=-1, embedding_dim=-1, fc_dims=[]):
super(PNN, self).__init__()
self.loss_fn = torch.nn.BCELoss()
self.input_dim = input_dim
self.n_fields = n_fields
self.embedding_dim = embedding_dim
self.mats = []
# local model do not need real input_dim to init params, so set fake_dim to
# speed up to produce local pt file.
fake_input_dim = 10
if input_dim > 0 and embedding_dim > 0 and n_fields > 0 and fc_dims:
self.bias = torch.nn.Parameter(torch.zeros(1, 1))
self.weights = torch.nn.Parameter(torch.zeros(fake_input_dim, 1))
self.embedding = torch.nn.Parameter(torch.nn.init.xavier_uniform_(torch.zeros(fake_input_dim, embedding_dim)))
product_linear = torch.nn.Parameter(
torch.nn.init.kaiming_uniform_(torch.zeros(fc_dims[0], n_fields * embedding_dim),
mode='fan_in', nonlinearity='relu'))
product_bias = torch.nn.Parameter(torch.zeros(1, 1))
self.mats.append(product_linear)
self.mats.append(product_bias)
num_pairs = (int)(n_fields * (n_fields - 1) / 2)
product_quadratic_inner = torch.nn.Parameter(
torch.nn.init.kaiming_uniform_(torch.zeros(fc_dims[0], num_pairs),
mode='fan_in', nonlinearity='relu'))
product_quadratic_inner = torch.jit.annotate(Tensor, product_quadratic_inner)
self.mats.append(product_quadratic_inner)
dim = fc_dims[0]
for fc_dim in fc_dims[1:]:
w = torch.nn.init.kaiming_uniform_(torch.nn.Parameter(torch.zeros(dim, fc_dim)),
mode='fan_in', nonlinearity='relu')
self.mats.append(w)
b = torch.nn.Parameter(torch.zeros(1, 1))
self.mats.append(b)
dim = fc_dim
def first_order(self, batch_size, index, values, bias, weights):
# type: (int, Tensor, Tensor, Tensor, Tensor) -> Tensor
size = batch_size
srcs = weights.view(1, -1).mul(values.view(1, -1)).view(-1)
output = torch.zeros(size, dtype=torch.float32)
output.scatter_add_(0, index, srcs)
first = output + bias
return first
def product(self, batch_size, embeddings, mats):
# type: (int, Tensor, List[Tensor]) -> Tensor
b = batch_size
bn = embeddings.size(0)
embedding_dim = embeddings.size(1)
n_fileds = (int)(bn / b)
x = embeddings.view(b, -1)
product_linear_out = x.matmul(mats[0].t())
x_1 = embeddings.view(b, n_fileds, embedding_dim)
indices = torch.triu_indices(n_fileds, n_fileds, 1)
p = torch.index_select(x_1, 1, indices[0])
q = torch.index_select(x_1, 1, indices[1])
product_inner_out = torch.sum(p * q, 2) # b * num_pairs
product_inner_out = product_inner_out.matmul(mats[2].t())
output = torch.relu(product_linear_out + product_inner_out + mats[1])
return output
def deep(self, batch_size, product_input, mats):
# type: (int, Tensor, List[Tensor]) -> Tensor
b = batch_size
output = product_input.view(b, -1)
for i in range(int(len(mats) / 2)):
output = torch.relu(output.matmul(mats[i * 2]) + mats[i * 2 + 1])
return output.view(-1) # [b * 1]
def forward_(self, batch_size, index, feats, values, bias, weights, embeddings, mats):
# type: (int, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, List[Tensor]) -> Tensor
first = self.first_order(batch_size, index, values, bias, weights)
product = self.product(batch_size, embeddings, mats[0:3])
output = self.deep(batch_size, product, mats[3:])
return torch.sigmoid(output + first)
def forward(self, batch_size, index, feats, values):
# type: (int, Tensor, Tensor, Tensor) -> Tensor
batch_first = F.embedding(feats, self.weights)
emb = F.embedding(feats, self.embedding)
return self.forward_(batch_size, index, feats, values,
self.bias, batch_first, emb, self.mats)
@torch.jit.export
def loss(self, output, targets):
return self.loss_fn(output, targets)
@torch.jit.export
def get_type(self):
return "BIAS_WEIGHT_EMBEDDING_MATS"
@torch.jit.export
def get_name(self):
return "PNN"
FLAGS = None
def main():
pnn = PNN(FLAGS.input_dim, FLAGS.n_fields, FLAGS.embedding_dim, FLAGS.fc_dims)
pnn_script_module = torch.jit.script(pnn)
pnn_script_module.save("pnn.pt")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--input_dim",
type=int,
default=-1,
help="data input dim."
)
parser.add_argument(
"--n_fields",
type=int,
default=-1,
help="data num fields."
)
parser.add_argument(
"--embedding_dim",
type=int,
default=-1,
help="embedding dim."
)
parser.add_argument(
"--fc_dims",
nargs="+",
type=int,
default=-1,
help="fc layers dim list."
)
FLAGS, unparsed = parser.parse_known_args()
main()