mirror of
https://github.com/openmlsys/openmlsys-zh.git
synced 2026-04-05 03:37:53 +08:00
fix ch2 (#207)
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
algorithms)来计算梯度(Gradient),完成对模型参数的更新。
|
||||
|
||||
- **训练过程:**
|
||||
给定一个数据集,模型,损失函数和优化器,用户需要训练API来定一个循环(Loop)从而将数据集中的数据按照小批量(mini-batch)的方式读取出来,反复计算梯度来更新模型。这个反复的过程称为训练。
|
||||
给定一个数据集,模型,损失函数和优化器,用户需要训练API来定义一个循环(Loop)从而将数据集中的数据按照小批量(mini-batch)的方式读取出来,反复计算梯度来更新模型。这个反复的过程称为训练。
|
||||
|
||||
- **测试和调试:**
|
||||
训练过程中,用户需要测试API来对当前模型的精度进行评估。当精度达到目标后,训练结束。这一过程中,用户往往需要调试API来完成对模型的性能和正确性进行验证。
|
||||
@@ -25,8 +25,9 @@
|
||||
|
||||
### 环境配置
|
||||
|
||||
下面以MindSpore框架实现多层感知机为例,了解完整的机器学习工作流。代码运行环境为MindSpore1.5.2,Ubuntu16.04,CUDA10.1。
|
||||
在构建机器学习工作流程前,MindSpore需要通过context.set_context来配置运行需要的信息,如运行模式、后端信息、硬件等信息。
|
||||
导入context模块,配置运行需要的信息。以下代码运行环境为Ubuntu16.04,CUDA10.1,MindSpore1.5.2。
|
||||
以下代码导入context模块,配置运行需要的信息。
|
||||
|
||||
```python
|
||||
import os
|
||||
@@ -89,7 +90,8 @@ def create_dataset(data_path, batch_size=32, repeat_size=1,
|
||||
### 模型定义
|
||||
|
||||
使用MindSpore定义神经网络需要继承mindspore.nn.Cell,神经网络的各层需要预先在\_\_init\_\_方法中定义,然后重载\_\_construct\_\_方法实现神经网络的前向传播过程。
|
||||
因为输入大小被处理成$32 \times 32$的图片,需要用Flatten将数据压平为一维向量后给全连接层,全连接层输入大小为$32 \times 32$,预测$0 \sim 9$中的哪个数字所以最后输出大小为10,下面定义了一个三层的全连接层。
|
||||
因为输入大小被处理成$32 \times 32$的图片,所以需要用Flatten将数据压平为一维向量后给全连接层。
|
||||
全连接层的输入大小为$32 \times 32$,输出是预测属于$0 \sim 9$中的哪个数字,因此输出大小为10,下面定义了一个三层的全连接层。
|
||||
```python
|
||||
# 导入需要用到的模块
|
||||
import mindspore.nn as nn
|
||||
|
||||
@@ -76,10 +76,10 @@ fc3_weights = variable(random(size=(64, 10)))
|
||||
all_weights = [conv1_filters, conv2_filters, fc1_weights, fc2_weights, fc3_weights]
|
||||
|
||||
# 构建卷积模型的连接过程
|
||||
output = convolution(input, conv1_filters, stride=1, padding=0)
|
||||
output = pooling(output, kernel_size=3, stride=1, padding=0, mode='max')
|
||||
output = convolution(output, conv2_filters, stride=1, padding=0)
|
||||
output = pooling(output, kernel_size=3, stride=1, padding=0, mode='max')
|
||||
output = convolution(input, conv1_filters, stride=1, padding='same')
|
||||
output = pooling(output, kernel_size=3, stride=2, padding='same', mode='max')
|
||||
output = convolution(output, conv2_filters, stride=1, padding='same')
|
||||
output = pooling(output, kernel_size=3, stride=2, padding='same', mode='max')
|
||||
output=flatten(output)
|
||||
output = fully_connected(output, fc1_weights)
|
||||
output = fully_connected(output, fc2_weights)
|
||||
|
||||
Reference in New Issue
Block a user