1. 基本数据结构

  和pytorch等中的tensor类似,mxnet中的ndarray或者nd,用来操作矩阵或者张量数据。基本操作类似于Numpy, 支持计算,索引等。

      创建矩阵

from mxnet import nd   #或者 from mxnet import ndarray as nd

#创建矩阵
x1 = nd.array([[1, 2,], [3, 4]])
x2 = nd.random.uniform(1, 10, shape=(3, 3))   #3*3的矩阵
x3 = nd.random.randn(2,3)  #2*3 的矩阵
x4 = nd.random.randint(1, 10, shape=(2, 3)) #2*3 的矩阵
x5 = nd.ones(shape=(2, 2))  #2*2 的矩阵
x6 = nd.full(shape=(2, 3), val=2)  #2*3 的矩阵, 值为2
print(x1.shape, x1.size, x1.dtype)  #(2, 2)   4   <class 'numpy.float32'>

  操作矩阵

x = nd.random.randn(2, 3)
y = nd.random.randn(2, 3)
print(y.exp())  # 2*3 的矩阵
print(x*y)  # 2*3 的矩阵
print(nd.dot(x, y.T))  # 2*2 的矩阵

#和numpy相互转换
a = y.asnumpy()
print(a)
a = nd.array(np.ones((2, 3)))
print(a)

 

  矩阵索引

y = nd.random.randint(1, 10, shape=(3, 3))
print(y[1, 2]) # [5]
print(y[:, 1:3]) # 3*2
y[:,1:3] = 2   #赋值
y[1:2,0:2] = 4  #赋值
print(y)

 

2. 创建神经网络

  mxnet中gluon包中包含神经网络创建中的相关操作,和pytorch类似,可以继承block来创建神经网络,只需定义网络结构和实现前向传播函数。

  方式一: 继承nn.Block

class MyNet(nn.Block):
    def __init__(self):
        super(MyNet, self).__init__()
        self.features = nn.Sequential()
        self.features.add(
            nn.Conv2D(channels=16, kernel_size=5, strides=(1, 1),
                      padding=(0, 0), activation="relu"),  #和pytorch不同之处:不需要设置输入通道数,可以设置激活函数
            nn.MaxPool2D(pool_size=(2, 2), stides=2, padding=0),
            nn.Conv2D(channels=32, kernel_size=3, strides=(1, 1),
                      padding=(0, 0), activation="relu"),
            nn.MaxPool2D(pool_size=(2, 2), stides=2, padding=0),

        )

        self.fc = nn.Sequential()
        self.fc.add(
            nn.Dense(units=120, activation="relu"),  #和pytorch不同之处:不需要设置输入向量的大小,可以设置激活函数
            nn.Dense(units=84, activation="relu"),
            nn.Dense(units=10)
        )

    def forward(self, x):
        x = self.features(x)
        x = self.fc(x)
        return x
net = MyNet()
net.initialize() # 网络内部的参数必须先进行初始化 (pytorch中需要逐层进行初始化)
x = nd.random.uniform(shape=(1, 3, 300, 300))
print(net(x))

   方式二:直接利用nn.Sequential

net = nn.Sequential()
net.add(
    nn.Conv2D(channels=16, kernel_size=5, strides=(1, 1),
              padding=(0, 0), activation="relu"),  # 和pytorch不同之处:不需要设置输入通道数,可以设置激活函数
    nn.MaxPool2D(pool_size=(2, 2), strides=2, padding=0),
    nn.Conv2D(channels=32, kernel_size=3, strides=(1, 1),
              padding=(0, 0), activation="relu"),
    nn.MaxPool2D(pool_size=(2, 2), strides=2, padding=0),
    nn.Dense(units=120, activation="relu"),  # 和pytorch不同之处:不需要设置输入向量的大小,可以设置激活函数
    nn.Dense(units=84, activation="relu"),
    nn.Dense(units=10)
)

net.initialize()   # 网络内部的参数必须先进行初始化 (pytorch中需要逐层进行初始化)
x = nd.random.uniform(shape=(1, 3, 300, 300))
print(net(x))

3. 神经网络训练

  梯度反向传播,mxnet会自动求导,需要利用mxnet的autograd,如下:

from mxnet import nd
from mxnet import autograd

x = nd.array([[1, 2], [3, 4]])
x.attach_grad()  #1. 声明存储导数的地方
with autograd.record():   #2. 该上下文中的过程,反向传播时会自动求导
    y = 2*x*x
y.backward()          #3. 反向传播; 会自动求和再计算导数,相当于y.sum().backward()
print(x.grad)          #4. 取导数值

   3.1. 加载数据

  自己加载数据,主要需要继承mxnet.gluon.data.Dataset,然后传递给mxnet.gluon.data.DataLoader。有几个坑:

  A.  Dataset返回img和label, label不能为字符串格式

  B. Dataloader中的num_workers设置大于0时, 对于windows系统,由于采用多进程,需要写在__main__中;若还是报错时,num_workers改为0

#coding:utf-8
import mxnet as mx
from mxnet import gluon
from mxnet.gluon.data import Dataset, DataLoader
from mxnet.gluon.data.vision import transforms
import os
import cv2


#1.继承mxnet.gluon.data.Dataset, 实现__len__和__getitem__(返回每张图片和标注)
class MyDataset(Dataset):
    def __init__(self, img_root, anno_file):
        assert os.path.exists(anno_file), print("Annotation file {} not exist".format(anno_file))
        self.img_root = img_root
        self.anno_file = anno_file
        with open(anno_file, "r", encoding="utf-8") as f:
            lines = f.readlines()
        self.items = [line.strip().split() for line in lines if line.strip()]

    def __len__(self):
        return len(self.items)

    def __getitem__(self, x):
        img_name, label = self.items[x]
        img_path = os.path.join(self.img_root, img_name)
        assert os.path.exists(img_path), print("img_file {} does not exist".format(img_path))
        img = mx.image.imread(img_path)

        return img, label   #注意此处label为字符串会报错

if __name__ == "__main__":
    #2. 将dataset传入mxnet.gluon.data.Dataloader
    img_root = r"D:\data\synthtext"
    anno_file = r"D:\data\synthtext\labels.txt"
    dataset = MyDataset(img_root, anno_file)
    transformer = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
    ])  # dataset.transform_first(transformer), 对图片进行增强(即对__getitem__返回的第一项进行处理)
    train_data = DataLoader(dataset.transform_first(transformer), batch_size=2, shuffle=True, num_workers=0)
    print(train_data)
    for img, label in train_data:
        print(label)
        print(img.shape)

  3.2 定义网络

  见文章上面第二点

 

  3.3 定义损失函数  

  gluon.loss包含了部分常用的Loss,如下:

   loss = gluon.loss.SoftmaxCrossEntropyLoss()   #交叉熵损失函数
    loss = gluon.loss.L2Loss()                  #均方差损失函数
    loss = gluon.loss.CTCLoss()                 # CTC损失函数
    loss = gluon.loss.L1Loss()                  # L1 损失函数
    #位找到smoothL1,发现两个相关的 mx.nd.smooth_l1(); mx.metric.Loss("SmoothL1")


_all__ = ['Loss', 'L2Loss', 'L1Loss',
'SigmoidBinaryCrossEntropyLoss', 'SigmoidBCELoss',
'SoftmaxCrossEntropyLoss', 'SoftmaxCELoss',
'KLDivLoss', 'CTCLoss', 'HuberLoss', 'HingeLoss',
'SquaredHingeLoss', 'LogisticLoss', 'TripletLoss', 'PoissonNLLLoss', 'CosineEmbeddingLoss']

 

  3.4 定义优化器

   优化器定义在gluon.Trainer() ,第一个参数params为网络参数,第二个参数optimizer为优化器的名字,第三个参数optimizer_params为传给优化器的参数

   支持的optimizer如下:

 __all__ = [
        'AdaDelta', 'AdaGrad', 'Adam', 'Adamax', 'DCASGD', 'FTML', 'Ftrl', 'LBSGD',
        'NAG', 'NDabs', 'Nadam', 'Optimizer', 'RMSProp', 'SGD', 'SGLD', 'Signum',
        'Test', 'ccSGD', 
    ]

  共同支持的optimizer_params如下: (不同优化器还有其特定的参数)

Parameters
rescale_grad (float, optional, default 1.0) – Multiply the gradient with rescale_grad before updating. Often choose to be 1.0/batch_size.

param_idx2name (dict from int to string, optional, default None) – A dictionary that maps int index to string name.

clip_gradient (float, optional, default None) – Clip the gradient by projecting onto the box [-clip_gradient, clip_gradient].

learning_rate (float) – The initial learning rate. If None, the optimization will use the learning rate from lr_scheduler. If not None, it will overwrite the learning rate in lr_scheduler. If None and lr_scheduler is also None, then it will be set to 0.01 by default.

lr_scheduler (LRScheduler, optional, default None) – The learning rate scheduler.

wd (float, optional, default 0.0) – The weight decay (or L2 regularization) coefficient. Modifies objective by adding a penalty for having large weights.

sym (Symbol, optional, default None) – The Symbol this optimizer is applying to.

begin_num_update (int, optional, default 0) – The initial number of updates.

multi_precision (bool, optional, default False) – Flag to control the internal precision of the optimizer. False: results in using the same precision as the weights (default), True: makes internal 32-bit copy of the weights and applies gradients in 32-bit precision even if actual weights used in the model have lower precision. Turning this on can improve convergence and accuracy when training with float16.

param_dict (dict of int -> gluon.Parameter, default None) – Dictionary of parameter index to gluon.Parameter, used to lookup parameter attributes such as lr_mult, wd_mult, etc. param_dict shall not be deep copied.

aggregate_num (int, optional, default None) – Number of weights to be aggregated in a list. They are passed to the optimizer for a single optimization step. In default, only one weight is aggregated. When aggregate_num is set to numpy.inf, all the weights are aggregated.

use_fused_step (bool, optional, default None) – Whether or not to use fused kernels for optimizer. When use_fused_step=False, step is called, otherwise, fused_step is called.

Properties –

---------- –

learning_rate – The current learning rate of the optimizer. Given an Optimizer object optimizer, its learning rate can be accessed as optimizer.learning_rate.
optimizer_params

相关文章:

  • 2022-12-23
  • 2021-10-16
  • 2021-07-17
  • 2021-12-12
  • 2021-12-15
  • 2021-12-15
  • 2021-12-12
  • 2021-11-18
猜你喜欢
  • 2021-12-16
  • 2021-06-29
  • 2021-05-21
  • 2021-07-13
  • 2021-11-14
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案