コード例 #1
0
opt = optimizer.minimize(avg_cost)
# 创建一个执行器,CPU训练速度比较慢
place = fluid.CPUPlace()
# 请用GPU进行计算,CPU运算结果不佳。
# place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)

# 进行参数初始化
exe.run(fluid.default_startup_program())

# 获取训练和预测数据
train_reader = paddle.batch(
    reader=text_reader.train_reader(root_path + 'train_list_end.txt'),
    batch_size=BATCH_SIZE)
test_reader = paddle.batch(
    reader=text_reader.test_reader(root_path + 'valid_list_end.txt'),
    batch_size=BATCH_SIZE)

# 定义输入数据的维度
feeder = fluid.DataFeeder(place=place, feed_list=[words, label])

# 开始训练
# 修改模型迭代次数
for pass_id in range(10):
    # 进行训练
    for batch_id, data in enumerate(train_reader()):
        train_cost, train_acc = exe.run(program=fluid.default_main_program(),
                                        feed=feeder.feed(data),
                                        fetch_list=[avg_cost, acc])

        if batch_id % 40 == 0:
コード例 #2
0
# 定义优化方法
optimizer = fluid.optimizer.AdagradOptimizer(learning_rate=0.002)
opt = optimizer.minimize(avg_cost)

# 创建一个执行器,CPU训练速度比较慢
# place = fluid.CPUPlace()
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
# 进行参数初始化
exe.run(fluid.default_startup_program())

# 获取训练和预测数据
train_reader = paddle.batch(
    reader=text_reader.train_reader('datasets/train_list.txt'), batch_size=128)
test_reader = paddle.batch(
    reader=text_reader.test_reader('datasets/test_list.txt'), batch_size=128)

# 定义输入数据的维度
feeder = fluid.DataFeeder(place=place, feed_list=[words, label])

# 开始训练
for pass_id in range(10):
    # 进行训练
    for batch_id, data in enumerate(train_reader()):
        train_cost, train_acc = exe.run(program=fluid.default_main_program(),
                                        feed=feeder.feed(data),
                                        fetch_list=[avg_cost, acc])

        if batch_id % 40 == 0:
            print('Pass:%d, Batch:%d, Cost:%0.5f, Acc:%0.5f' %
                  (pass_id, batch_id, train_cost[0], train_acc[0]))
コード例 #3
0
# optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001,
#                          regularization=fluid.regularizer.L2DecayRegularizer(
#                          regularization_coeff=0.01))
opt = optimizer.minimize(avg_cost)
# 创建一个执行器,CPU训练速度比较慢
# place = fluid.CPUPlace()
# 请用GPU进行计算,CPU运算结果不佳。
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)

# 进行参数初始化
exe.run(fluid.default_startup_program())

# 获取训练和预测数据   
train_reader = paddle.batch(reader=text_reader.train_reader(root_path +'train_list_end.txt'), batch_size=BATCH_SIZE)
test_reader = paddle.batch(reader=text_reader.test_reader(root_path + 'valid_list_end.txt'), batch_size=BATCH_SIZE)


# 定义输入数据的维度
feeder = fluid.DataFeeder(place=place, feed_list=[words, label])

# 开始训练
# 修改模型迭代次数
for pass_id in range(10):
    # 进行训练
    for batch_id, data in enumerate(train_reader()):
        train_cost, train_acc = exe.run(program=fluid.default_main_program(),
                             feed=feeder.feed(data),
                             fetch_list=[avg_cost, acc])
        
        if batch_id % 40 == 0: