Paddle
Paddle copied to clipboard
关于训练速度
https://github.com/PaddlePaddle/book/tree/develop/05.recommender_system
-
trainer_count = 20 和 trainer_count = 1 训练速度差别不大,但是看资源占用是起了多个线程的。
paddle.init(use_gpu=False, trainer_count=20)I0714 20:27:13.848081 2546738 Util.cpp:166] commandline: --use_gpu=False --trainer_count=20 Pass 0 Batch 0 Cost 1.08 {} 7.2955160141s Pass 0 Batch 10 Cost 0.83 {} 39.6152331829s Pass 0 Batch 20 Cost 0.80 {} 37.5780589581sI0714 20:20:40.590940 2544095 Util.cpp:166] commandline: --use_gpu=False --trainer_count=1 Pass 0 Batch 0 Cost 1.06 {} 4.85946583748s Pass 0 Batch 100 Cost 0.82 {} 377.929177999s -
发现当user的数量变大成几百万的时候,速度比几千个时速度慢了100倍,请问,是不是emb layer计算时搜索引起的?
谢谢
- 对第一个问题:当batch size不足够大,单纯的增加线程数未必会加快速度
- 对第二个问题:速度评测需要严格控制变量,能详细贴一下您的实验设置吗
- 开始以为batch_size是每个trainer的,这里batch_size过大会不会影响实验结果
- 第二个我再做实验对比一下 非常感谢
train.py
import paddle.v2 as paddle
import cPickle
import copy
import reader
import time
import gzip
def get_usr_combined_features():
uid = paddle.layer.data(
name='user_id',
type=paddle.data_type.integer_value(
len(reader.get_user_id_dict())
))
usr_emb = paddle.layer.embedding(input=uid, size=64)
usr_fc = paddle.layer.fc(input=usr_emb, size=64)
usr_combined_features = paddle.layer.fc(
input=[usr_fc],
size=100,
act=paddle.activation.Tanh())
return usr_combined_features
def get_video_combined_features():
video_id = paddle.layer.data(
name='video_id',
type=paddle.data_type.integer_value(
len(reader.get_video_id_dict())
))
video_emb = paddle.layer.embedding(input=video_id, size=32)
video_fc = paddle.layer.fc(input=video_emb, size=32)
video_categories = paddle.layer.data(
name='category_id',
type=paddle.data_type.sparse_binary_vector(
len(reader.get_video_categories_dict())))
video_categories_hidden = paddle.layer.fc(input=video_categories, size=32)
video_tags = paddle.layer.data(
name='tag_id',
type=paddle.data_type.sparse_binary_vector(
len(reader.get_video_tags_dict())))
video_tags_hidden = paddle.layer.fc(input=video_tags, size=32)
video_title_id = paddle.layer.data(
name='video_title',
type=paddle.data_type.integer_value_sequence(len(reader.get_video_title_dict())))
video_title_emb = paddle.layer.embedding(input=video_title_id, size=32)
video_title_conv = paddle.networks.sequence_conv_pool(
input=video_title_emb, hidden_size=32, context_len=3)
video_image = paddle.layer.data(
name='video_image',
type=paddle.data_type.dense_vector(256))
video_combined_features = paddle.layer.fc(
input=[video_fc, video_categories_hidden, video_tags_hidden, video_title_conv, video_image],
size=100,
act=paddle.activation.Tanh())
return video_combined_features
t = None
step = 0
def main():
paddle.init(use_gpu=False)
print 'create user layres'
usr_combined_features = get_usr_combined_features()
print 'create video layres'
video_combined_features = get_video_combined_features()
print 'create cos_sim layre'
inference = paddle.layer.cos_sim(
a=usr_combined_features, b=video_combined_features, size=1)
cost = paddle.layer.mse_cost(
input=inference,
label=paddle.layer.data(
name='score', type=paddle.data_type.dense_vector(1)))
parameters = paddle.parameters.create(cost)
trainer = paddle.trainer.SGD(
cost=cost,
parameters=parameters,
update_equation=paddle.optimizer.Adam(learning_rate=1e-4))
feeding = {
'user_id': 0,
'video_id': 1,
'video_title': 2,
'category_id': 3,
'tag_id': 4,
'video_image': 5,
'score': 6
}
global t
t = time.time()
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 10 == 0:
global t
print "Pass %d Batch %d Cost %.2f %s %s" % (
event.pass_id, event.batch_id, event.cost, event.metrics, time.time()-t)
t = time.time()
global step
if step % 100 == 0: # every 1000 batches, record a test cost
result = trainer.test(
reader=paddle.batch(
reader.test(), batch_size=256),
feeding=feeding)
print "Step %d Test Cost %.2f" % \
(step, result.cost)
step += 1
if isinstance(event, paddle.event.EndPass):
# save parameters
print "save parameters params_pass_%d.tar.gz" % event.pass_id
with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f:
parameters.to_tar(f)
trainer.train(
reader=paddle.batch(reader.train(), batch_size=256),
event_handler=event_handler,
feeding=feeding,
num_passes=5)
user_id = '103276950'
movie_id = 'V_00yxzrtR'
user = reader.user_info()[user_id]
movie = reader.video_info()[movie_id]
feature = user.value() + movie.value()
infer_dict = copy.copy(feeding)
del infer_dict['score']
prediction = paddle.infer(
output_layer=inference,
parameters=parameters,
input=[feature],
feeding=infer_dict)
print prediction
if __name__ == '__main__':
main()
下面是user长度为98630和7353665以及video长度为88930和1454673的时间日志
user len: 98630
video len: 88930
tag len: 21345
cat len: 211
create video layres
create cos_sim layre
I0716 18:40:43.590524 3120875 GradientMachine.cpp:85] Initing parameters..
I0716 18:40:44.192438 3120875 GradientMachine.cpp:92] Init parameters done.
Pass 0 Batch 0 Cost 0.97 {} 0.114304065704
Step 0 Test Cost 0.93
Pass 0 Batch 10 Cost 0.85 {} 2.52500104904
Pass 0 Batch 20 Cost 0.86 {} 1.15553307533
Pass 0 Batch 30 Cost 0.83 {} 1.16259098053
Pass 0 Batch 40 Cost 0.81 {} 1.12988400459
Pass 0 Batch 50 Cost 0.85 {} 1.18395400047
Pass 0 Batch 60 Cost 0.82 {} 1.20668983459
Pass 0 Batch 70 Cost 0.84 {} 1.12499880791
Pass 0 Batch 80 Cost 0.78 {} 1.10768914223
Pass 0 Batch 90 Cost 0.81 {} 1.12954592705
Pass 0 Batch 100 Cost 0.80 {} 2.48943710327
user len: 7353665
video len: 88930
tag len: 21345
cat len: 211
create video layres
create cos_sim layre
I0716 18:43:51.610711 3121427 GradientMachine.cpp:85] Initing parameters..
I0716 18:44:15.637262 3121427 GradientMachine.cpp:92] Init parameters done.
Pass 0 Batch 0 Cost 1.00 {} 3.57946705818
Step 0 Test Cost 0.84
Pass 0 Batch 10 Cost 0.86 {} 36.8801269531
Pass 0 Batch 20 Cost 0.86 {} 35.3740968704
Pass 0 Batch 30 Cost 0.83 {} 35.6775920391
Pass 0 Batch 40 Cost 0.83 {} 36.8862230778
Pass 0 Batch 50 Cost 0.83 {} 37.359801054
Pass 0 Batch 60 Cost 0.81 {} 38.4442980289
Pass 0 Batch 70 Cost 0.84 {} 39.8621969223
Pass 0 Batch 80 Cost 0.78 {} 39.7067630291
Pass 0 Batch 90 Cost 0.81 {} 39.81448102
Pass 0 Batch 100 Cost 0.80 {} 38.5178740025
user len: 98630
video len: 1454673
tag len: 153422
cat len: 217
create video layres
create cos_sim layre
I0716 19:40:48.638840 3131897 GradientMachine.cpp:85] Initing parameters..
I0716 19:40:52.157831 3131897 GradientMachine.cpp:92] Init parameters done.
Pass 0 Batch 0 Cost 0.98 {} 0.542382001877
Step 0 Test Cost 0.93
Pass 0 Batch 10 Cost 0.82 {} 6.98406100273
Pass 0 Batch 20 Cost 0.79 {} 5.36508989334
Pass 0 Batch 30 Cost 0.77 {} 5.40783691406
Pass 0 Batch 40 Cost 0.73 {} 5.66075801849
Pass 0 Batch 50 Cost 0.77 {} 5.27257418633
Pass 0 Batch 60 Cost 0.77 {} 5.26645302773
Pass 0 Batch 70 Cost 0.74 {} 5.41987514496
Pass 0 Batch 80 Cost 0.74 {} 5.32466411591
Pass 0 Batch 90 Cost 0.82 {} 5.23611021042
Pass 0 Batch 100 Cost 0.72 {} 5.14394903183
user len: 7353665
video len: 1454673
tag len: 153422
cat len: 217
create video layres
create cos_sim layre
I0716 19:21:41.327428 3128183 GradientMachine.cpp:85] Initing parameters..
I0716 19:22:08.292177 3128183 GradientMachine.cpp:92] Init parameters done.
Pass 0 Batch 0 Cost 0.99 {} 3.87032198906
Step 0 Test Cost 0.82
Pass 0 Batch 10 Cost 0.81 {} 40.7035338879
Pass 0 Batch 20 Cost 0.77 {} 38.6431701183
Pass 0 Batch 30 Cost 0.75 {} 38.7131221294
Pass 0 Batch 40 Cost 0.73 {} 39.8511910439
Pass 0 Batch 50 Cost 0.77 {} 39.6950318813
Pass 0 Batch 60 Cost 0.77 {} 39.0339639187
Pass 0 Batch 70 Cost 0.73 {} 39.8153209686
Pass 0 Batch 80 Cost 0.74 {} 39.9841759205
Pass 0 Batch 90 Cost 0.83 {} 39.7987859249
Pass 0 Batch 100 Cost 0.73 {} 39.1545190811
Step 100 Test Cost 0.77
另外,如何在固定batch_size的情况下,提高训练速度? 谢谢
如果是单机多卡:可以固定batch_size,尝试一下使用几个卡速度最快;在数据读取时,可以尝试多线程版本(xmap_readers)
已经解答,因此关闭issue。如仍有问题,欢迎reopen