forked from huawei/mindspore2022
203 lines
7.4 KiB
Python
203 lines
7.4 KiB
Python
# 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.
|
||
# ============================================================================
|
||
""" test model train """
|
||
|
||
"""
|
||
导入MindSpore中的一些基本块和类
|
||
"""
|
||
import numpy as np
|
||
import mindspore.nn as nn
|
||
from mindspore import Tensor, Parameter, Model
|
||
from mindspore.common.initializer import initializer
|
||
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
|
||
from mindspore.nn.optim import Momentum
|
||
from mindspore.ops import operations as P
|
||
|
||
|
||
# fn is a funcation use i as input
|
||
def lr_gen(fn, epoch_size):
|
||
for i in range(epoch_size):
|
||
yield fn(i)
|
||
|
||
"""
|
||
函数参数:
|
||
net :代表要训练的神经网络模型
|
||
input_np :包含输入数据的NumPy数组
|
||
label_np :包含标签数据的NumPy数组
|
||
epoch_size :指定训练的轮数
|
||
函数流程:
|
||
定义损失函数 SoftmaxCrossEntropyWithLogits 作为交叉熵损失,并使用 sparse=True 表示标签数据为稀疏的,reduction="mean" 表示使用平均损失。
|
||
定义优化器 Momentum 并设置学习率为 lr_gen(lambda i: 0.1, epoch_size),使用动量算法进行模型参数优化。
|
||
创建一个模型 Model 实例。
|
||
使用 WithLossCell 将网络模型 net 和损失函数 loss 关联,并使用 TrainOneStepCell 创建一个用于单步训练的网络实例 _train_net,其中使用了之前定义的损失函数和优化器。
|
||
将 _train_net 设置为训练模式。对标签数据进行处理,将其从 one-hot 编码转换为单热编码。
|
||
通过循环迭代 epoch_size 次进行模型训练,每个迭代周期调用 _train_net 对输入数据进行单步训练。
|
||
“”“
|
||
def me_train_tensor(net, input_np, label_np, epoch_size=2):
|
||
"""me_train_tensor"""
|
||
loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
|
||
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr_gen(lambda i: 0.1, epoch_size), 0.9,
|
||
0.01, 1024)
|
||
Model(net, loss, opt)
|
||
_network = nn.WithLossCell(net, loss)
|
||
_train_net = nn.TrainOneStepCell(_network, opt)
|
||
_train_net.set_train()
|
||
label_np = np.argmax(label_np, axis=-1).astype(np.int32)
|
||
for epoch in range(0, epoch_size):
|
||
print(f"epoch %d" % (epoch))
|
||
_train_net(Tensor(input_np), Tensor(label_np))
|
||
|
||
"""
|
||
测试用例函数;用于测试MindSpore框架中的P.BiasAdd操作和相关逻辑的正确性。
|
||
函数中的Net类定义了一个简单的神经网络,用于测试加法操作的正确性。
|
||
"""
|
||
def test_bias_add(test_with_simu):
|
||
"""test_bias_add"""
|
||
import mindspore.context as context
|
||
is_pynative_mode = (context.get_context("mode") == context.PYNATIVE_MODE)
|
||
# training api is implemented under Graph mode
|
||
if is_pynative_mode:
|
||
context.set_context(mode=context.GRAPH_MODE)
|
||
if test_with_simu:
|
||
return
|
||
|
||
class Net(nn.Cell):
|
||
"""Net definition"""
|
||
|
||
def __init__(self,
|
||
output_channels,
|
||
bias_init='zeros',
|
||
):
|
||
super(Net, self).__init__()
|
||
self.biasAdd = P.BiasAdd()
|
||
|
||
if isinstance(bias_init, Tensor):
|
||
if bias_init.ndim != 1 or bias_init.shape[0] != output_channels:
|
||
raise ValueError("bias_init shape error")
|
||
|
||
self.bias = Parameter(initializer(
|
||
bias_init, [output_channels]), name="bias")
|
||
|
||
def construct(self, input_x):
|
||
return self.biasAdd(input_x, self.bias)
|
||
|
||
bias_init = Tensor(np.ones([3]).astype(np.float32))
|
||
input_np = np.ones([1, 3, 3, 3], np.float32)
|
||
label_np = np.ones([1, 3, 3, 3], np.int32) * 2
|
||
me_train_tensor(Net(3, bias_init=bias_init), input_np, label_np)
|
||
|
||
|
||
def test_conv(test_with_simu):
|
||
"""test_conv"""
|
||
import mindspore.context as context
|
||
is_pynative_mode = (context.get_context("mode") == context.PYNATIVE_MODE)
|
||
# training api is implemented under Graph mode
|
||
if is_pynative_mode:
|
||
context.set_context(mode=context.GRAPH_MODE)
|
||
if test_with_simu:
|
||
return
|
||
|
||
class Net(nn.Cell):
|
||
"Net definition"""
|
||
|
||
def __init__(self,
|
||
cin,
|
||
cout,
|
||
kernel_size):
|
||
super(Net, self).__init__()
|
||
Tensor(np.ones([6, 3, 3, 3]).astype(np.float32) * 0.01)
|
||
self.conv = nn.Conv2d(cin,
|
||
cout,
|
||
kernel_size)
|
||
|
||
def construct(self, input_x):
|
||
return self.conv(input_x)
|
||
|
||
net = Net(3, 6, (3, 3))
|
||
input_np = np.ones([1, 3, 32, 32]).astype(np.float32) * 0.01
|
||
label_np = np.ones([1, 6, 32, 32]).astype(np.int32)
|
||
me_train_tensor(net, input_np, label_np)
|
||
|
||
|
||
def test_net():
|
||
"""test_net"""
|
||
import mindspore.context as context
|
||
is_pynative_mode = (context.get_context("mode") == context.PYNATIVE_MODE)
|
||
# training api is implemented under Graph mode
|
||
if is_pynative_mode:
|
||
context.set_context(mode=context.GRAPH_MODE)
|
||
|
||
class Net(nn.Cell):
|
||
"""Net definition"""
|
||
|
||
def __init__(self):
|
||
super(Net, self).__init__()
|
||
Tensor(np.ones([64, 3, 7, 7]).astype(np.float32) * 0.01)
|
||
self.conv = nn.Conv2d(3, 64, (7, 7), pad_mode="same", stride=2)
|
||
self.relu = nn.ReLU()
|
||
self.bn = nn.BatchNorm2d(64)
|
||
self.mean = P.ReduceMean(keep_dims=True)
|
||
self.flatten = nn.Flatten()
|
||
self.dense = nn.Dense(64, 12)
|
||
|
||
def construct(self, input_x):
|
||
output = input_x
|
||
output = self.conv(output)
|
||
output = self.bn(output)
|
||
output = self.relu(output)
|
||
output = self.mean(output, (-2, -1))
|
||
output = self.flatten(output)
|
||
output = self.dense(output)
|
||
return output
|
||
|
||
"""
|
||
使用了定义的Net类和me_train_tensor函数
|
||
对神经网络进行了简单的训练
|
||
"""
|
||
net = Net()
|
||
input_np = np.ones([32, 3, 224, 224]).astype(np.float32) * 0.01
|
||
label_np = np.ones([32, 12]).astype(np.int32)
|
||
me_train_tensor(net, input_np, label_np)
|
||
|
||
|
||
def test_bn():
|
||
"""test_bn"""
|
||
import mindspore.context as context
|
||
is_pynative_mode = (context.get_context("mode") == context.PYNATIVE_MODE)
|
||
# training api is implemented under Graph mode
|
||
if is_pynative_mode:
|
||
context.set_context(mode=context.GRAPH_MODE)
|
||
|
||
class Net(nn.Cell):
|
||
"""Net definition"""
|
||
|
||
def __init__(self, cin, cout):
|
||
super(Net, self).__init__()
|
||
self.bn = nn.BatchNorm2d(cin)
|
||
self.flatten = nn.Flatten()
|
||
self.dense = nn.Dense(cin, cout)
|
||
|
||
def construct(self, input_x):
|
||
output = input_x
|
||
output = self.bn(output)
|
||
output = self.flatten(output)
|
||
output = self.dense(output)
|
||
return output
|
||
|
||
net = Net(2048, 16)
|
||
input_np = np.ones([32, 2048, 1, 1]).astype(np.float32) * 0.01
|
||
label_np = np.ones([32, 16]).astype(np.int32)
|
||
me_train_tensor(net, input_np, label_np)
|