资源简介

自 2017 年 1 月 PyTorch 推出以来,其热度持续上升,一度有赶超 TensorFlow 的趋势。PyTorch 能在短时间内被众多研究人员和工程师接受并推崇是因为其有着诸多优点,如采用 Python 语言、动态图机制、网络构建灵活以及拥有强大的社群等。因此,走上学习 PyTorch 的道路已刻不容缓。 本教程以实际应用、工程开发为目的,着重介绍模型训练过程中遇到的实际问题和方法。如上图所示,在机器学习模型开发中,主要涉及三大部分,分别是数据、模型和损失函数及优化器。本文也按顺序的依次介绍数据、模型和损失函数及优化器,从而给大家带来清晰的机器学习结构。

资源截图

代码片段和文件信息

# coding:utf-8
“““
    将cifar10的data_batch_12345 转换成 png格式的图片
    每个类别单独存放在一个文件夹,文件夹名称为0-9
“““
from scipy.misc import imsave
import numpy as np
import os
import pickle

data_dir = ‘../../Data/cifar-10-batches-py/‘
train_o_dir = ‘../../Data/cifar-10-png/raw_train/‘
test_o_dir = ‘../../Data/cifar-10-png/raw_test/‘

Train = False   # 不解压训练集,仅解压测试集

# 解压缩,返回解压后的字典
def unpickle(file):
    fo = open(file ‘rb‘)
    dict_ = pickle.load(fo encoding=‘bytes‘)
    fo.close()
    return dict_

def my_mkdir(my_dir):
    if not os.path.isdir(my_dir):
        os.makedirs(my_dir)


# 生成训练集图片,
if __name__ == ‘__main__‘:
    if Train:
        for j in range(1 6):
            data_path = data_dir + “data_batch_“ + str(j)  # data_batch_12345
            train_data = unpickle(data_path)
            print(data_path + “ is loading...“)

            for i in range(0 10000):
                img = np.reshape(train_data[b‘data‘][i] (3 32 32))
                img = img.transpose(1 2 0)

                label_num = str(train_data[b‘labels‘][i])
                o_dir = os.path.join(train_o_dir label_num)
                my_mkdir(o_dir)

                img_name = label_num + ‘_‘ + str(i + (j - 1)*10000) + ‘.png‘
                img_path = os.path.join(o_dir img_name)
                imsave(img_path img)
            print(data_path + “ loaded.“)

    print(“test_batch is loading...“)

    # 生成测试集图片
    test_data_path = data_dir + “test_batch“
    test_data = unpickle(test_data_path)
    for i in range(0 10000):
        img = np.reshape(test_data[b‘data‘][i] (3 32 32))
        img = img.transpose(1 2 0)

        label_num = str(test_data[b‘labels‘][i])
        o_dir = os.path.join(test_o_dir label_num)
        my_mkdir(o_dir)

        img_name = label_num + ‘_‘ + str(i) + ‘.png‘
        img_path = os.path.join(o_dir img_name)
        imsave(img_path img)

    print(“test_batch loaded.“)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\
     文件        2050  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\1_1_cifar10_to_png.py
     文件        1409  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\1_2_split_dataset.py
     文件        1106  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\1_3_generate_txt.py
     文件         991  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\1_3_mydataset.py
     文件        1148  2018-12-20 05:05  PyTorch_Tutorial-master\Code\1_data_prepare\1_5_compute_mean.py
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\2_model\
     文件        7118  2018-12-20 05:05  PyTorch_Tutorial-master\Code\2_model\2_finetune.py
     文件      249573  2018-12-20 05:05  PyTorch_Tutorial-master\Code\2_model\net_params.pkl
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\
     文件         737  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\1_L1Loss.py
     文件         744  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\2_MSELoss.py
     文件        2698  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\3_CrossEntropyLoss.py
     文件         701  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\4_NLLLoss.py
     文件         335  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\5_PoissonNLLLoss.py
     文件         967  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_1_lossFunction\6_KLDivLoss.py
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\
     文件         600  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\1_param_groups.py
     文件         631  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\2_zero_grad.py
     文件         777  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\3_state_dict.py
     文件        1400  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\4_load_state_dict.py
     文件         838  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\5_add_param_group.py
     文件        1001  2018-12-20 05:05  PyTorch_Tutorial-master\Code\3_optimizer\3_2_optimizer\net_params.pkl
     目录           0  2018-12-20 05:05  PyTorch_Tutorial-master\Code\4_viewer\
     文件        3901  2018-12-20 05:05  PyTorch_Tutorial-master\Code\4_viewer\1_tensorboardX_demo.py
     文件        2633  2018-12-20 05:05  PyTorch_Tutorial-master\Code\4_viewer\2_visual_weights.py
     文件        2041  2018-12-20 05:05  PyTorch_Tutorial-master\Code\4_viewer\3_visual_featuremaps.py
     文件        3733  2018-12-20 05:05  PyTorch_Tutorial-master\Code\4_viewer\4_hist_grad_weight.py
............此处省略14个文件信息

评论

共有 条评论