未验证 提交 b877a86f 编写于 作者: O overlordmax 提交者: GitHub

Mmoe sb 04211336 (#4552)

* fix bugs

* fix bugs
上级 3d37039c
......@@ -121,19 +121,19 @@ python train_mmoe.py --use_gpu 0 \ #使用cpu训练
## 预测
本模型训练和预测交替进行,运行train_mmoe.py 即可得到预测结果
本模型训练和预测交替进行,训练的同时可得到预测结果
## 模型效果
epoch设置为100的训练和测试效果如下:
```text
batch_size:[32],feature_size:[499],expert_num:[8],gate_num[2],expert_size[16],tower_size[8],epochs:[100]
2020-04-16 11:28:06,- INFO - epoch_id: 0,epoch_time: 129.17434 s,loss: 0.62215,train_auc_income: 0.86302,train_auc_marital: 0.92316,test_auc_income: 0.84525,test_auc_marital: 0.98269
2020-04-16 11:30:36,- INFO - epoch_id: 1,epoch_time: 149.79017 s,loss: 0.42484,train_auc_income: 0.90634,train_auc_marital: 0.98418,test_auc_income:
2020-04-21 12:39:08,-INFO: batch_size:32,feature_size:499,expert_num:8,gate_num:2,expert_size:16,tower_size:8,epochs:2
2020-04-16 11:28:06,-INFO: epoch_id: 0,epoch_time: 129.17434 s,loss: 0.62215,train_auc_income: 0.86302,train_auc_marital: 0.92316,test_auc_income: 0.84525,test_auc_marital: 0.98269
2020-04-16 11:30:36,-INFO: epoch_id: 1,epoch_time: 149.79017 s,loss: 0.42484,train_auc_income: 0.90634,train_auc_marital: 0.98418,test_auc_income:
......
2020-04-16 15:31:23,- INFO - epoch_id: 97,epoch_time: 147.07304 s,loss: 0.30267,train_auc_income: 0.94743,train_auc_marital: 0.99430,test_auc_income: 0.94905,test_auc_marital: 0.99414
2020-04-16 15:33:51,- INFO - epoch_id: 98,epoch_time: 148.34412 s,loss: 0.29688,train_auc_income: 0.94736,train_auc_marital: 0.99433,test_auc_income: 0.94846,test_auc_marital: 0.99409
2020-04-16 15:36:21,- INFO - epoch_id: 99,epoch_time: 149.91047 s,loss: 0.31330,train_auc_income: 0.94732,train_auc_marital: 0.99403,test_auc_income: 0.94881,test_auc_marital: 0.99386
2020-04-16 15:36:21,- INFO - mean_mmoe_test_auc_income: 0.94465,mean_mmoe_test_auc_marital 0.99324,max_mmoe_test_auc_income: 0.94937,max_mmoe_test_auc_marital 0.99419
2020-04-16 15:31:23,-INFO: epoch_id: 97,epoch_time: 147.07304 s,loss: 0.30267,train_auc_income: 0.94743,train_auc_marital: 0.99430,test_auc_income: 0.94905,test_auc_marital: 0.99414
2020-04-16 15:33:51,-INFO: epoch_id: 98,epoch_time: 148.34412 s,loss: 0.29688,train_auc_income: 0.94736,train_auc_marital: 0.99433,test_auc_income: 0.94846,test_auc_marital: 0.99409
2020-04-16 15:36:21,-INFO: epoch_id: 99,epoch_time: 149.91047 s,loss: 0.31330,train_auc_income: 0.94732,train_auc_marital: 0.99403,test_auc_income: 0.94881,test_auc_marital: 0.99386
2020-04-16 15:36:21,-INFO: mean_mmoe_test_auc_income: 0.94465,mean_mmoe_test_auc_marital 0.99324,max_mmoe_test_auc_income: 0.94937,max_mmoe_test_auc_marital 0.99419
```
......@@ -27,7 +27,7 @@ def parse_args():
parser.add_argument("--tower_size", type=int, default=8, help="tower_size")
parser.add_argument("--expert_num", type=int, default=8, help="expert_num")
parser.add_argument("--gate_num", type=int, default=2, help="gate_num")
parser.add_argument("--epochs", type=int, default=400, help="epochs")
parser.add_argument("--epochs", type=int, default=100, help="epochs")
parser.add_argument("--batch_size", type=int, default=32, help="batch_size")
parser.add_argument('--use_gpu', type=int, default=0, help='whether using gpu')
parser.add_argument('--model_dir',type=str, default='model_dir', help="model_dir")
......@@ -38,10 +38,9 @@ def parse_args():
def data_preparation_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--train_path", type=str, default='', help="train_path")
parser.add_argument("--test_path", type=str, default='', help="test_path")
parser.add_argument('--train_data_path',type=str, default='', help="train_data_path")
parser.add_argument('--test_data_path',type=str, default='', help="test_data_path")
parser.add_argument("--train_path", type=str, default='data/census-income.data', help="train_path")
parser.add_argument("--test_path", type=str, default='data/census-income.test', help="test_path")
parser.add_argument('--train_data_path',type=str, default='train_data/', help="train_data_path")
parser.add_argument('--test_data_path',type=str, default='test_data/', help="test_data_path")
args = parser.parse_args()
return args
......@@ -7,6 +7,11 @@ import datetime
import os
import utils
from args import *
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)
def set_zero(var_name,scope=fluid.global_scope(),place=fluid.CPUPlace(),param_type="int64"):
"""
......@@ -62,16 +67,16 @@ def MMOE(feature_size=499,expert_num=8, gate_num=2, expert_size=16, tower_size=8
output_layers.append(out)
cost_income = paddle.fluid.layers.cross_entropy(input=output_layers[0], label=label_income,soft_label = True)
cost_marital = paddle.fluid.layers.cross_entropy(input=output_layers[1], label=label_marital,soft_label = True)
pred_income = fluid.layers.clip(output_layers[0], min=1e-15, max=1.0 - 1e-15)
pred_marital = fluid.layers.clip(output_layers[1], min=1e-15, max=1.0 - 1e-15)
cost_income = paddle.fluid.layers.cross_entropy(input=pred_income, label=label_income,soft_label = True)
cost_marital = paddle.fluid.layers.cross_entropy(input=pred_marital, label=label_marital,soft_label = True)
label_income_1 = fluid.layers.slice(label_income, axes=[1], starts=[1], ends=[2])
label_marital_1 = fluid.layers.slice(label_marital, axes=[1], starts=[1], ends=[2])
pred_income = fluid.layers.clip(output_layers[0], min=1e-10, max=1.0 - 1e-10)
pred_marital = fluid.layers.clip(output_layers[1], min=1e-10, max=1.0 - 1e-10)
auc_income, batch_auc_1, auc_states_1 = fluid.layers.auc(input=pred_income, label=fluid.layers.cast(x=label_income_1, dtype='int64'))
auc_marital, batch_auc_2, auc_states_2 = fluid.layers.auc(input=pred_marital, label=fluid.layers.cast(x=label_marital_1, dtype='int64'))
......@@ -95,8 +100,8 @@ expert_num = args.expert_num
epochs = args.epochs
gate_num = args.gate_num
print("batch_size:[%d],feature_size:[%d],expert_num:[%d],gate_num[%d],expert_size[%d],tower_size[%d],epochs:[%d]"%(batch_size,feature_size,expert_num,
gate_num,expert_size,tower_size,epochs))
logger.info("batch_size:{} ,feature_size:{} ,expert_num:{} ,gate_num:{} ,expert_size:{} ,tower_size:{} ,epochs:{} ".format(
batch_size,feature_size,expert_num,gate_num,expert_size,tower_size,epochs))
train_reader = utils.prepare_reader(train_path,batch_size)
test_reader = utils.prepare_reader(test_path,batch_size)
......@@ -156,14 +161,12 @@ for epoch in range(epochs):
auc_income_list.append(test_auc_1_p)
auc_marital_list.append(test_auc_2_p)
end = time.time()
time_stamp = datetime.datetime.now()
print("%s,- INFO - epoch_id: %d,epoch_time: %.5f s,loss: %.5f,train_auc_income: %.5f,train_auc_marital: %.5f,test_auc_income: %.5f,test_auc_marital: %.5f"%
(time_stamp.strftime('%Y-%m-%d %H:%M:%S'),epoch,end - begin,loss_data,auc_1_p,auc_2_p,test_auc_1_p,test_auc_2_p))
time_stamp = datetime.datetime.now()
print("%s,- INFO - mean_mmoe_test_auc_income: %.5f,mean_mmoe_test_auc_marital %.5f,max_mmoe_test_auc_income: %.5f,max_mmoe_test_auc_marital %.5f"%(
time_stamp.strftime('%Y-%m-%d %H:%M:%S'),np.mean(auc_income_list),np.mean(auc_marital_list),np.max(auc_income_list),np.max(auc_marital_list)))
logger.info("epoch_id:{},epoch_time:{} s,loss:{},train_auc_income:{},train_auc_marital:{},test_auc_income:{},test_auc_marital:{}".format(
epoch,end - begin,loss_data,auc_1_p,auc_2_p,test_auc_1_p,test_auc_2_p))
logger.info("mean_sb_test_auc_income:{},mean_sb_test_auc_marital:{},max_sb_test_auc_income:{},max_sb_test_auc_marital:{}".format(
np.mean(auc_income_list),np.mean(auc_marital_list),np.max(auc_income_list),np.max(auc_marital_list)))
......
......@@ -119,14 +119,14 @@ python share_bottom.py --use_gpu 0\ #使用cpu训练
## 预测
本模型训练和预测交替进行,运行share_bottom.py即可得到预测结果
本模型训练和预测交替进行,训练的同时可得到预测结果。
## 模型效果
epoch设置为100的训练和测试效果如下:
```text
batch_size:[32],epochs:[100],feature_size:[499],bottom_size:[117],tower_nums:[2],tower_size:[8]
2020-04-16 16:01:04,-INFO: batch_size:[32],epochs:[100],feature_size:[499],bottom_size:[117],tower_nums:[2],tower_size:[8]
2020-04-16 16:01:04,- INFO - epoch_id: 0,epoch_time: 77.17624 s,loss: 0.62643,train_auc_income: 0.49442,train_auc_marital: 0.93509,test_auc_income: 0.50000,test_auc_marital: 0.93920
2020-04-16 16:02:23,- INFO - epoch_id: 1,epoch_time: 78.84795 s,loss: 0.47955,train_auc_income: 0.49721,train_auc_marital: 0.98118,test_auc_income: 0.50000,test_auc_marital: 0.98804
2020-04-16 16:03:43,- INFO - epoch_id: 2,epoch_time: 79.67485 s,loss:
......
......@@ -26,7 +26,7 @@ def parse_args():
parser.add_argument("--bottom_size", type=int, default=117, help="bottom_size")
parser.add_argument("--tower_nums", type=int, default=2, help="tower_nums")
parser.add_argument("--tower_size", type=int, default=8, help="tower_size")
parser.add_argument("--epochs", type=int, default=400, help="epochs")
parser.add_argument("--epochs", type=int, default=100, help="epochs")
parser.add_argument("--batch_size", type=int, default=32, help="batch_size")
parser.add_argument('--use_gpu', type=int, default=0, help='whether using gpu')
parser.add_argument('--train_data_path',type=str,default='train_data',help="train_data_path")
......@@ -38,9 +38,9 @@ def parse_args():
def data_preparation_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--train_path", type=str, default='', help="train_path")
parser.add_argument("--test_path", type=str, default='', help="test_path")
parser.add_argument('--train_data_path', type=str, default='train_data', help="train_data_path")
parser.add_argument('--test_data_path', type=str, default='test_data', help="test_data_path")
parser.add_argument("--train_path", type=str, default='data/census-income.data', help="train_path")
parser.add_argument("--test_path", type=str, default='data/census-income.test', help="test_path")
parser.add_argument('--train_data_path', type=str, default='train_data/', help="train_data_path")
parser.add_argument('--test_data_path', type=str, default='test_data/', help="test_data_path")
args = parser.parse_args()
return args
......@@ -5,8 +5,13 @@ import os
import time
import datetime
import utils
import logging
from args import *
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)
def set_zero(var_name,scope=fluid.global_scope(),place=fluid.CPUPlace(),param_type="int64"):
"""
Set tensor of a Variable to zero.
......@@ -49,25 +54,25 @@ def share_bottom(feature_size=499,bottom_size=117,tower_nums=2,tower_size=8):
name='output_layer_' + str(index))
output_layers.append(output_layer)
cost_income = paddle.fluid.layers.cross_entropy(input=output_layers[0], label=label_income,soft_label = True)
cost_marital = paddle.fluid.layers.cross_entropy(input=output_layers[1], label=label_marital,soft_label = True)
pred_income = fluid.layers.clip(output_layers[0], min=1e-15, max=1.0 - 1e-15)
pred_marital = fluid.layers.clip(output_layers[1], min=1e-15, max=1.0 - 1e-15)
cost_income = paddle.fluid.layers.cross_entropy(input=pred_income, label=label_income,soft_label = True)
cost_marital = paddle.fluid.layers.cross_entropy(input=pred_marital, label=label_marital,soft_label = True)
label_income_1 = fluid.layers.slice(label_income, axes=[1], starts=[1], ends=[2])
label_marital_1 = fluid.layers.slice(label_marital, axes=[1], starts=[1], ends=[2])
pred_income = fluid.layers.clip(output_layers[0], min=1e-10, max=1.0 - 1e-10)
pred_marital = fluid.layers.clip(output_layers[1], min=1e-10, max=1.0 - 1e-10)
auc_income, batch_auc_1, auc_states_1 = fluid.layers.auc(input=pred_income, label=fluid.layers.cast(x=label_income_1, dtype='int64'))
auc_marital, batch_auc_2, auc_states_2 = fluid.layers.auc(input=pred_marital, label=fluid.layers.cast(x=label_marital_1, dtype='int64'))
cost = fluid.layers.elementwise_add(cost_income, cost_marital, axis=1)
avg_cost_income = fluid.layers.mean(x=cost_income)
avg_cost_marital = fluid.layers.mean(x=cost_marital)
cost = avg_cost_income + avg_cost_marital
avg_cost = fluid.layers.mean(x=cost)
return [a_data,label_income,label_marital],cost,output_layers[0],output_layers[1],label_income,label_marital,auc_income,auc_marital,auc_states_1,auc_states_2
return [a_data,label_income,label_marital],avg_cost,output_layers[0],output_layers[1],label_income,label_marital,auc_income,auc_marital,auc_states_1,auc_states_2
......@@ -81,7 +86,8 @@ tower_nums = args.tower_nums
tower_size = args.tower_size
epochs = args.epochs
print("batch_size:[%d],epochs:[%d],feature_size:[%d],bottom_size:[%d],tower_nums:[%d],tower_size:[%d]"%(batch_size,epochs,feature_size,bottom_size,tower_nums,tower_size))
logger.info("batch_size:{} ,epochs:{} ,feature_size:{} ,bottom_size:{} ,tower_nums:{} ,tower_size:{} ".format(
batch_size,epochs,feature_size,bottom_size,tower_nums,tower_size))
train_reader = utils.prepare_reader(train_path,batch_size)
test_reader = utils.prepare_reader(test_path,batch_size)
......@@ -142,14 +148,12 @@ for epoch in range(epochs):
auc_income_list.append(test_auc_1_p)
auc_marital_list.append(test_auc_2_p)
end = time.time()
time_stamp = datetime.datetime.now()
print("%s,- INFO - epoch_id: %d,epoch_time: %.5f s,loss: %.5f,train_auc_income: %.5f,train_auc_marital: %.5f,test_auc_income: %.5f,test_auc_marital: %.5f"%
(time_stamp.strftime('%Y-%m-%d %H:%M:%S'),epoch,end - begin,loss_data,auc_1_p,auc_2_p,test_auc_1_p,test_auc_2_p))
time_stamp = datetime.datetime.now()
print("%s,- INFO - mean_sb_test_auc_income: %.5f,mean_sb_test_auc_marital %.5f,max_sb_test_auc_income: %.5f,max_sb_test_auc_marital %.5f"%(
time_stamp.strftime('%Y-%m-%d %H:%M:%S'),np.mean(auc_income_list),np.mean(auc_marital_list),np.max(auc_income_list),np.max(auc_marital_list)))
logger.info("epoch_id:{},epoch_time:{} s,loss:{},train_auc_income:{},train_auc_marital:{},test_auc_income:{},test_auc_marital:{}".format(
epoch,end - begin,loss_data,auc_1_p,auc_2_p,test_auc_1_p,test_auc_2_p))
logger.info("mean_sb_test_auc_income:{},mean_sb_test_auc_marital:{},max_sb_test_auc_income:{},max_sb_test_auc_marital:{}".format(
np.mean(auc_income_list),np.mean(auc_marital_list),np.max(auc_income_list),np.max(auc_marital_list)))
......
......@@ -2,8 +2,6 @@ CUDA_VISIBLE_DEVICES=0 python share_bottom.py --use_gpu 1 \
--epochs 100 \
--train_data_path 'train_data' \
--test_data_path 'test_data' \
--train_data_path '.train_data' \
--test_data_path '.test_data' \
--model_dir 'model_dir' \
--batch_size 32 \
--feature_size 499 \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册