提交 f18375f8 编写于 作者: O overlordmax

fix code style

上级 d02f1ccc
......@@ -167,20 +167,23 @@ python infer.py --batch_size 40 \ #batch大小
在测试集的效果如下:
```
W0422 10:45:12.497740 1218 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0422 10:45:12.501889 1218 device_context.cc:245] device: 0, cuDNN Version: 7.3.
2020-04-22 10:45:13,804-INFO: batch_id: 0,batch_time: 0.00625s,acc: 0.72500,auc: 0.92790
2020-04-22 10:45:13,809-INFO: batch_id: 1,batch_time: 0.00468s,acc: 0.80000,auc: 0.92321
2020-04-22 10:45:13,814-INFO: batch_id: 2,batch_time: 0.00442s,acc: 0.82500,auc: 0.93003
2020-04-22 10:45:13,819-INFO: batch_id: 3,batch_time: 0.00434s,acc: 0.75000,auc: 0.94108
2020-04-22 10:45:13,824-INFO: batch_id: 4,batch_time: 0.00438s,acc: 0.67500,auc: 0.93013
2020-04-22 10:45:13,829-INFO: batch_id: 5,batch_time: 0.00438s,acc: 0.80000,auc: 0.92201
W0422 11:44:50.891095 1573 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0422 11:44:50.895593 1573 device_context.cc:245] device: 0, cuDNN Version: 7.3.
2020-04-22 11:44:52,236-INFO: batch_id: 0, batch_time: 0.00613s, acc: 0.72500, auc: 0.92790
2020-04-22 11:44:52,242-INFO: batch_id: 1, batch_time: 0.00467s, acc: 0.80000, auc: 0.93356
2020-04-22 11:44:52,247-INFO: batch_id: 2, batch_time: 0.00462s, acc: 0.82500, auc: 0.93372
2020-04-22 11:44:52,252-INFO: batch_id: 3, batch_time: 0.00445s, acc: 0.75000, auc: 0.94198
2020-04-22 11:44:52,257-INFO: batch_id: 4, batch_time: 0.00449s, acc: 0.67500, auc: 0.93222
2020-04-22 11:44:52,262-INFO: batch_id: 5, batch_time: 0.00444s, acc: 0.80000, auc: 0.92254
......
2020-04-22 10:45:15,914-INFO: batch_id: 403,batch_time: 0.00487s,acc: 0.80000,auc: 0.90454
2020-04-22 10:45:15,920-INFO: batch_id: 404,batch_time: 0.00505s,acc: 0.72500,auc: 0.90427
2020-04-22 10:45:15,925-INFO: batch_id: 405,batch_time: 0.00460s,acc: 0.77500,auc: 0.90405
2020-04-22 10:45:15,931-INFO: batch_id: 406,batch_time: 0.00517s,acc: 0.77500,auc: 0.90412
2020-04-22 10:45:15,936-INFO: batch_id: 407,batch_time: 0.00457s,acc: 0.00000,auc: 0.90415
2020-04-22 10:45:15,936-INFO: mean_acc:0.76195,mean_auc:0.90335
2020-04-22 11:44:54,439-INFO: batch_id: 400, batch_time: 0.00507s, acc: 0.80000, auc: 0.90650
2020-04-22 11:44:54,445-INFO: batch_id: 401, batch_time: 0.00512s, acc: 0.67500, auc: 0.90658
2020-04-22 11:44:54,452-INFO: batch_id: 402, batch_time: 0.00591s, acc: 0.72500, auc: 0.90638
2020-04-22 11:44:54,458-INFO: batch_id: 403, batch_time: 0.00518s, acc: 0.80000, auc: 0.90634
2020-04-22 11:44:54,464-INFO: batch_id: 404, batch_time: 0.00513s, acc: 0.72500, auc: 0.90619
2020-04-22 11:44:54,470-INFO: batch_id: 405, batch_time: 0.00497s, acc: 0.77500, auc: 0.90597
2020-04-22 11:44:54,476-INFO: batch_id: 406, batch_time: 0.00554s, acc: 0.77500, auc: 0.90606
2020-04-22 11:44:54,481-INFO: batch_id: 407, batch_time: 0.00471s, acc: 0.00000, auc: 0.90608
2020-04-22 11:44:54,481-INFO: mean_acc:0.76195, mean_auc:0.90577
```
......@@ -22,6 +22,7 @@ import sys
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--epochs", type=int, default=40, help="epochs")
parser.add_argument("--batch_size", type=int, default=40, help="batch_size")
......@@ -37,7 +38,5 @@ def parse_args():
parser.add_argument('--hidden3_units', type=int, default=25, help='hidden3_units')
args = parser.parse_args()
return args
return args
......@@ -6,7 +6,7 @@ test_path="data/adult.test"
train_data_path="train_data/train_data.csv"
test_data_path="test_data/test_data.csv"
pip install -r requirements.txt
pip install -r requirements.txt
wget -P data/ https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data
wget -P data/ https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test
......
......@@ -29,19 +29,19 @@ def run_infer(args,test_data_path):
wide_deep_model = wide_deep()
test_data_generator = utils.CriteoDataset()
test_reader = paddle.batch(test_data_generator.test(test_data_path),batch_size=args.batch_size)
test_reader = paddle.batch(test_data_generator.test(test_data_path), batch_size=args.batch_size)
inference_scope = fluid.Scope()
startup_program = fluid.framework.Program()
test_program = fluid.framework.Program()
cur_model_path = os.path.join(args.model_dir,'epoch_' + str(args.test_epoch), "checkpoint")
cur_model_path = os.path.join(args.model_dir, 'epoch_' + str(args.test_epoch), "checkpoint")
with fluid.scope_guard(inference_scope):
with fluid.framework.program_guard(test_program, startup_program):
inputs = wide_deep_model.input_data()
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
loss, acc, auc, batch_auc, auc_states = wide_deep_model.model(inputs,args.hidden1_units, args.hidden2_units, args.hidden3_units)
loss, acc, auc, batch_auc, auc_states = wide_deep_model.model(inputs, args.hidden1_units, args.hidden2_units, args.hidden3_units)
exe = fluid.Executor(place)
exe.run(startup_program)
......@@ -56,32 +56,20 @@ def run_infer(args,test_data_path):
for batch_id, data in enumerate(test_reader()):
begin = time.time()
acc_val,auc_val = exe.run(program=test_program,
feed=feeder.feed(data),
fetch_list=[acc.name,auc.name],
return_numpy=True
)
feed=feeder.feed(data),
fetch_list=[acc.name, auc.name],
return_numpy=True
)
mean_acc.append(np.array(acc_val)[0])
mean_auc.append(np.array(auc_val)[0])
end = time.time()
logger.info("batch_id: {},batch_time: {:.5f}s,acc: {:.5f},auc: {:.5f}".format(batch_id,end-begin,np.array(acc_val)[0],np.array(auc_val)[0]))
logger.info("mean_acc:{:.5f},mean_auc:{:.5f}".format(np.mean(mean_acc),np.mean(mean_auc)))
logger.info("batch_id: {}, batch_time: {:.5f}s, acc: {:.5f}, auc: {:.5f}".format(
batch_id, end-begin, np.array(acc_val)[0], np.array(auc_val)[0]))
logger.info("mean_acc:{:.5f}, mean_auc:{:.5f}".format(np.mean(mean_acc), np.mean(mean_auc)))
if __name__ == "__main__":
args = args.parse_args()
run_infer(args,args.test_data_path)
\ No newline at end of file
run_infer(args, args.test_data_path)
\ No newline at end of file
......@@ -6,5 +6,4 @@ CUDA_VISIBLE_DEVICES=0 python infer.py --batch_size 40 \
--hidden1_units 75 \
--hidden2_units 50 \
--hidden3_units 25
\ No newline at end of file
\ No newline at end of file
......@@ -4,30 +4,32 @@ import math
import paddle.fluid as fluid
class wide_deep(object):
def wide_part(self,data):
def wide_part(self, data):
out = fluid.layers.fc(input=data,
size=1,
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0 / math.sqrt(8)),
regularizer=fluid.regularizer.L2DecayRegularizer(regularization_coeff=1e-4)),
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0 / math.sqrt(data.shape[1])),
regularizer=fluid.regularizer.L2DecayRegularizer(regularization_coeff=1e-4)),
act=None,
name='wide')
return out
def fc(self,inputs,hidden_units,active,tag):
output = fluid.layers.fc(input=inputs,
def fc(self, data, hidden_units, active, tag):
output = fluid.layers.fc(input=data,
size=hidden_units,
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0 / math.sqrt(58))),
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0 / math.sqrt(data.shape[1]))),
act=active,
name=tag)
return output
def deep_part(self,inputs,hidden1_units, hidden2_units, hidden3_units):
def deep_part(self, data, hidden1_units, hidden2_units, hidden3_units):
l1 = self.fc(data, hidden1_units, 'relu', 'l1')
l2 = self.fc(l1, hidden2_units, 'relu', 'l2')
l3 = self.fc(l2, hidden3_units, 'relu', 'l3')
l1 = self.fc(inputs,hidden1_units,'relu','l1')
l2 = self.fc(l1,hidden2_units,'relu','l2')
l3 = self.fc(l2,hidden3_units,'relu','l3')
return l3
def input_data(self):
......@@ -38,10 +40,10 @@ class wide_deep(object):
return inputs
def model(self,inputs,hidden1_units, hidden2_units, hidden3_units):
def model(self, inputs, hidden1_units, hidden2_units, hidden3_units):
wide_output = self.wide_part(inputs[0])
deep_output = self.deep_part(inputs[1],hidden1_units, hidden2_units, hidden3_units)
deep_output = self.deep_part(inputs[1], hidden1_units, hidden2_units, hidden3_units)
wide_model = fluid.layers.fc(input=wide_output,
size=1,
......@@ -50,28 +52,19 @@ class wide_deep(object):
name='w_wide')
deep_model = fluid.layers.fc(input=deep_output,
size=1,
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0)),
act=None,
name='w_deep')
size=1,
param_attr=fluid.ParamAttr(initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=1.0)),
act=None,
name='w_deep')
prediction = fluid.layers.elementwise_add(wide_model, deep_model)
pred = fluid.layers.sigmoid(fluid.layers.clip(prediction, min=-15.0, max=15.0), name="prediction")
num_seqs = fluid.layers.create_tensor(dtype='int64')
acc = fluid.layers.accuracy(input=pred, label=fluid.layers.cast(x=inputs[2], dtype='int64'), total=num_seqs)
auc_val, batch_auc, auc_states = fluid.layers.auc(input=pred, label=fluid.layers.cast(x=inputs[2], dtype='int64'))
auc_val, batch_auc, auc_states = fluid.layers.auc(input=pred, label=fluid.layers.cast(x=inputs[2], dtype='int64'))
cost = fluid.layers.sigmoid_cross_entropy_with_logits(x=prediction,label=inputs[2])
cost = fluid.layers.sigmoid_cross_entropy_with_logits(x=prediction, label=inputs[2])
avg_cost = fluid.layers.mean(cost)
return avg_cost,acc,auc_val, batch_auc, auc_states
return avg_cost, acc,auc_val, batch_auc, auc_states
......@@ -12,13 +12,13 @@ logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)
def train(args,train_data_path):
def train(args, train_data_path):
wide_deep_model = wide_deep()
inputs = wide_deep_model.input_data()
train_data_generator = utils.CriteoDataset()
train_reader = paddle.batch(train_data_generator.train(train_data_path),batch_size=args.batch_size)
train_reader = paddle.batch(train_data_generator.train(train_data_path), batch_size=args.batch_size)
loss,acc,auc, batch_auc, auc_states = wide_deep_model.model(inputs,args.hidden1_units, args.hidden2_units, args.hidden3_units)
loss, acc, auc, batch_auc, auc_states = wide_deep_model.model(inputs, args.hidden1_units, args.hidden2_units, args.hidden3_units)
optimizer = fluid.optimizer.AdagradOptimizer(learning_rate=0.01)
optimizer.minimize(loss)
......@@ -30,24 +30,18 @@ def train(args,train_data_path):
for epoch in range(args.epochs):
for batch_id, data in enumerate(train_reader()):
begin = time.time()
loss_val,acc_val,auc_val = exe.run(program=fluid.default_main_program(),
loss_val, acc_val, auc_val = exe.run(program=fluid.default_main_program(),
feed=feeder.feed(data),
fetch_list=[loss.name,acc.name,auc.name],
fetch_list=[loss.name, acc.name, auc.name],
return_numpy=True)
end = time.time()
logger.info("epoch:{},batch_time:{:.5f}s,loss:{:.5f},acc:{:.5f},auc:{:.5f}".format(epoch,end-begin,np.array(loss_val)[0],np.array(acc_val)[0],np.array(auc_val)[0]))
logger.info("epoch:{}, batch_time:{:.5f}s, loss:{:.5f}, acc:{:.5f}, auc:{:.5f}".format(epoch, end-begin, np.array(loss_val)[0],
np.array(acc_val)[0], np.array(auc_val)[0]))
model_dir = os.path.join(args.model_dir,'epoch_' + str(epoch + 1), "checkpoint")
model_dir = os.path.join(args.model_dir, 'epoch_' + str(epoch + 1), "checkpoint")
main_program = fluid.default_main_program()
fluid.io.save(main_program,model_dir)
if __name__ == "__main__":
args = args.parse_args()
train(args,args.train_data_path)
train(args, args.train_data_path)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册