1. 下载教程

可以用浏览器下载zip格式并解压,在解压目录文件资源管理器的地址栏输入cmd进入命令行模式。

也可以

git pull https://github.com/mli/gluon-tutorials-zh

2.安装gluon CPU

添加源:

# 优先使用清华conda镜像
conda config --prepend channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ # 也可选用科大conda镜像
conda config --prepend channels http://mirrors.ustc.edu.cn/anaconda/pkgs/free/

cmd中安装

conda env create -f environment.yml
activate gluon # 注意Windows下不需要 source

可更新教程:

conda env update -f environment.yml

3.安装GPU版本

先卸载CPU

pip uninstall mxnet

然后

pip install --pre mxnet-cu75 # CUDA 7.5
pip install --pre mxnet-cu80 # CUDA 8.0

【可选项】国内用户可使用豆瓣pypi镜像加速下载:

pip install --pre mxnet-cu75 -i https://pypi.douban.com/simple # CUDA 7.5
pip install --pre mxnet-cu80 -i https://pypi.douban.com/simple # CUDA 8.0

查看安装

import pip
for pkg in ['mxnet', 'mxnet-cu75', 'mxnet-cu80']:
pip.main(['show', pkg])

4.查看教程

然后安装notedown,运行Jupyter并加载notedown插件:

pip install https://github.com/mli/notedown/tarball/master
jupyter notebook --generate-config
jupyter notebook --NotebookApp.contents_manager_class='notedown.NotedownContentsManager'

5.教程简记

跟NumPy的转换

from mxnet import ndarray as nd
import numpy as np
x = np.ones((2,3))
y = nd.array(x) # numpy -> mxnet
z = y.asnumpy() # mxnet -> numpy
print([z, y])

自动求导

import mxnet.autograd as ag

假设我们想对函数 $f = 2*x^2$ 求关于 $x$的导数。

1.创建变量

x = nd.array([[1, 2], [3, 4]])

2.通过NDArray的方法attach_grad()来要求系统申请梯度空间

x.attach_grad()

3.定义函数 f

with ag.record():
y = x * 2
z = y * x

4.反向传播,求梯度

z.backward()

5.梯度:

print('x.grad: ', x.grad)

线性回归,从零开始

#coding=utf-8
"""线性回归,从零开始""" from mxnet import ndarray as nd
from mxnet import autograd
import matplotlib.pyplot as plt
import random # 1.创建数据集
# y[i] = 2 * X[i][0] - 3.4 * X[i][1] + 4.2 + noise
# y = X*w + b + n
num_inputs = 2
num_examples = 1000 true_w = [2, -3.4]
true_b = 4.2 X = nd.random_normal(shape=(num_examples, num_inputs))
y = true_w[0] * X[:,0] + true_w[1] * X[:,1] + true_b
y += 0.01 * nd.random_normal(shape=y.shape) # plt.scatter(X[:,1].asnumpy(), y.asnumpy())
# plt.show() # 2.数据读取
batch_size = 10
def data_iter():
# 产生一个随机索引
idx = list(range(num_examples))
random.shuffle(idx)
for i in range(0, num_examples, batch_size):
j = nd.array(idx[i:min(i+batch_size, num_examples)])
yield nd.take(X, j), nd.take(y, j) # for data, label in data_iter():
# print (data, label)
# break # 3.初始化模型参数
w = nd.random_normal(shape=(num_inputs,1))
b = nd.zeros((1,))
params = [w, b] # print (params)
# 创建梯度空间
for param in params:
param.attach_grad() # 4.定义模型
def net(X):
return nd.dot(X, w) + b # 5.定义损失函数
def square_loss(yhat, y):
# 注意这里将y变形成yhat的形状来避免矩阵的broadcasting
return (yhat - y.reshape(yhat.shape)) ** 2 # 6.优化
def SGD(params, lr):
for param in params:
param[:] = param - lr * param.grad # 7.训练
# 模型函数
def real_fn(X):
return 2 * X[:, 0] - 3.4 * X[:, 1] + 4.2
# 绘制损失随训练次数降低的折线图,以及预测值和真实值的散点图
def plot(losses, X, sample_size=100):
xs = list(range(len(losses)))
fig, axes = plt.subplots(1, 2)
axes[0].set_title('Loss during training')
axes[0].plot(xs, losses, '-r')
axes[1].set_title('Estimated vs real function')
axes[1].plot(X[:sample_size, 1].asnumpy(),
net(X[:sample_size, :]).asnumpy(), 'or', label='Estimated')
axes[1].plot(X[:sample_size, 1].asnumpy(),
real_fn(X[:sample_size, :]).asnumpy(), '*g', label='Real')
axes[1].legend()
plt.show() epochs = 5
learning_rate = 0.001
niter = 0
losses = []
moving_loss = 0
smoothing_constant = 0.01 # 训练
for e in range(epochs):
total_loss = 0
# 每个epoch
for data, label in data_iter():
with autograd.record():
output = net(data) # 前向传播
loss = square_loss(output, label)
loss.backward() # 反向传播
SGD(params, learning_rate) # 更新参数
iter_loss = nd.sum(loss).asscalar() / batch_size
total_loss += nd.sum(loss).asscalar() # 记录损失变化
niter += 1
curr_loss = nd.mean(loss).asscalar()
moving_loss = (1 - smoothing_constant) * moving_loss + smoothing_constant * curr_loss losses.append(iter_loss)
if (niter + 1) % 100 == 0:
print("Epoch %s, batch %s. Average loss: %f" % (e, niter, total_loss / num_examples))
plot(losses, X)

使用GPU

a = nd.array([1,2,3], ctx=mx.gpu())
b = nd.zeros((3,2), ctx=mx.gpu())

可以通过copytoas_in_context来在设备直接传输数据。

y = x.copyto(mx.gpu())
z = x.as_in_context(mx.gpu())

这两个函数的主要区别是,如果源和目标的context一致,as_in_context不复制,而copyto总是会新建内存:

这类似与caffe中的cuda操作

float* tmp_transform_bbox = NULL;
CUDA_CHECK(cudaMalloc(&tmp_transform_bbox, * sizeof(Dtype) * rpn_pre_nms_top_n));//修改retained_anchor_num
cudaMemcpy(tmp_transform_bbox, &transform_bbox_[transform_bbox_begin], rpn_pre_nms_top_n * sizeof(Dtype) * , cudaMemcpyDeviceToDevice);

参数获取

w = net[0].weight
b = net[0].bias
print 'name: ', net[0].name, '\nweight: ', w, '\nbias: ', b print('weight:', w.data())
print('weight gradient', w.grad())
print('bias:', b.data())
print('bias gradient', b.grad())
params = net.collect_params()
print(params)
print(params['sequential0_dense0_bias'].data())
print(params.get('dense0_weight').data())

参数初始化

from mxnet import init
params = net.collect_params()
params.initialize(init=init.Normal(sigma=0.02), force_reinit=True)
print(net[0].weight.data(), net[0].bias.data())

6.使用中错误解决

1.python2打印权重报错

w = net[0].weight
b = net[0].bias
print('name: ', net[0].name, '\nweight: ', w, '\nbias: ', b)

把C:\Anaconda2\envs\gluon\Lib\site-packages\mxnet\gluon\parameter.py 119行改为

s = 'Parameter {name} (shape={_shape}, dtype={dtype})'

同时,如果是Python2需要把print后去掉括号。。。

win10 + gluon + GPU的更多相关文章

  1. Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置

    最近入手一台GTX 1070的笔记本,手痒想在win10上试下GPU跑模型,所以就有了接下来的安装GPU版mxnet的坎坷历程,经过多重试验终于搞定了python和R安装mxnet,现将主要点记录如下 ...

  2. win10+caffe+GPU

    由于学习需要,决定安装caffe,之前用的都是基于theano的keras.听说win下caffe很难配置,经过一个下午和晚上的配置终于成功,以此记录. 我的电脑:win10 64位,N卡GTX950 ...

  3. win10 caffe GPU环境搭建

    一.准备 系统:win10 显卡:gtx1050Ti 前期的一些必要软件安装,包括python3.5.matlab2016.vs2015.git, 可参考:win10+vs2015编译caffe的cp ...

  4. TensorFlow 安装 Win10 Python+GPU

    前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...

  5. win10+Theano+GPU

    1. cuda + cudnn 首先还是要先安装GPU库,具体和caffe安装中一样. 2. Theano 为防止下载速度慢,配置清华镜像 conda config --add channels ht ...

  6. win10+tensorflow+CUDA 心酸采坑之路

    最近准备学习机器学习和深度学习,所以入坑Tensorflow,之前一直使用的是Anaconda3的cpu版本的Tensorflow,但是这次作死一直想用GPU版本的,主要是不想浪费我的1080ti,但 ...

  7. Windows安装TensorFlow

    1.下载安装Anaconda 官方地址:https://www.continuum.io/downloads/镜像地址:https://mirrors.tuna.tsinghua.edu.cn/ana ...

  8. TensorFlow目标检测(object_detection)api使用

    https://github.com/tensorflow/models/tree/master/research/object_detection 深度学习目标检测模型全面综述:Faster R-C ...

  9. win10 + cuda10 +cudnn + GLUON 环境搭建

    1. <动手学深度学习> 由于新型非典型肺炎扩散,上班日期挪到2.10 日了,在家比较无聊决定了解一下深度学习. 在github 上找到一个资源,可以动手学深度学习,便打算按照这本书的内容 ...

随机推荐

  1. Linux下,如何查看磁盘是否包含数据

    可以使用lquerypv -h来查看磁盘是否包含数据,或磁盘头是否被dd过.这在安装RAC的过程中,是非常实用的一个命令.如果不包括数据的话,那么如下所示: [ZFFR4CB2101:root]/]& ...

  2. Linux - 操作系统

    操作系统(科普章节) 目标 了解操作系统及作用 1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自 ...

  3. Rifidi

    简介 Rifidi是RFID软件公司Pramari推出了一款开源中间件平台,其主页是:http://www.rifidi.org/ 其分为Edge Server, Workbench, Prototy ...

  4. String 中intern

    首先贴上源码中的注释 在一个String类上调用这个方法的时候如果常量池中存在和这个String对象相同的对象的时候,直接返回常量池中的常量,如果常量池中不存在这个对象,就直接将其将其加入常量池,并且 ...

  5. Java中CountDownLatch和CyclicBarrier

    Java编程思想中的例子import javax.validation.constraints.Size; import java.util.Random; import java.util.conc ...

  6. tp3.2 phpexcel 简单导出多个sheet(execl表格)

    参考链接:https://blog.csdn.net/u011341352/article/details/70211962 以下是公共类PHPExcel.php文件: // 开始 <?php/ ...

  7. charles本地调试之map和rewrite功能

    charles是一款mac下代理调试工具,对于前端开发同学来说是相当方便的一个调试接口的工具:不过charles需要收费,不过在天朝几乎收费的软件都能找到破解方法: 使用charles前,需要将cha ...

  8. Python之路(第二十九篇) 面向对象进阶:内置方法补充、异常处理

    一.__new__方法 __init__()是初始化方法,__new__()方法是构造方法,创建一个新的对象 实例化对象的时候,调用__init__()初始化之前,先调用了__new__()方法 __ ...

  9. Pivot For和UNPivot For

    一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...

  10. 简单利用jQuery,让前端开发不再依赖于后端的接口

    前端开发的过程中,我们免不了和后端进行联调,这时候就会出现以下的尴尬场景: 接口没写好,没法做接下来的功能 功能写好了,接口没写好,没法测这个功能 联调了,出了BUG,不知道锅在谁身上,只得陪后端耗时 ...