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# ============================================================================ 15""" test momentum """ 16import numpy as np 17 18import mindspore.nn as nn 19from mindspore import Tensor, Parameter 20from mindspore.common.api import _cell_graph_executor 21from mindspore.nn import TrainOneStepCell, WithLossCell 22from mindspore.nn.optim import Momentum 23from mindspore.ops import operations as P 24 25 26class Net(nn.Cell): 27 """ Net definition """ 28 29 def __init__(self): 30 super(Net, self).__init__() 31 self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight") 32 self.bias = Parameter(Tensor(np.ones([10]).astype(np.float32)), name="bias") 33 self.matmul = P.MatMul() 34 self.biasAdd = P.BiasAdd() 35 36 def construct(self, x): 37 x = self.biasAdd(self.matmul(x, self.weight), self.bias) 38 return x 39 40 41def test_momentum_compile(): 42 """ test_momentum_compile """ 43 inputs = Tensor(np.ones([1, 64]).astype(np.float32)) 44 label = Tensor(np.zeros([1, 10]).astype(np.float32)) 45 net = Net() 46 net.set_train() 47 48 loss = nn.SoftmaxCrossEntropyWithLogits(sparse=False) 49 optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9) 50 51 net_with_loss = WithLossCell(net, loss) 52 train_network = TrainOneStepCell(net_with_loss, optimizer) 53 _cell_graph_executor.compile(train_network, inputs, label) 54