使用paddle fluid训练RankNet模型,训练速度很慢,一直不能收敛
Created by: aturbofly
使用fluid Executor 方式训练RankNet模型。 模型训练很久。loss在很缓慢的下降。但训练好几个小时了,loss也还在0.0X。 数据量才6w多。
代码: #!/usr/bin/python
coding: gbk
from future import print_function import os import logging import paddle import paddle.fluid as fluid import numpy as np from dataset import popin_samples import math import gzip import sys
try: from paddle.fluid.contrib.trainer import * from paddle.fluid.contrib.inferencer import * except ImportError: print( "In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib", file=sys.stderr) from paddle.fluid.trainer import * from paddle.fluid.inferencer import *
logger = logging.getLogger("paddle") logger.setLevel(logging.INFO)
BATCH_SIZE = 30
def half_ranknet(name_prefix, input_dim): """ parameter in same name will be shared in paddle framework, these parameters in ranknet can be used in shared state, e.g. left network and right network shared parameters in detail https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/api.md """ # data layer data = fluid.layers.data(name_prefix + "_data",shape = [input_dim], dtype='float32')
# hidden layer
hd1 = fluid.layers.fc(input=data,
name=name_prefix + "_hidden",
size=10,
act='tanh',
param_attr=None)
# fully connected layer and output layer
output = fluid.layers.fc(input=hd1,
name=name_prefix + "_score",
size=1,
act=None,#paddle.activation.Linear(),
param_attr=None)
return data, output
def train(use_cuda, save_dirname, is_local): """
:param use_cuda:
:param save_dirname:
:param is_local:
:return:
"""
label = fluid.layers.data("label", shape=[1])
input_dim = 801
left_data, output_left = half_ranknet("left", input_dim)
right_data, output_right = half_ranknet("right", input_dim)
loss = fluid.layers.rank_loss(
# https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/layers/nn.py
name="cost", left=output_left, right=output_right, label=label)
#test
avg_cost = fluid.layers.mean(loss)
sgd_optimizer = fluid.optimizer.SGD(learning_rate = 0.0001)
sgd_optimizer.minimize(avg_cost)
train_reader = paddle.batch(paddle.reader.shuffle(popin_samples.train, buf_size=100),
batch_size=BATCH_SIZE)
test_reader = paddle.batch(
paddle.reader.shuffle(popin_samples.test, buf_size=100),
batch_size=BATCH_SIZE
)
#feed_order = ["label", "left_data", "right_data"]
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
feeder = fluid.DataFeeder(feed_list = [label, left_data, right_data], place=place)
def train_loop(main_program):
exe.run(fluid.default_startup_program())
PASS_NUM = 100
for pass_id in range(PASS_NUM):
for data in train_reader():
avg_loss_value = exe.run(main_program,
feed=feeder.feed(data),
fetch_list=[avg_cost])
print(avg_loss_value)
if avg_loss_value[0] < 0.001:
if save_dirname is not None:
fluid.io.save_inference_model(save_dirname,["right_data"],[output_right], exe)#["left_data", "right_data"],[output_left, output_right], exe)
return
if math.isnan(float(avg_loss_value[0])):
sys.exit("got NaN loss, training failed.")
if is_local:
train_loop(fluid.default_main_program())
print("train is over! ")
def main(use_cuda, is_local=True): if use_cuda and not fluid.core.is_compiled_with_cuda(): return
# Directory for saving the trained model
save_dirname = "ranknet.inference.model.v3"
train(use_cuda, save_dirname, is_local)
if name == 'main': main(False)