(续 2 )在深度计算框架MindSpore中如何对不持续的计算进行处理——对数据集进行一定epoch数量的训练后,进行其他工作处理,再返回来接着进行一定epoch数量的训练——单步计算
2023-09-11 14:19:19 时间
内容接前文:
https://www.cnblogs.com/devilmaycry812839668/p/14988686.html
https://www.cnblogs.com/devilmaycry812839668/p/14990021.html
前面是我们自己按照个人理解实现的单步计算,随着对这个计算框架MindSpore的深入了解我们了解到其实官方是提供了单步计算函数的。
具体函数:
from mindspore.nn import TrainOneStepCell, WithLossCell
根据官方资料:
根据官方提供的函数,给出如下代码:
import mindspore import numpy as np # 引入numpy科学计算库 import matplotlib.pyplot as plt # 引入绘图库 np.random.seed(123) # 随机数生成种子 import mindspore.nn as nn import mindspore.ops as ops from mindspore import Tensor from mindspore import ParameterTuple, Parameter from mindspore import dtype as mstype from mindspore import Model import mindspore.dataset as ds from mindspore.train.callback import ModelCheckpoint, CheckpointConfig from mindspore.train.callback import LossMonitor from mindspore.nn import TrainOneStepCell, WithLossCell class Net(nn.Cell): def __init__(self, input_dims, output_dims): super(Net, self).__init__() self.matmul = ops.MatMul() self.weight_1 = Parameter(Tensor(np.random.randn(input_dims, 128), dtype=mstype.float32), name='weight_1') self.bias_1 = Parameter(Tensor(np.zeros(128), dtype=mstype.float32), name='bias_1') self.weight_2 = Parameter(Tensor(np.random.randn(128, 64), dtype=mstype.float32), name='weight_2') self.bias_2 = Parameter(Tensor(np.zeros(64), dtype=mstype.float32), name='bias_2') self.weight_3 = Parameter(Tensor(np.random.randn(64, output_dims), dtype=mstype.float32), name='weight_3') self.bias_3 = Parameter(Tensor(np.zeros(output_dims), dtype=mstype.float32), name='bias_3') def construct(self, x): x1 = self.matmul(x, self.weight_1) + self.bias_1 x2 = self.matmul(x1, self.weight_2) + self.bias_2 x3 = self.matmul(x2, self.weight_3) + self.bias_3 return x3 def main(): net = Net(1, 1) # loss function loss = nn.MSELoss() # optimizer optim = nn.SGD(params=net.trainable_params(), learning_rate=0.000001) # make net model # model = Model(net, loss, optim, metrics={'loss': nn.Loss()}) net_with_criterion = WithLossCell(net, loss) train_network = TrainOneStepCell(net_with_criterion, optim) # 数据集 x, y = np.array([[0.1]], dtype=np.float32), np.array([[0.1]], dtype=np.float32) x = Tensor(x) y = Tensor(y) for i in range(20000*100): #print(i, '\t', '*' * 100) train_network.set_train() res = train_network(x, y) # right # False, False # False, True # True, True xxx # not right # True, False if __name__ == '__main__': """ 设置运行的背景context """ from mindspore import context # 为mindspore设置运行背景context #context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') context.set_context(mode=context.GRAPH_MODE, device_target='GPU') import time a = time.time() main() b = time.time() print(b-a)
运行时间:
1158.24s
1154.29s
1152.69s
=====================================================
前文我们给出的单步计算 model.train 的代码修改如下:
import mindspore import numpy as np # 引入numpy科学计算库 import matplotlib.pyplot as plt # 引入绘图库 np.random.seed(123) # 随机数生成种子 import mindspore.nn as nn import mindspore.ops as ops from mindspore import Tensor from mindspore import ParameterTuple, Parameter from mindspore import dtype as mstype from mindspore import Model import mindspore.dataset as ds from mindspore.train.callback import ModelCheckpoint, CheckpointConfig from mindspore.train.callback import LossMonitor class Net(nn.Cell): def __init__(self, input_dims, output_dims): super(Net, self).__init__() self.matmul = ops.MatMul() self.weight_1 = Parameter(Tensor(np.random.randn(input_dims, 128), dtype=mstype.float32), name='weight_1') self.bias_1 = Parameter(Tensor(np.zeros(128), dtype=mstype.float32), name='bias_1') self.weight_2 = Parameter(Tensor(np.random.randn(128, 64), dtype=mstype.float32), name='weight_2') self.bias_2 = Parameter(Tensor(np.zeros(64), dtype=mstype.float32), name='bias_2') self.weight_3 = Parameter(Tensor(np.random.randn(64, output_dims), dtype=mstype.float32), name='weight_3') self.bias_3 = Parameter(Tensor(np.zeros(output_dims), dtype=mstype.float32), name='bias_3') def construct(self, x): x1 = self.matmul(x, self.weight_1) + self.bias_1 x2 = self.matmul(x1, self.weight_2) + self.bias_2 x3 = self.matmul(x2, self.weight_3) + self.bias_3 return x3 def main(): net = Net(1, 1) # loss function loss = nn.MSELoss() # optimizer optim = nn.SGD(params=net.trainable_params(), learning_rate=0.000001) # make net model model = Model(net, loss, optim, metrics={'loss': nn.Loss()}) # 数据集 x, y = np.array([[0.1]], dtype=np.float32), np.array([[0.1]], dtype=np.float32) def generator_multidimensional(): for i in range(1): a = x*i b = y*i #print(a, b) yield (a, b) dataset = ds.GeneratorDataset(source=generator_multidimensional, column_names=["input", "output"]) for i in range(20000*100): #print(i, '\t', '*' * 100) model.train(1, dataset, dataset_sink_mode=False) # right # False, False # False, True # True, True xxx # not right # True, False if __name__ == '__main__': """ 设置运行的背景context """ from mindspore import context # 为mindspore设置运行背景context #context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') context.set_context(mode=context.GRAPH_MODE, device_target='GPU') import time a = time.time() main() b = time.time() print(b-a)
运行时间:
2173.19s
2181.61s
==================================================================
可以看到,在单步计算时,如果使用框架提供的单步训练函数会更好的提升算法运算效率,运算效率提升的幅度也很大,所有在进行单步训练或者非持续数据量训练时使用框架提供的单步训练函数是首选。
单步训练函数:
from mindspore.nn import TrainOneStepCell, WithLossCell
=====================================================================
本文实验环境为 MindSpore1.1 docker版本
宿主机:Ubuntu18.04系统
CPU:I7-8700
GPU:1060ti NVIDIA显卡
相关文章
- word2vec词向量训练及中文文本相似度计算
- Java实现 蓝桥杯 算法训练 数字游戏
- java实现 蓝桥杯 算法训练 安慰奶牛
- Java实现 蓝桥杯VIP 算法训练 数对
- Java实现 蓝桥杯VIP 算法训练 数列
- Java实现 蓝桥杯VIP 算法训练 入学考试
- Java实现 蓝桥杯VIP 算法训练 集合运算
- Java实现 蓝桥杯VIP 算法训练 回文数
- Java实现 蓝桥杯VIP 算法训练 薪水计算
- Java实现 蓝桥杯VIP 算法训练 薪水计算
- Java实现 蓝桥杯VIP 算法训练 整数平均值
- Java实现 蓝桥杯VIP 算法训练 斜率计算
- Java实现 蓝桥杯VIP 算法训练 斜率计算
- Java实现 蓝桥杯VIP 算法训练 斜率计算
- Java实现 蓝桥杯VIP 算法训练 最大值与最小值的计算
- Java蓝桥杯 算法训练 复数归一化
- Java实现 蓝桥杯 算法训练 未名湖边的烦恼
- Java实现 蓝桥杯 算法训练 Anagrams问题
- DL之DNN:自定义2层神经网络TwoLayerNet模型(计算梯度两种方法)利用MNIST数据集进行训练、预测
- DL之DNN:自定义MultiLayerNet【6*100+ReLU,SGD】对MNIST数据集训练进而比较【多个超参数组合最优化】性能
- NLP之word2vec:利用 Wikipedia Text(中文维基百科)语料+Word2vec工具来训练简体中文词向量
- 使用PLP特征训练crnn语音分类
- 数学建模学习(108):帮助小白快速实现批量机器学习建模训练和批量的数据可视化
- 如何使用modelarts训练海量数据
- 一篇博客带你掌握pytorch基础,学以致用(包括张量创建,索引,切片,计算,Variable对象的创建,和梯度求解,再到激活函数的使用,神经网络的搭建、训练、优化、测试)
- OpenCV——Cascade,训练自己的数据
- 3D点云重建0-02:MVSNet(R-MVSNet)-官方数据模型测试训练-报错解决
- RL之DQN:基于TF训练DQN模型玩“打砖块”游戏
- 【预训练语言模型】第 1 章:模型基础知识
- pytorch 9 float16值域计算及混合精度训练