Skip to content

Commit 7792c63

Browse files
authored
Merge pull request #10695 from kolinwei/develop
benchmark/fluid script支持多卡训练
2 parents 32a15ed + 1ee9fea commit 7792c63

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

benchmark/fluid/mnist.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def run_benchmark(model, args):
159159
paddle.dataset.mnist.train(), batch_size=args.batch_size)
160160

161161
accuracy = fluid.metrics.Accuracy()
162+
train_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=avg_cost.name)
162163
iters, num_samples, start_time = 0, 0, time.time()
163164
for pass_id in range(args.pass_num):
164165
accuracy.reset()
@@ -175,17 +176,20 @@ def run_benchmark(model, args):
175176
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
176177
y_data = y_data.reshape([len(y_data), 1])
177178

178-
outs = exe.run(
179-
fluid.default_main_program(),
179+
outs = train_exe.run(
180180
feed={"pixel": img_data,
181181
"label": y_data},
182-
fetch_list=[avg_cost, batch_acc, batch_size_tensor]
182+
fetch_list=[
183+
avg_cost.name, batch_acc.name, batch_size_tensor.name
184+
]
183185
) # The accuracy is the accumulation of batches, but not the current batch.
184-
accuracy.update(value=outs[1], weight=outs[2])
186+
accuracy.update(
187+
value=np.array(np.mean(outs[1])),
188+
weight=np.mean(np.array(outs[2])))
185189
iters += 1
186190
num_samples += len(y_data)
187-
loss = np.array(outs[0])
188-
acc = np.array(outs[1])
191+
loss = np.mean(np.array(outs[0]))
192+
acc = np.mean(np.array(outs[1]))
189193
train_losses.append(loss)
190194
train_accs.append(acc)
191195
print("Pass: %d, Iter: %d, Loss: %f, Accuracy: %f" %

benchmark/fluid/resnet.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def test(exe):
241241
exe = fluid.Executor(place)
242242
exe.run(fluid.default_startup_program())
243243
accuracy = fluid.average.WeightedAverage()
244+
train_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=avg_cost.name)
244245
if args.use_fake_data:
245246
data = train_reader().next()
246247
image = np.array(map(lambda x: x[0].reshape(dshape), data)).astype(
@@ -264,14 +265,17 @@ def test(exe):
264265
data)).astype('float32')
265266
label = np.array(map(lambda x: x[1], data)).astype('int64')
266267
label = label.reshape([-1, 1])
267-
loss, acc, weight = exe.run(
268-
fluid.default_main_program(),
268+
loss, acc, weight = train_exe.run(
269269
feed={'data': image,
270270
'label': label},
271-
fetch_list=[avg_cost, batch_acc, batch_size_tensor])
271+
fetch_list=[
272+
avg_cost.name, batch_acc.name, batch_size_tensor.name
273+
])
272274
iters += 1
273275
num_samples += len(label)
274-
accuracy.add(value=acc, weight=weight)
276+
accuracy.add(value=np.array(np.mean(acc)), weight=np.mean(weight))
277+
loss = np.mean(np.array(loss))
278+
acc = np.mean(np.array(acc))
275279
train_losses.append(loss)
276280
train_accs.append(acc)
277281
print("Pass: %d, Iter: %d, Loss: %f, Accuracy: %f" %

benchmark/fluid/vgg.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def test(exe):
169169

170170
iters, num_samples, start_time = 0, 0, time.time()
171171
accuracy = fluid.average.WeightedAverage()
172+
train_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=avg_cost.name)
172173
for pass_id in range(args.pass_num):
173174
accuracy.reset()
174175
train_accs = []
@@ -184,14 +185,17 @@ def test(exe):
184185
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
185186
y_data = y_data.reshape([-1, 1])
186187

187-
loss, acc, weight = exe.run(
188-
fluid.default_main_program(),
188+
loss, acc, weight = train_exe.run(
189189
feed={"pixel": img_data,
190190
"label": y_data},
191-
fetch_list=[avg_cost, batch_acc, batch_size_tensor])
192-
accuracy.add(value=acc, weight=weight)
191+
fetch_list=[
192+
avg_cost.name, batch_acc.name, batch_size_tensor.name
193+
])
194+
accuracy.add(value=np.array(np.mean(acc)), weight=np.mean(weight))
193195
iters += 1
194196
num_samples += len(y_data)
197+
loss = np.mean(np.array(loss))
198+
acc = np.mean(np.array(acc))
195199
print(
196200
"Pass = %d, Iter = %d, Loss = %f, Accuracy = %f" %
197201
(pass_id, iters, loss, acc)

0 commit comments

Comments
 (0)