1# Copyright 2020 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15from collections import Counter 16import numpy as np 17 18import mindspore.nn as nn 19from mindspore import Tensor, Parameter 20from mindspore.common import dtype as mstype 21from mindspore.common.api import _cell_graph_executor 22from mindspore.nn import TrainOneStepCell, WithLossCell 23from mindspore.nn.optim import LARS, Momentum 24from mindspore.ops import operations as P 25 26 27def multisteplr(total_steps, milestone, base_lr=0.9, gamma=0.1, dtype=mstype.float32): 28 lr = [] 29 milestone = Counter(milestone) 30 31 for step in range(total_steps): 32 base_lr = base_lr * gamma ** milestone[step] 33 lr.append(base_lr) 34 return Tensor(np.array(lr), dtype) 35 36 37class Net(nn.Cell): 38 def __init__(self): 39 super(Net, self).__init__() 40 self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight") 41 self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias") 42 self.matmul = P.MatMul() 43 self.biasAdd = P.BiasAdd() 44 45 def construct(self, x): 46 x = self.biasAdd(self.matmul(x, self.weight), self.bias) 47 return x 48 49 50def test_lars_multi_step_lr(): 51 inputs = Tensor(np.ones([1, 64]).astype(np.float32)) 52 label = Tensor(np.zeros([1, 10]).astype(np.float32)) 53 net = Net() 54 net.set_train() 55 loss = nn.SoftmaxCrossEntropyWithLogits() 56 57 lr = multisteplr(10, [2, 6]) 58 SGD = Momentum(net.trainable_params(), lr, 0.9) 59 optimizer = LARS(SGD, epsilon=1e-08, coefficient=0.02, use_clip=True, 60 lars_filter=lambda x: 'bn' not in x.name) 61 62 net_with_loss = WithLossCell(net, loss) 63 train_network = TrainOneStepCell(net_with_loss, optimizer) 64 _cell_graph_executor.compile(train_network, inputs, label) 65 66 67def test_lars_float_lr(): 68 inputs = Tensor(np.ones([1, 64]).astype(np.float32)) 69 label = Tensor(np.zeros([1, 10]).astype(np.float32)) 70 net = Net() 71 net.set_train() 72 loss = nn.SoftmaxCrossEntropyWithLogits() 73 74 lr = 0.1 75 SGD = Momentum(net.trainable_params(), lr, 0.9) 76 optimizer = LARS(SGD, epsilon=1e-08, coefficient=0.02, 77 lars_filter=lambda x: 'bn' not in x.name) 78 79 net_with_loss = WithLossCell(net, loss) 80 train_network = TrainOneStepCell(net_with_loss, optimizer) 81 _cell_graph_executor.compile(train_network, inputs, label) 82