未验证 提交 99a5a3d8 编写于 作者: S Siddharth Goyal 提交者: GitHub

Fix optimizer in remaining chapters with high level API (#11172)

上级 c9067bef
...@@ -7,10 +7,10 @@ foreach(src ${TEST_OPS}) ...@@ -7,10 +7,10 @@ foreach(src ${TEST_OPS})
endforeach() endforeach()
add_subdirectory(fit_a_line) add_subdirectory(fit_a_line)
#add_subdirectory(recognize_digits) add_subdirectory(recognize_digits)
#add_subdirectory(image_classification) add_subdirectory(image_classification)
#add_subdirectory(understand_sentiment) add_subdirectory(understand_sentiment)
add_subdirectory(label_semantic_roles) add_subdirectory(label_semantic_roles)
#add_subdirectory(word2vec) add_subdirectory(word2vec)
#add_subdirectory(recommender_system) add_subdirectory(recommender_system)
#add_subdirectory(machine_translation) add_subdirectory(machine_translation)
...@@ -85,6 +85,10 @@ def train_network(): ...@@ -85,6 +85,10 @@ def train_network():
return [avg_cost, accuracy] return [avg_cost, accuracy]
def optimizer_func():
return fluid.optimizer.Adam(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
BATCH_SIZE = 128 BATCH_SIZE = 128
EPOCH_NUM = 1 EPOCH_NUM = 1
...@@ -111,9 +115,7 @@ def train(use_cuda, train_program, params_dirname): ...@@ -111,9 +115,7 @@ def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, train_func=train_program, optimizer_func=optimizer_func, place=place)
optimizer=fluid.optimizer.Adam(learning_rate=0.001),
place=place)
trainer.train( trainer.train(
reader=train_reader, reader=train_reader,
......
...@@ -64,6 +64,10 @@ def train_network(): ...@@ -64,6 +64,10 @@ def train_network():
return [avg_cost, accuracy] return [avg_cost, accuracy]
def optimizer_func():
return fluid.optimizer.Adam(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
BATCH_SIZE = 128 BATCH_SIZE = 128
train_reader = paddle.batch( train_reader = paddle.batch(
...@@ -88,9 +92,7 @@ def train(use_cuda, train_program, params_dirname): ...@@ -88,9 +92,7 @@ def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, train_func=train_program, place=place, optimizer_func=optimizer_func)
place=place,
optimizer=fluid.optimizer.Adam(learning_rate=0.001))
trainer.train( trainer.train(
reader=train_reader, reader=train_reader,
......
...@@ -158,6 +158,13 @@ def train_program(is_sparse): ...@@ -158,6 +158,13 @@ def train_program(is_sparse):
return avg_cost return avg_cost
def optimizer_func():
return fluid.optimizer.Adagrad(
learning_rate=1e-4,
regularization=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.1))
def train(use_cuda, is_sparse, is_local=True): def train(use_cuda, is_sparse, is_local=True):
EPOCH_NUM = 1 EPOCH_NUM = 1
...@@ -182,11 +189,8 @@ def train(use_cuda, is_sparse, is_local=True): ...@@ -182,11 +189,8 @@ def train(use_cuda, is_sparse, is_local=True):
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=partial(train_program, is_sparse), train_func=partial(train_program, is_sparse),
optimizer=fluid.optimizer.Adagrad( place=place,
learning_rate=1e-4, optimizer_func=optimizer_func)
regularization=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.1)),
place=place)
trainer.train( trainer.train(
reader=train_reader, reader=train_reader,
......
...@@ -57,14 +57,17 @@ def train_program(): ...@@ -57,14 +57,17 @@ def train_program():
return [avg_cost, acc] return [avg_cost, acc]
def optimizer_func():
return fluid.optimizer.Adam(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, train_func=train_program,
place=place, place=place,
optimizer=optimizer, optimizer_func=optimizer_func,
parallel=True) parallel=True)
def event_handler(event): def event_handler(event):
......
...@@ -44,12 +44,15 @@ def train_program(): ...@@ -44,12 +44,15 @@ def train_program():
return [avg_cost, acc] return [avg_cost, acc]
def optimizer_func():
return fluid.optimizer.Adam(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, place=place, optimizer=optimizer) train_func=train_program, place=place, optimizer_func=optimizer_func)
def event_handler(event): def event_handler(event):
if isinstance(event, fluid.EndEpochEvent): if isinstance(event, fluid.EndEpochEvent):
......
...@@ -155,12 +155,15 @@ def train_program(): ...@@ -155,12 +155,15 @@ def train_program():
return [avg_cost, scale_infer] return [avg_cost, scale_infer]
def optimizer_func():
return fluid.optimizer.SGD(learning_rate=0.2)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.SGD(learning_rate=0.2)
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, place=place, optimizer=optimizer) train_func=train_program, place=place, optimizer_func=optimizer_func)
feed_order = [ feed_order = [
'user_id', 'gender_id', 'age_id', 'job_id', 'movie_id', 'category_id', 'user_id', 'gender_id', 'age_id', 'job_id', 'movie_id', 'category_id',
......
...@@ -64,15 +64,18 @@ def train_program(word_dict): ...@@ -64,15 +64,18 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=partial(train_program, word_dict), train_func=partial(train_program, word_dict),
place=place, place=place,
optimizer=optimizer) optimizer_func=optimizer_func)
def event_handler(event): def event_handler(event):
if isinstance(event, fluid.EndEpochEvent): if isinstance(event, fluid.EndEpochEvent):
......
...@@ -79,15 +79,18 @@ def train_program(word_dict): ...@@ -79,15 +79,18 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=partial(train_program, word_dict), train_func=partial(train_program, word_dict),
place=place, place=place,
optimizer=optimizer) optimizer_func=optimizer_func)
def event_handler(event): def event_handler(event):
if isinstance(event, fluid.EndEpochEvent): if isinstance(event, fluid.EndEpochEvent):
......
...@@ -71,15 +71,18 @@ def train_program(word_dict): ...@@ -71,15 +71,18 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def optimizer_func():
return fluid.optimizer.Adagrad(learning_rate=0.002)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=partial(train_program, word_dict), train_func=partial(train_program, word_dict),
place=place, place=place,
optimizer=optimizer) optimizer_func=optimizer_func)
def event_handler(event): def event_handler(event):
if isinstance(event, fluid.EndEpochEvent): if isinstance(event, fluid.EndEpochEvent):
......
...@@ -80,6 +80,10 @@ def train_program(is_sparse): ...@@ -80,6 +80,10 @@ def train_program(is_sparse):
return avg_cost return avg_cost
def optimizer_func():
return fluid.optimizer.SGD(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, train_program, params_dirname):
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE) paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE)
...@@ -104,9 +108,7 @@ def train(use_cuda, train_program, params_dirname): ...@@ -104,9 +108,7 @@ def train(use_cuda, train_program, params_dirname):
sys.exit("got NaN loss, training failed.") sys.exit("got NaN loss, training failed.")
trainer = fluid.Trainer( trainer = fluid.Trainer(
train_func=train_program, train_func=train_program, optimizer_func=optimizer_func, place=place)
optimizer=fluid.optimizer.SGD(learning_rate=0.001),
place=place)
trainer.train( trainer.train(
reader=train_reader, reader=train_reader,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册