这里主要给出Wide&Deep的网络构造及训练部分
构建Wide部分
def build_linear(features,first_column):
with tf.variable_scope("Linear"):
wide_logits = tf.feature_column.linear_model(features, first_column, units=1)
return wide_logits
构建Deep部分
'''
hidden_units 128,128,64,32
'''
def build_deep_layers(input_x,parms,mode):
with tf.variable_scope("Deep"):
deep_emb = input_x
for elem in parms['hidden_units'].split(","):
elem = int(elem)
deep_emb = tf.layers.dense(deep_emb, units=elem, activation=None)
if (parms["is_bn"] == True):
if mode == tf.estimator.ModeKeys.TRAIN:
trainable = True
else:
trainable = False
deep_emb = tf.layers.batch_normalization(deep_emb, momentum=0.95,trainable=trainable)
deep_emb = tf.nn.relu(deep_emb)
if (parms["is_dropout"] == True):
deep_emb = tf.layers.dropout(deep_emb, parms["dropout_keep_rate"])
deep_emb = tf.layers.dense(deep_emb,units=1,activation=None)
return deep_emb
这里涉及的构建特征 build_feature() ,优化器 get_optimizer(),以及参数params参见ESMM多目标实践
def model_op(features,labels,mode,params):
if(labels != None):
labels = tf.cast(tf.reshape(labels, [-1,1]), dtype=tf.float32)
first_column, second_feature_columns = build_feature()
deep_emb = tf.feature_column.input_layer(features=features,feature_columns=second_feature_columns)
#wide
wide_logit = build_linear(features,first_column)
#deep
deep_logits = build_deep_layers(deep_emb,params,mode)
logits = deep_logits + wide_logit
out = tf.nn.sigmoid(logits)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'score':out,
'userid': features["userid"],
'itemid': features["itemid"]
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels,logits=logits)
loss = tf.reduce_mean(loss)
auc = tf.metrics.auc(labels, out)
pred_ctr = tf.metrics.mean(out)
true_ctr = tf.metrics.mean(labels)
eval_metric_ops = {
"eval_auc": auc,
"pred_ctr":pred_ctr,
"true_ctr":true_ctr
}
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops=eval_metric_ops)
assert mode == tf.estimator.ModeKeys.TRAIN
if params["is_decy"] == True:
learn_rate = tf.train.exponential_decay(params['learning_rate'], tf.train.get_global_step(),
decay_steps=params["decay_steps"], decay_rate=params["decay_rate"])
else:
learn_rate = params['learning_rate']
optimizer = get_optimizer(params['optimizer'], learning_rate=learn_rate)
lr_optimizer = tf.train.FtrlOptimizer(learning_rate=0.1)
nn_train = optimizer.minimize(loss, global_step=tf.train.get_global_step(),var_list=ops.get_collection(
ops.GraphKeys.TRAINABLE_VARIABLES,
scope='Deep'))
lr_train = lr_optimizer.minimize(loss, global_step=tf.train.get_global_step(),var_list=ops.get_collection(
ops.GraphKeys.TRAINABLE_VARIABLES,
scope='Linear'))
updata_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
return tf.estimator.EstimatorSpec(
mode=tf.estimator.ModeKeys.TRAIN,
predictions=out,
loss=loss,
eval_metric_ops=eval_metric_ops,
train_op=tf.group([nn_train,lr_train,updata_ops])
)