主要内容

(1)torch7的安装

(2)torch7的hello-world入门

(3)对抗生成网络torch-gan

......

(1)torch7的安装(centos7测试)

LuaJIT方法安装:

git clone https://github.com/torch/distro.git --recursive
cd torch; bash install-deps;
./install.sh
source ~/.bashrc

Lua5.2方法安装:

git clone https://github.com/torch/distro.git --recursive
cd torch
./clean.sh
TORCH_LUA_VERSION=LUA52 ./install.sh
luarocks install image
luarocks list
luarocks install hdf5
luarocks install matio
luarocks install optnet

测试安装环境:

th
print('hello world')

自己安装时遇到的问题,没有检测到libmatio.so.2

进入自己的torchroot/install/lib下面发现没有这个动态链接库,

解决方式,源码安装,

下载matio源码包,https://sourceforge.net/projects/matio/,注意要下载的是matio-1.5.2这个版本。

unzip matio-1.5.2.zip
cd matio-1.5.2
./configure
make -j8
make check
make install
#此时会安装在/usr/local/lib下面,
cp  /usr/local/lib/libmatio*  torchroot/install/lib

问题解决,matio.so.2可以检测到了。

(2)torch7的hello-world入门

字符串(strings),数字(numbers),tables(表)

a = 'hello'
print(a)
b = {}
b[1] = a
print(b)
b[2] = 30
for i=1,#b do -- the # operator is the length operator in Luaprint(b[i])
end

张量(Tensors)

a = torch.Tensor(5,3) -- construct a 5x3 matrix, uninitialized
a = torch.rand(5,3)
print(a)
b=torch.rand(3,4) -- matrix-matrix multiplication: syntax 1
a*b-- matrix-matrix multiplication: syntax 2
torch.mm(a,b)-- matrix-matrix multiplication: syntax 3
c=torch.Tensor(5,4)
c:mm(a,b) -- store the result of a*b in c

CUDA张量(CUDA Tensors)

require 'cutorch';
a = a:cuda()
b = b:cuda()
c = c:cuda()
c:mm(a,b) -- done on GPU

张量相加

function addTensors(a,b)return a+b
end
a = torch.ones(5,2)
b = torch.Tensor(2,5):fill(4)
print(addTensors(a,b))

神经网络(Neural Networks)

lenet5的网络结构图如下,

torch的神经网络支持线性,级联,并行三种连接方式。这里我们的lenet5采用线性的方式。

require 'nn';
net = nn.Sequential()
net:add(nn.SpatialConvolution(1, 6, 5, 5)) -- 1 input image channel, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU())                       -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())                       -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU())                       -- non-linearity
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())                       -- non-linearity
net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax())
print('Lenet5\n' .. net:__tostring());

input = torch.rand(1,32,32) -- pass a random tensor as input to the network
output = net:forward(input)
print(output)
net:zeroGradParameters() -- zero the internal gradient buffers of the network (will come to this later)
gradInput = net:backward(input, torch.rand(10))
print(#gradInput)

损失函数(loss function)

criterion = nn.ClassNLLCriterion() -- a negative log-likelihood criterion for multi-class classification
criterion:forward(output, 3) -- let's say the groundtruth was class number: 3
gradients = criterion:backward(output, 3)
gradInput = net:backward(input, gradients)

查看学习到的权值(weights)和偏差(bias),其中,权值weights的跟新策略为weight = weight + learningRate * gradWeight

m = nn.SpatialConvolution(1,3,2,2) -- learn 3 2x2 kernels
print(m.weight) -- initially, the weights are randomly initialized
print(m.bias) -- The operation in a convolution layer is: output = convolution(input,weight) + bias

训练神经网络(cifar-10)

cifar-10是加拿大研究院开源的一个数据集,包含10类目标,大小为32*32*3

整体流程分为下面几步

<1>导入数据,并且做归一化处理(减均值,除标准差)

<2>定义网络结构

<3>定义损失函数

<4>训练网络

<5>测试训练结果

<1>下载数据,归一化处理

require 'paths'
if (not paths.filep("cifar10torchsmall.zip")) thenos.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')os.execute('unzip cifar10torchsmall.zip')
end
trainset = torch.load('cifar10-train.t7')
testset = torch.load('cifar10-test.t7')
classes = {'airplane', 'automobile', 'bird', 'cat','deer', 'dog', 'frog', 'horse', 'ship', 'truck'}
print(trainset)
print(#trainset.data)

定义一个可以返回图片和标签的函数

setmetatable(trainset, {__index = function(t, i) return {t.data[i], t.label[i]} end}
);
trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.function trainset:size() return self.data:size(1)
end
print(trainset:size())
print(trainset[33]) -- load sample number 33.

获取图片数据的红色通道

redChannel = trainset.data[{ {}, {1}, {}, {}  }]  -- this picks {all images, 1st channel, all vertical pixels, all horizontal pixels}
print(#redChannel)

减均值和归一化操作

mean = {} -- store the mean, to normalize the test set in the future
stdv  = {} -- store the standard-deviation for the future
for i=1,3 do -- over each image channelmean[i] = trainset.data[{ {}, {i}, {}, {}  }]:mean() -- mean estimationprint('Channel ' .. i .. ', Mean: ' .. mean[i])trainset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtractionstdv[i] = trainset.data[{ {}, {i}, {}, {}  }]:std() -- std estimationprint('Channel ' .. i .. ', Standard Deviation: ' .. stdv[i])trainset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scaling
end

<2>定义一个神经网络,这里使用的还是lenet5

net = nn.Sequential() net:add(nn.SpatialConvolution(3, 6, 5, 5))  -- 3 input image channels, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU())  -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))  -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())  -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))  -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5 net:add(nn.Linear(16*5*5, 120)) -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU())  -- non-linearity
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())  -- non-linearitynet:add(nn.Linear(84, 10))  -- 10 is the number of outputs of the network (in this case, 10 digits) net:add(nn.LogSoftMax()) -- converts the output to a log-probability. Useful for classification problems

<3>定义损失函数

criterion = nn.ClassNLLCriterion()

<4>训练神经网络(cpu训练7700)

trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
trainer:train(trainset)

<5>测试网络训练,输出准确性

测试集的减均值和归一化处理

testset.data = testset.data:double()   -- convert from Byte tensor to Double tensor
for i=1,3 do -- over each image channeltestset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtraction    testset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scaling
end

随便测试一下第100张图片是否分类正确,并且输出分数

print(classes[testset.label[100]])
predicted = net:forward(testset.data[100])
-- the output of the network is Log-Probabilities. To convert them to probabilities, you have to take e^x
print(predicted:exp())

输出测试集上的准确性

correct = 0
for i=1,10000 dolocal groundtruth = testset.label[i]local prediction = net:forward(testset.data[i])local confidences, indices = torch.sort(prediction, true)  -- true means sort in descending orderif groundtruth == indices[1] thencorrect = correct + 1end
end
print(correct, 100*correct/10000 .. ' % ')

输出测试集上的准确性和类别

class_performance = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
for i=1,10000 do local groundtruth = testset.label[i] local prediction = net:forward(testset.data[i]) local confidences, indices = torch.sort(prediction, true) -- true means sort in descending order if groundtruth == indices[1] then class_performance[groundtruth] = class_performance[groundtruth] + 1 end
end
for i=1,#classes doprint(classes[i], 100*class_performance[i]/1000 .. ' %')
end

<6>使用cuda进行训练,大大加速训练过程(TitanX),可以看到速度提高了22.88倍

require 'cunn';
net = net:cuda()
criterion = criterion:cuda()
trainset.data = trainset.data:cuda()
trainset.label = trainset.label:cuda()
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
trainer:train(trainset)

(3)对抗生成网络torch-gan

git clone https://github.com/skaae/torch-gan.git
th train_lfw.lua -g 0 --K 10

然后就可以静静的等待结果了。

reference:

http://torch.ch/

https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning%20with%20Torch.ipynb

https://github.com/skaae/torch-gan

https://github.com/torch/demos

周末torch7闪电战(blitz)相关推荐

  1. 周末轻松一刻,欣赏完全由程序自己回忆的视频片段

    本文转自微信公众号:Createamind 原文地址:http://mp.weixin.qq.com/s?__biz=MzA5MDMwMTIyNQ==&mid=2649286943&i ...

  2. 程序员的周末:纯野的一天

    牵强的标题 自己写上这个标题都觉得挺牵强的.首先,我算不上是一个纯粹意义上的程序员了.虽然上了一个多月的班,但对于这份职业到底做什么我都还不是特别的清楚.每天做着一些类似文秘的工作,又类似技术支持的工 ...

  3. 智能跳过节假日算法java_java计算两个日期之前的天数实例(排除节假日和周末)...

    java计算两个日期之前的天数实例(排除节假日和周末) 发布时间:2020-09-02 23:07:01 来源:脚本之家 阅读:108 作者:jingxian 如题所说,计算两个日期之前的天数,排除节 ...

  4. 周末不用过来了,好好休息吧_如何好好休息

    周末不用过来了,好好休息吧 When I wrote about my productive routine in a previous article, I said I'd work for 1. ...

  5. xebium周末启动_我如何在周末建立和启动聊天机器人

    xebium周末启动 by Mike Williams 由Mike Williams 我如何在周末建立和启动聊天机器人 (How I Built And Launched A Chatbot Over ...

  6. C#/.Net判断是否为周末/节假日

    判断节假日请求的Api:http://tool.bitefu.net/jiari/ /// <summary>/// 判断是不是周末/节假日/// </summary>/// ...

  7. 开心周末:这就是2018年的我~

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 本文转载于公众号:IT平头哥联盟 大家好,今天周五,明天就是周末,再过几天也就是2019,2018 ...

  8. 05后都上清华了!首批丘成桐数学领军人才名单发布,三位菲尔兹奖得主为其授课,周末就来学校报到...

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 金磊 杨净 发自 凹非寺 量子位 报道 | 公众号 QbitAI 不 ...

  9. 力科示波器 matlab,力科周末文章四周年180期合集目录

    力科周末文章四周年180期合集 一.示波器基础系列 01 关于示波器的带宽-Frankie 01-1calibrating scopes 02 采样率和存储深度-Rick 03 DSO中的内插技术-P ...

最新文章

  1. SQL 交集 差集 并集 笛卡尔积 应用实例
  2. Swoole练习 Web
  3. Visual Studio Code常用快捷键
  4. 查询数据库中字段内容相同的记录
  5. FZU 2129 子序列个数 (递推dp)
  6. gitlab创建分支上传文件_环境搭建:gitLab平台的搭建和简单使用
  7. cmockery库详解
  8. Qt基于FFmpeg解码本地视频生成RGB数据
  9. Docker 配置加速器
  10. GitHub和75亿美金
  11. 崔希凡-javaWeb-笔记day07-day09(2016年7月26日23:17:27)
  12. 计算机初中毕业好学吗,初中毕业学计算机好学吗?
  13. [BT_Books]《无线蓝牙技术深入探讨》笔记
  14. Java中的gvm_深入浅出GVM之GC
  15. JS获取指定日期前后N天的日期、前N个月日期、后N个月日期
  16. 360°全景影像建库流程
  17. 手动生成MyEclipse注册码及序列号(源代码)
  18. Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment3
  19. 斗图高手教你用Python批量爬取表情包
  20. 西电“可展开天线”项目获2013年度国家科学技术进步二等奖

热门文章

  1. Unity VRPanorama 360 PRO Renderer 简单使用小记
  2. python基本数据类型2
  3. amd核芯显卡控制面板自定义分辨率_显卡天梯图2020最新版 2020年5月显卡排行榜天梯图...
  4. 【3DMAX】教你如何在中秋建模出一个好看的月亮(最详细3DMAX教程)
  5. hibernate帮助文档
  6. JavaWeb——超链接及表格练习题
  7. [Game Framework之StarForce解读]01.StarForce下载运行
  8. https及ca证书
  9. 相关噪声 matlab,噪声相关笔记
  10. 弘辽科技:抖音平台如何养新号、老号。