mindspore/tests/train_step_wrap.py

105 lines
3.1 KiB
Python
Raw Normal View History

# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""
train step wrap
"""
import mindspore.nn as nn
2020-05-13 11:30:27 +08:00
from mindspore import ParameterTuple
from mindspore.ops import composite as C
2020-05-13 11:30:27 +08:00
class TrainStepWrap(nn.Cell):
"""
TrainStepWrap definition
"""
2020-05-13 11:30:27 +08:00
def __init__(self, network):
super(TrainStepWrap, self).__init__()
self.network = network
self.network.set_train()
self.weights = ParameterTuple(network.trainable_params())
self.optimizer = nn.Momentum(self.weights, 0.1, 0.9)
self.hyper_map = C.HyperMap()
2020-08-25 20:16:08 +08:00
self.grad = C.GradOperation(get_by_list=True)
def construct(self, x, label):
weights = self.weights
grads = self.grad(self.network, weights)(x, label)
return self.optimizer(grads)
2020-05-13 11:30:27 +08:00
class NetWithLossClass(nn.Cell):
"""
NetWithLossClass definition
"""
2020-05-13 11:30:27 +08:00
def __init__(self, network):
super(NetWithLossClass, self).__init__(auto_prefix=False)
self.loss = nn.SoftmaxCrossEntropyWithLogits()
self.network = network
def construct(self, x, label):
predict = self.network(x)
return self.loss(predict, label)
def train_step_with_loss_warp(network):
return TrainStepWrap(NetWithLossClass(network))
class TrainStepWrap2(nn.Cell):
"""
TrainStepWrap2 definition
"""
2020-05-13 11:30:27 +08:00
def __init__(self, network, sens):
super(TrainStepWrap2, self).__init__()
self.network = network
self.network.set_train()
self.weights = ParameterTuple(network.get_parameters())
self.optimizer = nn.Momentum(self.weights, 0.1, 0.9)
self.hyper_map = C.HyperMap()
2020-08-25 20:16:08 +08:00
self.grad = C.GradOperation(get_by_list=True, sens_param=True)
self.sens = sens
def construct(self, x):
weights = self.weights
grads = self.grad(self.network, weights)(x, self.sens)
return self.optimizer(grads)
2020-05-13 11:30:27 +08:00
def train_step_with_sens(network, sens):
return TrainStepWrap2(network, sens)
2020-05-13 11:30:27 +08:00
class TrainStepWrapWithoutOpt(nn.Cell):
"""
TrainStepWrapWithoutOpt definition
"""
2020-05-13 11:30:27 +08:00
def __init__(self, network):
super(TrainStepWrapWithoutOpt, self).__init__()
self.network = network
self.weights = ParameterTuple(network.trainable_params())
2020-08-25 20:16:08 +08:00
self.grad = C.GradOperation(get_by_list=True)
def construct(self, x, label):
grads = self.grad(self.network, self.weights)(x, label)
return grads
2020-05-13 11:30:27 +08:00
def train_step_without_opt(network):
return TrainStepWrapWithoutOpt(NetWithLossClass(network))