-
Notifications
You must be signed in to change notification settings - Fork 29
/
paddle_model.py
executable file
·195 lines (156 loc) · 6.37 KB
/
paddle_model.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
A PaddlePaddle implementation of a question answering model.
"""
from __future__ import print_function
import json
import random
import collections
from collections import namedtuple
import paddle.v2 as paddle
from paddle.v2.layer import parse_network
__all__ = ["build_model"]
EMBEDDING_DIM = 300
def embedding_input(name, vocab_size, drop_rate=0.):
"""
Create an embedding input to the network.
Embeddings are static Glove vectors.
"""
data = paddle.layer.data(
name=name, type=paddle.data_type.integer_value_sequence(vocab_size))
# CAUTIOUS: static parameters must be intialized by pre-trained parameter.
# BUT, currently, if static parameters is not intialized,
# Paddle will not warn you.
embeddings = paddle.layer.embedding(
input=data,
size=EMBEDDING_DIM,
param_attr=paddle.attr.Param(name="GloveVectors", is_static=True),
layer_attr=paddle.attr.ExtraLayerAttribute(drop_rate=drop_rate), )
return embeddings
def binary_output(name):
"""
Create a binary output for the network.
"""
data = paddle.layer.data(
name=name, type=paddle.data_type.integer_value_sequence(2))
return data
def binary_input(name):
"""
Create a binary input for the network.
"""
data = paddle.layer.data(
name=name, type=paddle.data_type.dense_vector_sequence(1))
return data
def bidirectional_lstm(inputs, size, depth, drop_rate=0., prefix=""):
"""
Run a bidirectional LSTM on the inputs.
"""
if not isinstance(inputs, collections.Sequence):
inputs = [inputs]
lstm_last = []
for dirt in ["fwd", "bwd"]:
for i in range(depth):
input_proj = paddle.layer.mixed(
name="%s_in_proj_%0d_%s__" % (prefix, i, dirt),
size=size * 4,
bias_attr=paddle.attr.Param(initial_std=0.),
input=[paddle.layer.full_matrix_projection(lstm)] if i else [
paddle.layer.full_matrix_projection(in_layer)
for in_layer in inputs
])
lstm = paddle.layer.lstmemory(
input=input_proj,
bias_attr=paddle.attr.Param(initial_std=0.),
param_attr=paddle.attr.Param(initial_std=5e-4),
reverse=(dirt == "bwd"))
lstm_last.append(lstm)
final_states = paddle.layer.concat(input=[
paddle.layer.last_seq(input=lstm_last[0]),
paddle.layer.first_seq(input=lstm_last[1]),
])
return final_states, paddle.layer.concat(
input=lstm_last,
layer_attr=paddle.attr.ExtraLayerAttribute(drop_rate=drop_rate), )
def build_document_embeddings(config, documents, same_as_question,
question_vector):
"""
Build the document word embeddings.
"""
hidden = paddle.layer.concat(input=[
documents,
same_as_question,
])
# Half the question embedding is the final states of the LSTMs.
question_expanded = paddle.layer.expand(
input=question_vector, expand_as=documents)
_, hidden = bidirectional_lstm([hidden, question_expanded],
config.layer_size, config.document_layers,
config.hidden_dropout, "__document__")
return hidden
def build_question_vector(config, questions):
"""
Build the question vector.
"""
final, lstm_hidden = bidirectional_lstm(
questions, config.layer_size, config.question_layers,
config.hidden_dropout, "__question__")
# The other half is created by doing an affine transform to generate
# candidate embeddings, doing a second affine transform followed by a
# sequence softmax to generate weights for the embeddings, and summing over
# the weighted embeddings to generate the second half of the question
# embedding.
candidates = paddle.layer.fc(
input=lstm_hidden, size=config.layer_size, act=None)
weights = paddle.layer.fc(
input=questions, size=1, act=paddle.activation.SequenceSoftmax())
weighted = paddle.layer.scaling(input=candidates, weight=weights)
embedding = paddle.layer.pooling(
input=weighted, pooling_type=paddle.pooling.Sum())
return paddle.layer.concat(input=[final, embedding])
def pick_word(config, word_embeddings):
"""
For each word, predict a one or a zero indicating whether it is the chosen
word.
This is done with a two-class classification.
"""
predictions = paddle.layer.fc(
input=word_embeddings, size=2, act=paddle.activation.Softmax())
return predictions
def build_classification_loss(predictions, classes):
"""
Build a classification loss given predictions and desired outputs.
"""
# classification_cost is just multi-class cross entropy,
# but it also add a classification error evaluator.
return paddle.layer.classification_cost(input=predictions, label=classes)
def build_model(config, is_infer=False):
"""
Build the PaddlePaddle model for a configuration.
"""
questions = embedding_input("Questions", config.vocab_size,
config.embedding_dropout)
documents = embedding_input("Documents", config.vocab_size,
config.embedding_dropout)
same_as_question = binary_input("SameAsQuestion")
correct_sentence = binary_output("CorrectSentence")
correct_start_word = binary_output("CorrectStartWord")
correct_end_word = binary_output("CorrectEndWord")
# here the question vector is not a sequence
question_vector = build_question_vector(config, questions)
document_embeddings = build_document_embeddings(
config, documents, same_as_question, question_vector)
sentence_pred = pick_word(config, document_embeddings)
start_word_pred = pick_word(config, document_embeddings)
end_word_pred = pick_word(config, document_embeddings)
if is_infer:
return [sentence_pred, start_word_pred, end_word_pred]
else:
return [
build_classification_loss(sentence_pred, correct_sentence),
build_classification_loss(start_word_pred, correct_start_word),
build_classification_loss(end_word_pred, correct_end_word)
]
if __name__ == "__main__":
from paddle_train import load_config
conf = load_config("paddle-config.json")
losses = build_model(conf)
print(parse_network(losses))