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

Fix attribute name in new API (#10947)

上级 c79ec9f0
...@@ -48,7 +48,7 @@ def linear(): ...@@ -48,7 +48,7 @@ def linear():
return avg_loss return avg_loss
def train(use_cuda, train_program, save_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()
trainer = fluid.Trainer( trainer = fluid.Trainer(
...@@ -68,8 +68,8 @@ def train(use_cuda, train_program, save_dirname): ...@@ -68,8 +68,8 @@ def train(use_cuda, train_program, save_dirname):
['15.343549569447836'] ['15.343549569447836']
... ...
''' '''
if save_dirname is not None: if params_dirname is not None:
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
trainer.train( trainer.train(
...@@ -80,13 +80,13 @@ def train(use_cuda, train_program, save_dirname): ...@@ -80,13 +80,13 @@ def train(use_cuda, train_program, save_dirname):
# infer # infer
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
if save_dirname is None: if params_dirname is None:
return return
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
batch_size = 10 batch_size = 10
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
...@@ -100,10 +100,10 @@ def main(use_cuda): ...@@ -100,10 +100,10 @@ def main(use_cuda):
return return
# Directory for saving the trained model # Directory for saving the trained model
save_dirname = "fit_a_line.inference.model" params_dirname = "fit_a_line.inference.model"
train(use_cuda, linear, save_dirname) train(use_cuda, linear, params_dirname)
infer(use_cuda, inference_program, save_dirname) infer(use_cuda, inference_program, params_dirname)
class TestFitALine(unittest.TestCase): class TestFitALine(unittest.TestCase):
......
...@@ -85,7 +85,7 @@ def train_network(): ...@@ -85,7 +85,7 @@ def train_network():
return [avg_cost, accuracy] return [avg_cost, accuracy]
def train(use_cuda, train_program, save_dirname): def train(use_cuda, train_program, params_dirname):
BATCH_SIZE = 128 BATCH_SIZE = 128
EPOCH_NUM = 1 EPOCH_NUM = 1
...@@ -105,8 +105,8 @@ def train(use_cuda, train_program, save_dirname): ...@@ -105,8 +105,8 @@ def train(use_cuda, train_program, save_dirname):
print('Loss {0:2.2}, Acc {1:2.2}'.format(avg_cost, accuracy)) print('Loss {0:2.2}, Acc {1:2.2}'.format(avg_cost, accuracy))
if accuracy > 0.01: # Low threshold for speeding up CI if accuracy > 0.01: # Low threshold for speeding up CI
if save_dirname is not None: if params_dirname is not None:
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
return return
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
...@@ -122,10 +122,10 @@ def train(use_cuda, train_program, save_dirname): ...@@ -122,10 +122,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['pixel', 'label']) feed_order=['pixel', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
# The input's dimension of conv should be 4-D or 5-D. # The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range # Use normilized image pixels as input data, which should be in the range
...@@ -142,12 +142,14 @@ def main(use_cuda): ...@@ -142,12 +142,14 @@ def main(use_cuda):
save_path = "image_classification_resnet.inference.model" save_path = "image_classification_resnet.inference.model"
train( train(
use_cuda=use_cuda, train_program=train_network, save_dirname=save_path) use_cuda=use_cuda,
train_program=train_network,
params_dirname=save_path)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=inference_network, inference_program=inference_network,
save_dirname=save_path) params_dirname=save_path)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -64,7 +64,7 @@ def train_network(): ...@@ -64,7 +64,7 @@ def train_network():
return [avg_cost, accuracy] return [avg_cost, accuracy]
def train(use_cuda, train_program, save_dirname): def train(use_cuda, train_program, params_dirname):
BATCH_SIZE = 128 BATCH_SIZE = 128
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -82,8 +82,8 @@ def train(use_cuda, train_program, save_dirname): ...@@ -82,8 +82,8 @@ def train(use_cuda, train_program, save_dirname):
print('Loss {0:2.2}, Acc {1:2.2}'.format(avg_cost, accuracy)) print('Loss {0:2.2}, Acc {1:2.2}'.format(avg_cost, accuracy))
if accuracy > 0.01: # Low threshold for speeding up CI if accuracy > 0.01: # Low threshold for speeding up CI
if save_dirname is not None: if params_dirname is not None:
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
return return
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
...@@ -99,10 +99,10 @@ def train(use_cuda, train_program, save_dirname): ...@@ -99,10 +99,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['pixel', 'label']) feed_order=['pixel', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
# The input's dimension of conv should be 4-D or 5-D. # The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range # Use normilized image pixels as input data, which should be in the range
...@@ -119,12 +119,14 @@ def main(use_cuda): ...@@ -119,12 +119,14 @@ def main(use_cuda):
save_path = "image_classification_vgg.inference.model" save_path = "image_classification_vgg.inference.model"
train( train(
use_cuda=use_cuda, train_program=train_network, save_dirname=save_path) use_cuda=use_cuda,
train_program=train_network,
params_dirname=save_path)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=inference_network, inference_program=inference_network,
save_dirname=save_path) params_dirname=save_path)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -141,7 +141,7 @@ def train_program(): ...@@ -141,7 +141,7 @@ def train_program():
return [avg_cost] return [avg_cost]
def train(use_cuda, train_program, save_path): 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.01) optimizer = fluid.optimizer.SGD(learning_rate=0.01)
...@@ -172,7 +172,7 @@ def train(use_cuda, train_program, save_path): ...@@ -172,7 +172,7 @@ def train(use_cuda, train_program, save_path):
print("avg_cost: %s" % avg_cost) print("avg_cost: %s" % avg_cost)
if float(avg_cost) < 100.0: # Large value to increase CI speed if float(avg_cost) < 100.0: # Large value to increase CI speed
trainer.save_params(save_path) trainer.save_params(params_dirname)
else: else:
print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1, print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
float(avg_cost))) float(avg_cost)))
...@@ -183,7 +183,7 @@ def train(use_cuda, train_program, save_path): ...@@ -183,7 +183,7 @@ def train(use_cuda, train_program, save_path):
print("Step {0}, Epoch {1} Metrics {2}".format( print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, map(np.array, event.metrics))) event.step, event.epoch, map(np.array, event.metrics)))
if event.step == 1: # Run 2 iterations to speed CI if event.step == 1: # Run 2 iterations to speed CI
trainer.save_params(save_path) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
train_reader = paddle.batch( train_reader = paddle.batch(
...@@ -197,10 +197,10 @@ def train(use_cuda, train_program, save_path): ...@@ -197,10 +197,10 @@ def train(use_cuda, train_program, save_path):
feed_order=feed_order) feed_order=feed_order)
def infer(use_cuda, inference_program, save_path): def infer(use_cuda, inference_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
inference_program, param_path=save_path, place=place) inference_program, param_path=params_dirname, place=place)
# Setup inputs by creating LoDTensors to represent sequences of words. # Setup inputs by creating LoDTensors to represent sequences of words.
# Here each word is the basic element of these LoDTensors and the shape of # Here each word is the basic element of these LoDTensors and the shape of
...@@ -251,9 +251,9 @@ def infer(use_cuda, inference_program, save_path): ...@@ -251,9 +251,9 @@ def infer(use_cuda, inference_program, save_path):
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "label_semantic_roles.inference.model" params_dirname = "label_semantic_roles.inference.model"
train(use_cuda, train_program, save_path) train(use_cuda, train_program, params_dirname)
infer(use_cuda, inference_program, save_path) infer(use_cuda, inference_program, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -57,7 +57,7 @@ def train_program(): ...@@ -57,7 +57,7 @@ def train_program():
return [avg_cost, acc] return [avg_cost, acc]
def train(use_cuda, train_program, save_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) optimizer = fluid.optimizer.Adam(learning_rate=0.001)
...@@ -78,7 +78,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -78,7 +78,7 @@ def train(use_cuda, train_program, save_dirname):
print("acc : %s" % acc) print("acc : %s" % acc)
if acc > 0.2: # Smaller value to increase CI speed if acc > 0.2: # Smaller value to increase CI speed
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
else: else:
print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
event.epoch + 1, avg_cost, acc)) event.epoch + 1, avg_cost, acc))
...@@ -100,11 +100,11 @@ def train(use_cuda, train_program, save_dirname): ...@@ -100,11 +100,11 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['img', 'label']) feed_order=['img', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
batch_size = 1 batch_size = 1
tensor_img = numpy.random.uniform(-1.0, 1.0, tensor_img = numpy.random.uniform(-1.0, 1.0,
...@@ -116,17 +116,17 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -116,17 +116,17 @@ def infer(use_cuda, inference_program, save_dirname=None):
def main(use_cuda): def main(use_cuda):
save_dirname = "recognize_digits_conv.inference.model" params_dirname = "recognize_digits_conv.inference.model"
# call train() with is_local argument to run distributed train # call train() with is_local argument to run distributed train
train( train(
use_cuda=use_cuda, use_cuda=use_cuda,
train_program=train_program, train_program=train_program,
save_dirname=save_dirname) params_dirname=params_dirname)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=inference_program, inference_program=inference_program,
save_dirname=save_dirname) params_dirname=params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -44,7 +44,7 @@ def train_program(): ...@@ -44,7 +44,7 @@ def train_program():
return [avg_cost, acc] return [avg_cost, acc]
def train(use_cuda, train_program, save_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) optimizer = fluid.optimizer.Adam(learning_rate=0.001)
...@@ -62,7 +62,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -62,7 +62,7 @@ def train(use_cuda, train_program, save_dirname):
print("acc : %s" % acc) print("acc : %s" % acc)
if acc > 0.2: # Smaller value to increase CI speed if acc > 0.2: # Smaller value to increase CI speed
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
else: else:
print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
event.epoch + 1, avg_cost, acc)) event.epoch + 1, avg_cost, acc))
...@@ -81,11 +81,11 @@ def train(use_cuda, train_program, save_dirname): ...@@ -81,11 +81,11 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['img', 'label']) feed_order=['img', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
batch_size = 1 batch_size = 1
tensor_img = numpy.random.uniform(-1.0, 1.0, tensor_img = numpy.random.uniform(-1.0, 1.0,
...@@ -97,17 +97,17 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -97,17 +97,17 @@ def infer(use_cuda, inference_program, save_dirname=None):
def main(use_cuda): def main(use_cuda):
save_dirname = "recognize_digits_mlp.inference.model" params_dirname = "recognize_digits_mlp.inference.model"
# call train() with is_local argument to run distributed train # call train() with is_local argument to run distributed train
train( train(
use_cuda=use_cuda, use_cuda=use_cuda,
train_program=train_program, train_program=train_program,
save_dirname=save_dirname) params_dirname=params_dirname)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=inference_program, inference_program=inference_program,
save_dirname=save_dirname) params_dirname=params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -155,7 +155,7 @@ def train_program(): ...@@ -155,7 +155,7 @@ def train_program():
return [avg_cost, scale_infer] return [avg_cost, scale_infer]
def train(use_cuda, train_program, save_path): 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) optimizer = fluid.optimizer.SGD(learning_rate=0.2)
...@@ -180,7 +180,7 @@ def train(use_cuda, train_program, save_path): ...@@ -180,7 +180,7 @@ def train(use_cuda, train_program, save_path):
print("avg_cost: %s" % avg_cost) print("avg_cost: %s" % avg_cost)
if float(avg_cost) < 4: # Smaller value to increase CI speed if float(avg_cost) < 4: # Smaller value to increase CI speed
trainer.save_params(save_path) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
else: else:
print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1, print('BatchID {0}, Test Loss {1:0.2}'.format(event.epoch + 1,
...@@ -200,10 +200,10 @@ def train(use_cuda, train_program, save_path): ...@@ -200,10 +200,10 @@ def train(use_cuda, train_program, save_path):
feed_order=feed_order) feed_order=feed_order)
def infer(use_cuda, inference_program, save_path): def infer(use_cuda, inference_program, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
inference_program, param_path=save_path, place=place) inference_program, param_path=params_dirname, place=place)
# Use the first data from paddle.dataset.movielens.test() as input. # Use the first data from paddle.dataset.movielens.test() as input.
# Use create_lod_tensor(data, lod, place) API to generate LoD Tensor, # Use create_lod_tensor(data, lod, place) API to generate LoD Tensor,
...@@ -240,12 +240,15 @@ def infer(use_cuda, inference_program, save_path): ...@@ -240,12 +240,15 @@ def infer(use_cuda, inference_program, save_path):
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "recommender_system.inference.model" params_dirname = "recommender_system.inference.model"
train(use_cuda=use_cuda, train_program=train_program, save_path=save_path) train(
use_cuda=use_cuda,
train_program=train_program,
params_dirname=params_dirname)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=inference_program, inference_program=inference_program,
save_path=save_path) params_dirname=params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -64,7 +64,7 @@ def train_program(word_dict): ...@@ -64,7 +64,7 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def train(use_cuda, train_program, save_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) optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
...@@ -85,7 +85,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -85,7 +85,7 @@ def train(use_cuda, train_program, save_dirname):
print("acc : %s" % acc) print("acc : %s" % acc)
if acc > 0.2: # Smaller value to increase CI speed if acc > 0.2: # Smaller value to increase CI speed
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
else: else:
...@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname):
print("Step {0}, Epoch {1} Metrics {2}".format( print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, map(np.array, event.metrics))) event.step, event.epoch, map(np.array, event.metrics)))
if event.step == 1: # Run 2 iterations to speed CI if event.step == 1: # Run 2 iterations to speed CI
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
train_reader = paddle.batch( train_reader = paddle.batch(
...@@ -112,13 +112,13 @@ def train(use_cuda, train_program, save_dirname): ...@@ -112,13 +112,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['words', 'label']) feed_order=['words', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=partial(inference_program, word_dict), infer_func=partial(inference_program, word_dict),
param_path=save_dirname, param_path=params_dirname,
place=place) place=place)
# Setup input by creating LoDTensor to represent sequence of words. # Setup input by creating LoDTensor to represent sequence of words.
...@@ -143,9 +143,9 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -143,9 +143,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
train(use_cuda, train_program, save_path) train(use_cuda, train_program, params_dirname)
infer(use_cuda, inference_program, save_path) infer(use_cuda, inference_program, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -79,7 +79,7 @@ def train_program(word_dict): ...@@ -79,7 +79,7 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def train(use_cuda, train_program, save_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) optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
...@@ -100,7 +100,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -100,7 +100,7 @@ def train(use_cuda, train_program, save_dirname):
print("acc : %s" % acc) print("acc : %s" % acc)
if acc > 0.2: # Smaller value to increase CI speed if acc > 0.2: # Smaller value to increase CI speed
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
else: else:
...@@ -112,7 +112,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -112,7 +112,7 @@ def train(use_cuda, train_program, save_dirname):
print("Step {0}, Epoch {1} Metrics {2}".format( print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, map(np.array, event.metrics))) event.step, event.epoch, map(np.array, event.metrics)))
if event.step == 1: # Run 2 iterations to speed CI if event.step == 1: # Run 2 iterations to speed CI
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
train_reader = paddle.batch( train_reader = paddle.batch(
...@@ -127,13 +127,13 @@ def train(use_cuda, train_program, save_dirname): ...@@ -127,13 +127,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['words', 'label']) feed_order=['words', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=partial(inference_program, word_dict), infer_func=partial(inference_program, word_dict),
param_path=save_dirname, param_path=params_dirname,
place=place) place=place)
# Setup input by creating LoDTensor to represent sequence of words. # Setup input by creating LoDTensor to represent sequence of words.
...@@ -158,9 +158,9 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -158,9 +158,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "understand_sentiment_conv.inference.model" params_dirname = "understand_sentiment_conv.inference.model"
train(use_cuda, train_program, save_path) train(use_cuda, train_program, params_dirname)
infer(use_cuda, inference_program, save_path) infer(use_cuda, inference_program, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -71,7 +71,7 @@ def train_program(word_dict): ...@@ -71,7 +71,7 @@ def train_program(word_dict):
return [avg_cost, accuracy] return [avg_cost, accuracy]
def train(use_cuda, train_program, save_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) optimizer = fluid.optimizer.Adagrad(learning_rate=0.002)
...@@ -92,7 +92,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -92,7 +92,7 @@ def train(use_cuda, train_program, save_dirname):
print("acc : %s" % acc) print("acc : %s" % acc)
if acc > 0.2: # Smaller value to increase CI speed if acc > 0.2: # Smaller value to increase CI speed
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
else: else:
...@@ -104,7 +104,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -104,7 +104,7 @@ def train(use_cuda, train_program, save_dirname):
print("Step {0}, Epoch {1} Metrics {2}".format( print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch, map(np.array, event.metrics))) event.step, event.epoch, map(np.array, event.metrics)))
if event.step == 1: # Run 2 iterations to speed CI if event.step == 1: # Run 2 iterations to speed CI
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
train_reader = paddle.batch( train_reader = paddle.batch(
...@@ -119,13 +119,13 @@ def train(use_cuda, train_program, save_dirname): ...@@ -119,13 +119,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['words', 'label']) feed_order=['words', 'label'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict() word_dict = paddle.dataset.imdb.word_dict()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=partial(inference_program, word_dict), infer_func=partial(inference_program, word_dict),
param_path=save_dirname, param_path=params_dirname,
place=place) place=place)
# Setup input by creating LoDTensor to represent sequence of words. # Setup input by creating LoDTensor to represent sequence of words.
...@@ -150,9 +150,9 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -150,9 +150,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def main(use_cuda): def main(use_cuda):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "understand_sentiment_stacked_lstm.inference.model" params_dirname = "understand_sentiment_stacked_lstm.inference.model"
train(use_cuda, train_program, save_path) train(use_cuda, train_program, params_dirname)
infer(use_cuda, inference_program, save_path) infer(use_cuda, inference_program, params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -80,7 +80,7 @@ def train_program(is_sparse): ...@@ -80,7 +80,7 @@ def train_program(is_sparse):
return avg_cost return avg_cost
def train(use_cuda, train_program, save_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)
test_reader = paddle.batch( test_reader = paddle.batch(
...@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname): ...@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname):
print("loss= ", avg_cost) print("loss= ", avg_cost)
if avg_cost < 10.0: if avg_cost < 10.0:
trainer.save_params(save_dirname) trainer.save_params(params_dirname)
trainer.stop() trainer.stop()
if math.isnan(avg_cost): if math.isnan(avg_cost):
...@@ -115,10 +115,10 @@ def train(use_cuda, train_program, save_dirname): ...@@ -115,10 +115,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order=['firstw', 'secondw', 'thirdw', 'forthw', 'nextw']) feed_order=['firstw', 'secondw', 'thirdw', 'forthw', 'nextw'])
def infer(use_cuda, inference_program, save_dirname=None): def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = fluid.Inferencer( inferencer = fluid.Inferencer(
infer_func=inference_program, param_path=save_dirname, place=place) infer_func=inference_program, param_path=params_dirname, place=place)
# Setup inputs by creating 4 LoDTensors representing 4 words. Here each word # Setup inputs by creating 4 LoDTensors representing 4 words. Here each word
# is simply an index to look up for the corresponding word vector and hence # is simply an index to look up for the corresponding word vector and hence
...@@ -153,17 +153,17 @@ def main(use_cuda, is_sparse): ...@@ -153,17 +153,17 @@ def main(use_cuda, is_sparse):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
save_path = "word2vec.inference.model" params_dirname = "word2vec.inference.model"
train( train(
use_cuda=use_cuda, use_cuda=use_cuda,
train_program=partial(train_program, is_sparse), train_program=partial(train_program, is_sparse),
save_dirname=save_path) params_dirname=params_dirname)
infer( infer(
use_cuda=use_cuda, use_cuda=use_cuda,
inference_program=partial(inference_program, is_sparse), inference_program=partial(inference_program, is_sparse),
save_dirname=save_path) params_dirname=params_dirname)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册