v2使用is_static未生效
Created by: zyz282994112
paddle v2环境,提前预训练后,使用is_static固定参数,但部分layer没有生效(例如time_layer),代码如下:
def network(self, is_predict=False, inputdata=None):
word_dict_size = self._word_dict_size
vocab_dict_size = self._vocab_dict_size
hidden_size = self._hidden_size
########################
#reset_parser()
if not inputdata:
inputdata = self.input_layer()
query, query_time, month, year = inputdata
query_emb = paddle.layer.embedding(input=query, size=hidden_size, name=self.model+'_wordemb', param_attr=paddle.attr.ParameterAttribute(is_static=True))
query_time_emb = paddle.layer.embedding(input=query_time, size=hidden_size, name=self.model+'_wordtimeemb', param_attr=paddle.attr.ParameterAttribute(is_static=True))
lstm_max_query = self.query_emb_func(query_emb, hidden_size)
lstm_max_query_time = self.query_emb_func(query_time_emb, hidden_size)
time_layer = paddle.layer.fc(size=32, input=[year, month], act=paddle.activation.Relu(), param_attr=paddle.attr.ParameterAttribute(is_static=True))
feature_concat_layer = paddle.layer.concat(input=[lstm_max_query, lstm_max_query_time, time_layer])
feature_concat_layer = paddle.layer.batch_norm(input=feature_concat_layer, act=paddle.activation.Relu(), param_attr=paddle.attr.ParameterAttribute(is_static=True))
if self.model == 'ctr':
output = paddle.layer.fc(size=2, input=feature_concat_layer, act=paddle.activation.Softmax(), param_attr=paddle.attr.ParameterAttribute(is_static=True))
elif self.model == 'acp':
output = paddle.layer.fc(size=1, input=feature_concat_layer, act=paddle.activation.Linear(), param_attr=paddle.attr.ParameterAttribute(is_static=True))
elif self.model == 'cpm':
std_acp = self.acp_model.network(is_predict=True, inputdata=inputdata)
#acp = std_acp * 4.0 + 3.0
acp = paddle.layer.scale_shift(input=std_acp, bias_attr=True)
ctr = self.ctr_model.network(is_predict=True, inputdata=inputdata)
with paddle.layer.mixed(size=1) as truectr:
truectr += paddle.layer.identity_projection(input=ctr, offset=1)
output = truectr * acp
else:
exit(1)
if is_predict:
if self.model == 'cpm':
output = output * 1000.0
return output
else:
if self.model == 'ctr':
weight = paddle.layer.data(name="weight", type=paddle.data_type.dense_vector(1))
label = paddle.layer.data(name="label", type=paddle.data_type.integer_value(2))
cost = paddle.layer.cross_entropy_cost(input=output, label=label, weight=weight)
elif self.model == 'acp':
weight = paddle.layer.data(name="weight", type=paddle.data_type.dense_vector(1))
acp = paddle.layer.data(name="std_acp", type=paddle.data_type.dense_vector(1))
cost = paddle.layer.square_error_cost(input=output, label=acp, weight=weight)
elif self.model == 'cpm':
ctr_weight = paddle.layer.data(name="ctr_weight", type=paddle.data_type.dense_vector(1))
ctr_label = paddle.layer.data(name="label", type=paddle.data_type.integer_value(2))
ctr_cost = paddle.layer.cross_entropy_cost(input=ctr, label=ctr_label, weight=ctr_weight)
acp_weight = paddle.layer.data(name="acp_weight", type=paddle.data_type.dense_vector(1))
acp_label = paddle.layer.data(name="std_acp", type=paddle.data_type.dense_vector(1))
acp_cost = paddle.layer.square_error_cost(input=std_acp, label=acp_label, weight=acp_weight)
cpm_weight = paddle.layer.data(name="cpm_weight", type=paddle.data_type.dense_vector(1))
cpm_label = paddle.layer.data(name="cpm", type=paddle.data_type.dense_vector(1))
cpm_label = cpm_label * 0.001
cpm_cost = paddle.layer.square_error_cost(input=output, label=cpm_label, weight=cpm_weight)
cost = [ctr_cost, acp_cost, cpm_cost]
else:
exit(1)
return cost