ranknet模型参数无法共享
Created by: xusoda
使用ranknet模型,结构跟模型库的基本一样。 跑完后用相同的数据拿left和right的参数infer,结果不同。
网络结构 def rank_net_arch(prefix): # ------------------------ data and emb layer -------------------------- # uid uid_data_l = paddle.layer.data( name=prefix + "_uid", type=paddle.data_type.integer_value(uid_dim) )
uid_emb_l = paddle.layer.embedding(
input=uid_data_l,
size=uid_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="uid_emb_param")
)
# source tid
source_tid_data_l = paddle.layer.data(
name=prefix + "_stid",
type=paddle.data_type.integer_value(tid_dim)
)
source_tid_emb_l = paddle.layer.embedding(
input=source_tid_data_l,
size=tid_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="tid_emb_param")
)
# target tid
target_tid_data_l = paddle.layer.data(
name=prefix + "_ttid",
type=paddle.data_type.integer_value(tid_dim)
)
target_tid_emb_l = paddle.layer.embedding(
input=target_tid_data_l,
size=tid_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="tid_emb_param")
)
# source fid
source_fid_data_l = paddle.layer.data(
name=prefix + "_sfid",
type=paddle.data_type.integer_value(fid_dim)
)
source_fid_emb_l = paddle.layer.embedding(
input=source_fid_data_l,
size=fid_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="fid_emb_param")
)
# target fid
target_fid_data_l = paddle.layer.data(
name=prefix + "_tfid",
type=paddle.data_type.integer_value(fid_dim)
)
target_fid_emb_l = paddle.layer.embedding(
input=target_fid_data_l,
size=fid_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="fid_emb_param")
)
# single categorical data:
# such as xf gender level etc
# also include t_fcate,t_scate, s_facte, s_scate
binary_value_data_l = paddle.layer.data(
name=prefix + "_binary",
type=paddle.data_type.sparse_binary_vector(binary_dim, SequenceType.NO_SEQUENCE)
)
# sparse binary vec is sparse connection, should first add a fc layer
binary_value_fc_l = paddle.layer.fc(
input=binary_value_data_l,
size=256,
param_attr=paddle.attr.Param(initial_std=0.01, name="binary_fc_param", l1_rate=6e-4)
)
# single dense feature
# such as display zan etc
real_value_data_l = paddle.layer.data(
name=prefix + "_real",
type=paddle.data_type.dense_vector(real_dim)
)
# the real value do bn before concat
real_value_bn_l = paddle.layer.batch_norm(
input=real_value_data_l,
act=paddle.activation.Relu(),
param_attr=paddle.attr.Param(initial_std=0.01, name="real_value_bn_param")
)
# user tag list taglistv1
user_tag_list_data_l = paddle.layer.data(
name=prefix + "_taglist",
type=paddle.data_type.integer_value_sequence(user_tag_list_dim)
)
user_tag_emb_l = paddle.layer.embedding(
input=user_tag_list_data_l,
size=user_tag_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="tag_emb_param")
)
# pooling with no bias param
user_tag_pool_l = paddle.layer.pooling(
input=user_tag_emb_l,
pooling_type=AvgPooling(),
bias_attr=None,
)
# video secate history
# user_video_secate_his_data_l =paddle.layer.data(
#
# )
# # target post feature this is in cate feature
# target_post_fcate_data_l = paddle.layer.data(
# name=prefix+"_t_fcate",
# type=paddle.data_type.sparse_binary_vector()
# )
target_post_tag_data_l = paddle.layer.data(
name=prefix + "_ttag",
type=paddle.data_type.integer_value_sequence(post_tag_list_dim)
)
target_post_tag_emb_l = paddle.layer.embedding(
input=target_post_tag_data_l,
size=post_tag_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="post_tag_emb_param")
)
target_post_tag_pool_l = paddle.layer.pooling(
input=target_post_tag_emb_l,
pooling_type=AvgPooling(),
bias_attr=None,
)
source_post_tag_data_l = paddle.layer.data(
name=prefix + "_stag",
type=paddle.data_type.integer_value_sequence(post_tag_list_dim)
)
source_post_tag_emb_l = paddle.layer.embedding(
input=source_post_tag_data_l,
size=post_tag_emb_size,
param_attr=paddle.attr.Param(initial_std=0.01, name="post_tag_emb_param")
)
source_post_tag_pool_l = paddle.layer.pooling(
input=source_post_tag_emb_l,
pooling_type=AvgPooling(),
bias_attr=None
)
# ------------------upper layer --------------------
concat_l = paddle.layer.concat(input=[uid_emb_l, user_tag_pool_l,
source_tid_emb_l, target_tid_emb_l,
source_fid_emb_l, target_fid_emb_l,
source_post_tag_pool_l, target_post_tag_pool_l,
binary_value_fc_l, real_value_data_l])
fc_layer1 = paddle.layer.fc(
name=prefix + "_fc_1",
input=concat_l,
size=256,
act=paddle.activation.Relu(),
param_attr=paddle.attr.Param(initial_std=0.01, name="fc1_param")
)
fc_layer2 = paddle.layer.fc(
name=prefix + "_fc_2",
input=fc_layer1,
size=128,
act=paddle.activation.Relu(),
param_attr=paddle.attr.Param(initial_std=0.01, name="fc2_param")
)
output_layer = paddle.layer.fc(
name=prefix + "_output",
input=fc_layer2,
size=1,
act=paddle.activation.Linear(),
param_attr=paddle.attr.Param(initial_std=0.01, name="output", l2_rate=6e-4)
)
return output_layer
def cost_layer(): # label label_l = paddle.layer.data( name="label", type=paddle.data_type.dense_vector(1) )
# left right
output_left = rank_net_arch("left")
output_right = rank_net_arch("right")
# rank_net cost_l
cost_l = paddle.layer.rank_cost(
name="cost",
left=output_left,
right=output_right,
label=label_l
)
return cost_l