Skip to content

Commit

Permalink
use the same version yapf with paddle main repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcy-seso committed Jan 19, 2018
1 parent 0e844a1 commit bb036f5
Show file tree
Hide file tree
Showing 66 changed files with 505 additions and 501 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- repo: https://github.com/pre-commit/mirrors-yapf.git
sha: v0.16.0
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
hooks:
- id: yapf
files: \.py$
Expand Down
28 changes: 13 additions & 15 deletions conv_seq2seq/beamsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ def get_beam_input(self, batch, sample_list):
for sample_id in sample_list:
for path in self.candidate_path[sample_id]:
if len(path['seq']) < self.win_len:
cur_trg = [self.word_padding] * (self.win_len - len(
path['seq']) - 1) + [self.trg_dict['<s>']] + path['seq']
cur_trg_pos = [self.pos_padding] * (self.win_len - len(
path['seq']) - 1) + [0] + range(1, len(path['seq']) + 1)
cur_trg = [self.word_padding] * (
self.win_len - len(path['seq']) - 1
) + [self.trg_dict['<s>']] + path['seq']
cur_trg_pos = [self.pos_padding] * (
self.win_len - len(path['seq']) - 1) + [0] + range(
1, len(path['seq']) + 1)
else:
cur_trg = path['seq'][-self.win_len:]
cur_trg_pos = range(
Expand Down Expand Up @@ -84,13 +86,11 @@ def beam_expand(self, prob, sample_list):
for seq_id, path in enumerate(self.candidate_path[sample_id]):
for w in top_words[idx, :]:
score = path['score'] + math.log(prob[idx, w])
candidate_words[sample_id] = candidate_words[sample_id] + [
{
'word': w,
'score': score,
'seq_id': seq_id
}
]
candidate_words[sample_id] = candidate_words[sample_id] + [{
'word': w,
'score': score,
'seq_id': seq_id
}]
idx = idx + 1

return candidate_words
Expand Down Expand Up @@ -140,10 +140,8 @@ def beam_shrink(self, candidate_words, sample_list):
w['word']
]
new_path[sample_id] = new_path[sample_id] + [{
'seq':
seq,
'score':
w['score']
'seq': seq,
'score': w['score']
}]

return new_path
Expand Down
39 changes: 20 additions & 19 deletions conv_seq2seq/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,20 @@ def attention(decoder_state, cur_embedding, encoded_vec, encoded_sum):

m = paddle.layer.dot_prod(input1=expanded, input2=encoded_vec)

attention_weight = paddle.layer.fc(
input=m,
size=1,
act=paddle.activation.SequenceSoftmax(),
bias_attr=False)
attention_weight = paddle.layer.fc(input=m,
size=1,
act=paddle.activation.SequenceSoftmax(),
bias_attr=False)

scaled = paddle.layer.scaling(weight=attention_weight, input=encoded_sum)

attended = paddle.layer.pooling(
input=scaled, pooling_type=paddle.pooling.Sum())

attended_proj = paddle.layer.fc(
input=attended,
size=state_size,
act=paddle.activation.Linear(),
bias_attr=True)
attended_proj = paddle.layer.fc(input=attended,
size=state_size,
act=paddle.activation.Linear(),
bias_attr=True)

attention_result = paddle.layer.addto(input=[attended_proj, residual])

Expand Down Expand Up @@ -279,11 +277,10 @@ def attention_step(decoder_state, cur_embedding, encoded_vec, encoded_sum):
if block_input.size == size:
residual = block_input
else:
residual = paddle.layer.fc(
input=block_input,
size=size,
act=paddle.activation.Linear(),
bias_attr=True)
residual = paddle.layer.fc(input=block_input,
size=size,
act=paddle.activation.Linear(),
bias_attr=True)

decoder_state = gated_conv_with_batchnorm(
input=block_input,
Expand Down Expand Up @@ -381,12 +378,14 @@ def conv_seq2seq(src_dict_size,
input=src,
size=emb_dim,
name='src_word_emb',
param_attr=paddle.attr.Param(initial_mean=0., initial_std=0.1))
param_attr=paddle.attr.Param(
initial_mean=0., initial_std=0.1))
src_pos_emb = paddle.layer.embedding(
input=src_pos,
size=emb_dim,
name='src_pos_emb',
param_attr=paddle.attr.Param(initial_mean=0., initial_std=0.1))
param_attr=paddle.attr.Param(
initial_mean=0., initial_std=0.1))

num_attention = len(dec_conv_blocks)
encoded_vec, encoded_sum = encoder(
Expand All @@ -410,12 +409,14 @@ def conv_seq2seq(src_dict_size,
input=trg,
size=emb_dim,
name='trg_word_emb',
param_attr=paddle.attr.Param(initial_mean=0., initial_std=0.1))
param_attr=paddle.attr.Param(
initial_mean=0., initial_std=0.1))
trg_pos_emb = paddle.layer.embedding(
input=trg_pos,
size=emb_dim,
name='trg_pos_emb',
param_attr=paddle.attr.Param(initial_mean=0., initial_std=0.1))
param_attr=paddle.attr.Param(
initial_mean=0., initial_std=0.1))

decoder_out, weight = decoder(
token_emb=trg_emb,
Expand Down
8 changes: 4 additions & 4 deletions conv_seq2seq/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ def train(train_data_path,
src_dict_size = src_dict.__len__()
trg_dict_size = trg_dict.__len__()

optimizer = paddle.optimizer.Adam(
learning_rate=1e-3, )
optimizer = paddle.optimizer.Adam(learning_rate=1e-3, )

cost = conv_seq2seq(
src_dict_size=src_dict_size,
Expand All @@ -182,8 +181,9 @@ def train(train_data_path,

# create parameters and trainer
parameters = paddle.parameters.create(cost)
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=optimizer)
trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters,
update_equation=optimizer)

padding_list = [context_len - 1 for (size, context_len) in dec_conv_blocks]
padding_num = reduce(lambda x, y: x + y, padding_list)
Expand Down
14 changes: 8 additions & 6 deletions ctr/avazu_data_processer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@

feature_dims = {}

categorial_features = ('C1 banner_pos site_category app_category ' +
'device_type device_conn_type').split()
categorial_features = (
'C1 banner_pos site_category app_category ' + 'device_type device_conn_type'
).split()

id_features = 'id site_id app_id device_id _device_id_cross_site_id'.split()

Expand Down Expand Up @@ -335,8 +336,8 @@ def _parse_record(self, row):
else:
fea0 = self.fields[key].cross_fea0
fea1 = self.fields[key].cross_fea1
record.append(
self.fields[key].gen_cross_fea(row[fea0], row[fea1]))
record.append(self.fields[key].gen_cross_fea(row[fea0], row[
fea1]))

sparse_input = concat_sparse_vectors(record, self.id_dims)

Expand Down Expand Up @@ -396,8 +397,9 @@ def ids2sparse(vec):
dnn_input, lr_input = record
dnn_input = ids2dense(dnn_input, feature_dims['dnn_input'])
lr_input = ids2sparse(lr_input)
line = "%s\t%s\n" % (' '.join(map(str, dnn_input)),
' '.join(map(str, lr_input)), )
line = "%s\t%s\n" % (
' '.join(map(str, dnn_input)),
' '.join(map(str, lr_input)), )
f.write(line)
if id > args.test_set_size:
break
Expand Down
23 changes: 12 additions & 11 deletions ctr/network_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ def _build_dnn_submodel_(self, dnn_layer_dims):
'''
build DNN submodel.
'''
dnn_embedding = layer.fc(
input=self.dnn_merged_input, size=dnn_layer_dims[0])
dnn_embedding = layer.fc(input=self.dnn_merged_input,
size=dnn_layer_dims[0])
_input_layer = dnn_embedding
for i, dim in enumerate(dnn_layer_dims[1:]):
fc = layer.fc(
input=_input_layer,
size=dim,
act=paddle.activation.Relu(),
name='dnn-fc-%d' % i)
fc = layer.fc(input=_input_layer,
size=dim,
act=paddle.activation.Relu(),
name='dnn-fc-%d' % i)
_input_layer = fc
return _input_layer

def _build_lr_submodel_(self):
'''
config LR submodel
'''
fc = layer.fc(
input=self.lr_merged_input, size=1, act=paddle.activation.Relu())
fc = layer.fc(input=self.lr_merged_input,
size=1,
act=paddle.activation.Relu())
return fc

def _build_classification_model(self, dnn, lr):
Expand All @@ -95,8 +95,9 @@ def _build_classification_model(self, dnn, lr):

def _build_regression_model(self, dnn, lr):
merge_layer = layer.concat(input=[dnn, lr])
self.output = layer.fc(
input=merge_layer, size=1, act=paddle.activation.Sigmoid())
self.output = layer.fc(input=merge_layer,
size=1,
act=paddle.activation.Sigmoid())
if not self.is_infer:
self.train_cost = paddle.layer.square_error_cost(
input=self.output, label=self.click)
Expand Down
5 changes: 3 additions & 2 deletions ctr/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def train():
params = paddle.parameters.create(model.train_cost)
optimizer = paddle.optimizer.AdaGrad()

trainer = paddle.trainer.SGD(
cost=model.train_cost, parameters=params, update_equation=optimizer)
trainer = paddle.trainer.SGD(cost=model.train_cost,
parameters=params,
update_equation=optimizer)

dataset = reader.Dataset()

Expand Down
4 changes: 3 additions & 1 deletion ctr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,7 @@ def load_dnn_input_record(sent):
def load_lr_input_record(sent):
res = []
for _ in [x.split(':') for x in sent.split()]:
res.append((int(_[0]), float(_[1]), ))
res.append((
int(_[0]),
float(_[1]), ))
return res
19 changes: 9 additions & 10 deletions deep_fm/network_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@


def fm_layer(input, factor_size, fm_param_attr):
first_order = paddle.layer.fc(
input=input, size=1, act=paddle.activation.Linear())
first_order = paddle.layer.fc(input=input,
size=1,
act=paddle.activation.Linear())
second_order = paddle.layer.factorization_machine(
input=input,
factor_size=factor_size,
Expand Down Expand Up @@ -51,17 +52,15 @@ def embedding_layer(input):
sparse_embed_seq = map(embedding_layer, sparse_input_ids)
sparse_embed = paddle.layer.concat(sparse_embed_seq)

fc1 = paddle.layer.fc(
input=[sparse_embed, dense_input],
size=400,
act=paddle.activation.Relu())
fc1 = paddle.layer.fc(input=[sparse_embed, dense_input],
size=400,
act=paddle.activation.Relu())
fc2 = paddle.layer.fc(input=fc1, size=400, act=paddle.activation.Relu())
fc3 = paddle.layer.fc(input=fc2, size=400, act=paddle.activation.Relu())

predict = paddle.layer.fc(
input=[dense_fm, sparse_fm, fc3],
size=1,
act=paddle.activation.Sigmoid())
predict = paddle.layer.fc(input=[dense_fm, sparse_fm, fc3],
size=1,
act=paddle.activation.Sigmoid())

if not infer:
label = paddle.layer.data(
Expand Down
13 changes: 6 additions & 7 deletions deep_fm/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def preprocess(datadir, outdir):
continous_vals = []
for i in range(0, len(continous_features)):
val = dists.gen(i, features[continous_features[i]])
continous_vals.append(
"{0:.6f}".format(val).rstrip('0').rstrip('.'))
continous_vals.append("{0:.6f}".format(val).rstrip('0')
.rstrip('.'))
categorial_vals = []
for i in range(0, len(categorial_features)):
val = dicts.gen(i, features[categorial_features[
Expand All @@ -147,13 +147,12 @@ def preprocess(datadir, outdir):
continous_vals = []
for i in range(0, len(continous_features)):
val = dists.gen(i, features[continous_features[i] - 1])
continous_vals.append(
"{0:.6f}".format(val).rstrip('0').rstrip('.'))
continous_vals.append("{0:.6f}".format(val).rstrip('0')
.rstrip('.'))
categorial_vals = []
for i in range(0, len(categorial_features)):
val = dicts.gen(i,
features[categorial_features[i] -
1]) + categorial_feature_offset[i]
val = dicts.gen(i, features[categorial_features[
i] - 1]) + categorial_feature_offset[i]
categorial_vals.append(str(val))

continous_vals = ','.join(continous_vals)
Expand Down
5 changes: 3 additions & 2 deletions deep_fm/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def train():

params = paddle.parameters.create(model)

trainer = paddle.trainer.SGD(
cost=model, parameters=params, update_equation=optimizer)
trainer = paddle.trainer.SGD(cost=model,
parameters=params,
update_equation=optimizer)

dataset = reader.Dataset()

Expand Down
29 changes: 14 additions & 15 deletions dssm/network_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ def create_fc(self, emb, prefix=""):
"""
_input_layer = paddle.layer.pooling(
input=emb, pooling_type=paddle.pooling.Max())
fc = paddle.layer.fc(
input=_input_layer,
size=self.dnn_dims[1],
param_attr=ParamAttr(name="%s_fc.w" % prefix),
bias_attr=ParamAttr(name="%s_fc.b" % prefix, initial_std=0.))
fc = paddle.layer.fc(input=_input_layer,
size=self.dnn_dims[1],
param_attr=ParamAttr(name="%s_fc.w" % prefix),
bias_attr=ParamAttr(
name="%s_fc.b" % prefix, initial_std=0.))
return fc

def create_rnn(self, emb, prefix=""):
Expand Down Expand Up @@ -161,12 +161,12 @@ def create_dnn(self, sent_vec, prefix):
name = "%s_fc_%d_%d" % (prefix, id, dim)
logger.info("create fc layer [%s] which dimention is %d" %
(name, dim))
fc = paddle.layer.fc(
input=_input_layer,
size=dim,
act=paddle.activation.Tanh(),
param_attr=ParamAttr(name="%s.w" % name),
bias_attr=ParamAttr(name="%s.b" % name, initial_std=0.))
fc = paddle.layer.fc(input=_input_layer,
size=dim,
act=paddle.activation.Tanh(),
param_attr=ParamAttr(name="%s.w" % name),
bias_attr=ParamAttr(
name="%s.b" % name, initial_std=0.))
_input_layer = fc
return _input_layer

Expand Down Expand Up @@ -278,10 +278,9 @@ def _build_classification_or_regression_model(self, is_classification):

if is_classification:
concated_vector = paddle.layer.concat(semantics)
prediction = paddle.layer.fc(
input=concated_vector,
size=self.class_num,
act=paddle.activation.Softmax())
prediction = paddle.layer.fc(input=concated_vector,
size=self.class_num,
act=paddle.activation.Softmax())
cost = paddle.layer.classification_cost(
input=prediction, label=label)
else:
Expand Down
Loading

0 comments on commit bb036f5

Please sign in to comment.