MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / train_resnet

Function train_resnet

python/paddle/distributed/launch/plugins/test.py:57–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55
56
57def train_resnet():
58 fleet.init(is_collective=True)
59
60 resnet = ResNet(BottleneckBlock, 18, num_classes=class_dim)
61 optimizer = optimizer_setting(parameter_list=resnet.parameters())
62 optimizer = fleet.distributed_optimizer(optimizer)
63 resnet = fleet.distributed_model(resnet)
64
65 dataset = RandomDataset(batch_num * batch_size)
66 train_loader = DataLoader(
67 dataset,
68 batch_size=batch_size,
69 shuffle=True,
70 drop_last=True,
71 num_workers=2,
72 )
73
74 print("Distributed training start...")
75 for eop in range(epoch):
76 resnet.train()
77
78 for batch_id, data in enumerate(train_loader()):
79 img, label = data
80 label.stop_gradient = True
81
82 out = resnet(img)
83 loss = paddle.nn.functional.cross_entropy(input=out, label=label)
84 avg_loss = paddle.mean(x=loss)
85 acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
86 acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
87
88 avg_loss.backward()
89 optimizer.step()
90 resnet.clear_gradients()
91
92 print(
93 f"[Epoch {eop}, batch {batch_id}] loss: {avg_loss:.5f}, acc1: {acc_top1:.5f}, acc5: {acc_top5:.5f}"
94 )
95
96 print("Distributed training completed")
97
98
99if __name__ == '__main__':

Callers 1

test.pyFile · 0.85

Calls 14

ResNetClass · 0.90
DataLoaderClass · 0.90
rangeFunction · 0.85
optimizer_settingFunction · 0.70
RandomDatasetClass · 0.70
printFunction · 0.50
initMethod · 0.45
parametersMethod · 0.45
distributed_optimizerMethod · 0.45
trainMethod · 0.45
meanMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected