以下是对代码中关键 MindSpore API 接口的整理:
1.mindspore.nn.Cell
使用例子:class Regression_car(nn.Cell):
参数解释:是所有神经网络的基类,用于构建网络模型。用户自定义的网络类需继承它,并在类中定义网络层和前向传播的construct方法 。在上述例子中,Regression_car类继承nn.Cell,定义了一个包含 3 个全连接层和 ReLU 激活函数的神经网络。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.Cell.html
2.mindspore.nn.Flatten
使用例子:在Regression_car类的__init__方法中self.flatten = nn.Flatten(),在construct方法中x = self.flatten(x)。
参数解释:用于将多维输入张量展平为一维张量,不改变批量大小(batch size)维度。比如将一个形状为(batch_size, channels, height, width)的图像张量,变为(batch_size, channels * height * width),方便后续全连接层的输入。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.Flatten.html
3.mindspore.nn.ReLU
使用例子:在Regression_car类的__init__方法中self.relu = nn.ReLU() ,在全连接层定义时self.fc1 = nn.Dense(9,64, activation='relu') ,self.fc2 = nn.Dense(64,64, activation='relu')。
参数解释:ReLU(Rectified Linear Unit)激活函数,公式为
f(x)=max(0,x)。它能给神经网络引入非线性,使得网络可以学习到更复杂的模式。在代码中,对fc1和fc2全连接层的输出使用 ReLU 激活,增强网络的表达能力。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.ReLU.html
4.mindspore.nn.Dense
使用例子:self.fc1 = nn.Dense(9,64, activation='relu'),self.fc2 = nn.Dense(64,64, activation='relu'),self.fc3 = nn.Dense(64,1)。
参数解释:全连接层,也叫密集连接层。nn.Dense(in_channels, out_channels, activation=None),in_channels表示输入特征的数量,out_channels表示输出特征的数量,activation用于指定激活函数(可选)。如self.fc1 = nn.Dense(9,64, activation='relu')表示输入有 9 个特征,输出为 64 个特征,并使用 ReLU 激活函数。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.Dense.html
5.mindspore.nn.MSELoss
使用例子:net_loss = nn.MSELoss()
参数解释:均方误差损失函数,用于衡量预测值和真实值之间的误差。计算公式为L=n1∑i=1n(yi−y^i)2
n是样本数量。在回归任务中常用,本实验用于计算汽车油耗里程预测值与真实值的误差。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.MSELoss.html
6.mindspore.nn.RMSProp
使用例子:net_opt = nn.RMSProp(network.trainable_params(), 0.001)
参数解释:nn.RMSProp(params, learning_rate),params是网络中需要更新的可训练参数,通常通过network.trainable_params()获取;learning_rate是学习率,控制参数更新的步长,学习率过大可能导致模型训练不稳定,过小则会使训练速度过慢。在代码中,使用 RMSProp 优化器对网络参数进行更新,学习率设为 0.001。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.RMSProp.html
7.mindspore.nn.WithLossCell
使用例子:with_loss=nn.WithLossCell(network, net_loss)
参数解释:用于将网络和损失函数进行封装。nn.WithLossCell(network, loss_fn),network是定义好的神经网络,loss_fn是损失函数。封装后,该单元接受数据和标签作为输入,并返回计算的损耗,方便后续在训练过程中计算损失值。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.WithLossCell.html
8.mindspore.nn.TrainOneStepCell
使用例子:train_step = nn.TrainOneStepCell(with_loss, net_opt).set_train()
参数解释:nn.TrainOneStepCell(loss_network, optimizer),loss_network是包含网络和损失函数的单元(如WithLossCell的实例),optimizer是优化器。该类实现了单步训练的逻辑,调用它的实例可以执行一次训练步骤,更新网络参数。set_train()方法用于设置为训练模式。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.TrainOneStepCell.html
9.mindspore.nn.WithEvalCell
使用例子:evalcell=nn.WithEvalCell(network,net_loss)
参数解释:nn.WithEvalCell(network, loss_fn),network是神经网络,loss_fn是损失函数。用于评估模型,接受网络和损失函数作为参数,计算模型的损失,并返回损失、输出和标签,方便计算评估指标(如 MAE、MSE)。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/nn/mindspore.nn.WithEvalCell.html
10.mindspore.nn.MAE、mindspore.nn.MSE
使用例子:mae = nn.MAE()
mse = nn.MSE()
参数解释:参数解释:nn.MAE是平均绝对误差评估指标类,计算预测值与真实值之间绝对误差的平均值;nn.MSE是均方误差评估指标类,计算预测值与真实值之间误差平方的平均值 。在代码中用于评估模型训练和验证过程中的性能。
API链接
mindspore.nn.MAE:
mindspore.nn.MSE:
11.mindspore.Tensor
使用例子:ds_xtrain= Tensor(X_train, ms.float32),ds_ytrain= Tensor(Y_train, ms.int32),ds_xtest=Tensor(X_test, ms.float32),ds_ytest=Tensor(Y_test, ms.int32)。
参数解释:mindspore.Tensor(data, dtype=None),data是要转换为张量的数据,可以是 NumPy 数组等;dtype指定张量的数据类型,如ms.float32表示 32 位浮点数,ms.int32表示 32 位整数。在代码中用于将 NumPy 数组转换为 MindSpore 的张量,以便在 MindSpore 框架中进行计算。
API链接
https://www.mindspore.cn/docs/zh-CN/r2.2/api_python/mindspore/mindspore.Tensor.html
1288

被折叠的 条评论
为什么被折叠?



