随机数生成函数导致模型速度越来越慢
收藏回复举报
随机数生成函数导致模型速度越来越慢
发表于2023-09-14 16:15:27
0 查看

1 系统环境

硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU

MindSpore版本: mindspore=2.0.0

执行模式(PyNative/ Graph):不限

Python版本: Python=3.9.13

操作系统平台: 不限

2 报错信息

2.1 问题描述

随机数生成函数导致模型速度越来越慢

2.2 脚本代码(代码格式,可上传附件)

import os
import time
import random
import numpy as np
import mindspore as ms
from mindspore import nn, Tensor, ops

seed = 1
ms.set_seed(seed)
np.random.seed(seed)  # Numpy module.
random.seed(seed)  # Python random module.	

class net(nn.Cell):
    def __init__(self, in_dims=4, out_dims=1):
        super().__init__()
        self.fc1 = nn.Dense(in_dims, 4, has_bias=False)
        self.relu = nn.ReLU()
        self.fc2 = nn.Dense(4, out_dims, has_bias=False)

    def construct(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        rand_tensor = ms.numpy.rand(x.shape)
        return x


class NetWithLoss(nn.Cell):
    def __init__(self, net: nn.Cell):
        super(NetWithLoss, self).__init__()
        self.net = net

    def construct(self, state):
        out = self.net(state)
        return out.mean()

net = net()
optimizer = nn.Adam(net.get_parameters(), 0.001) # 使用0学习率,便于观察梯度
net_with_loss = NetWithLoss(net)
net_train_one_step = nn.TrainOneStepCell(net_with_loss, optimizer)

data = ms.Tensor(
    np.random.normal(loc=0.0, scale=1.0, size=(1,4)),
    dtype=ms.float32)

for i in range(1, 1000):
    t1 = time.time()
    out = net_train_one_step(data)
    t2 = time.time()
    print('%d-iter, time:'%(i), t2-t1)

3 根因分析

******此处由用户补充详细的定位过程******

4 解决方案

******此处由用户填写******

包含文字方案和最终脚本代码

请将正确的脚本打包并上传附件

我要发帖子