forked from PaddlePaddle/PaddleSlim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_deep_mutual_learning.py
executable file
·87 lines (74 loc) · 3 KB
/
test_deep_mutual_learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
sys.path.append("../")
import unittest
import logging
import numpy as np
import paddle
from static_case import StaticCase
from paddleslim.dist import DML
from paddleslim.common import get_logger
logger = get_logger(__name__, level=logging.INFO)
class Model(paddle.nn.Layer):
def __init__(self):
super(Model, self).__init__()
self.conv = paddle.nn.Conv2D(
in_channels=1, out_channels=256, kernel_size=3, stride=1, padding=1)
self.pool2d_avg = paddle.nn.AdaptiveAvgPool2D([1, 1])
self.out = paddle.nn.Linear(256, 10)
def forward(self, inputs):
inputs = paddle.reshape(inputs, shape=[0, 1, 28, 28])
y = self.conv(inputs)
y = self.pool2d_avg(y)
y = paddle.reshape(y, shape=[-1, 256])
y = self.out(y)
return y
class TestDML(unittest.TestCase):
def test_dml(self):
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
) else paddle.CPUPlace()
def transform(x):
return np.reshape(x, [1, 28, 28])
train_dataset = paddle.vision.datasets.MNIST(
mode='train', backend='cv2', transform=transform)
train_loader = paddle.io.DataLoader(
train_dataset, places=place, drop_last=True, batch_size=64)
models = [Model(), Model()]
optimizers = []
for cur_model in models:
opt = paddle.optimizer.Momentum(
0.1, 0.9, parameters=cur_model.parameters())
optimizers.append(opt)
dml_model = DML(models)
dml_optimizer = dml_model.opt(optimizers)
def train(train_loader, dml_model, dml_optimizer):
dml_model.train()
for step_id, (images, labels) in enumerate(train_loader):
images, labels = paddle.to_tensor(images), paddle.to_tensor(
labels)
labels = paddle.reshape(labels, [0, 1])
logits = dml_model.forward(images)
precs = [
paddle.metric.accuracy(
input=l, label=labels, k=1).numpy() for l in logits
]
losses = dml_model.loss(logits, labels)
dml_optimizer.minimize(losses)
if step_id % 10 == 0:
print(step_id, precs)
for epoch_id in range(10):
train(train_loader, dml_model, dml_optimizer)
if __name__ == '__main__':
unittest.main()