【转载】Torch7 教程 Supervised Learning CNN

Torch7 教程 Supervised Learning CNN

分类:             机器学习              2014-08-08 15:59     1426人阅读     评论(0)     收藏     举报

cnnbpdeep learning

全部代码放在:https://github.com/guoyilin/CNN_Torch7

在搭建好Torch7之后,我们开始进行监督式Supervised Learning for CNN, Torch7提供了代码和一些说明文件:

http://code.madbits.com/wiki/doku.php?id=tutorial_supervised_1_data 和http://torch.cogbits.com/doc/tutorials_supervised/说的比较详细。

结合http://ufldl.stanford.edu/wiki/index.php/Feature_extraction_using_convolution了解CNN的做法,最关键的是要熟悉http://ufldl.stanford.edu/wiki/index.php/Backpropagation_Algorithm 算法的主要做法。bp算法的目的是为了一次性计算所有的参数导数,该算法利用了chain rule进行error的后向传播。这篇文章写了bp算法: http://neuralnetworksanddeeplearning.com/chap2.html,  写的比较详细。

如果背景不熟悉,可以看看Linear Classification, Neutral Network, SGD算法。

由于该教程使用了torch自己的数据格式,因此如果你要使用自己的数据,需要预先转换下。这里我训练的是图像分类,因此可以使用

https://github.com/clementfarabet/graphicsmagick 进行数据的加载。

如下是加载图像的代码:

[plain] view plaincopyprint?

  1. height = 200
  2. width = 200
  3. --see if the file exists
  4. function file_exists(file)
  5. local f = io.open(file, "rb")
  6. if f then f:close() end
  7. return f ~= nil
  8. end
  9. function read_file (file)
  10. if not file_exists(file) then return {} end
  11. lines = {}
  12. for line in io.lines(file) do
  13. lines[#lines + 1] = line
  14. end
  15. return lines
  16. end
  17. -- read all label name. hash them to id.
  18. labels_id = {}
  19. label_lines = read_file(‘labels.txt‘)
  20. for i = 1, #label_lines do
  21. labels_id[label_lines[i]] = i
  22. end
  23. -- read train data. iterate train.txt
  24. local train_lines = read_file("train.txt")
  25. local train_features = torch.Tensor(#train_lines, 3, height, width) -- dimension: sample number, YUV, height, width
  26. local train_labels = torch.Tensor(#train_lines) -- dimension: sample number
  27. for i = 1, #train_lines do
  28. local image = gm.Image("/train_images/" .. train_lines[i])
  29. image:size(width, height)
  30. img_yuv = image:toTensor(‘float‘, ‘YUV‘, ‘DHW‘)
  31. --print(img_yuv:size())
  32. --print(img_yuv:size())
  33. train_features[i] = img_yuv
  34. local label_name = train_lines[i]:match("([^,]+)/([^,]+)")
  35. train_labels[i] = labels_id[label_name]
  36. --print(train_labels[i])
  37. if(i % 100 == 0) then
  38. print("train data: " .. i)
  39. end
  40. end
  41. trainData = {
  42. data = train_features:transpose(3,4),
  43. labels = train_labels,
  44. --size = function() return #train_lines end
  45. size = function() return #train_lines end
  46. }
  47. -- read test data. iterate test.txt
  48. local test_lines = read_file("test.txt")
  49. local test_features = torch.Tensor(#test_lines, 3, height, width) -- dimension: sample number, YUV, height, width
  50. local test_labels = torch.Tensor(#test_lines) -- dimension: sample number
  51. for i = 1, #test_lines do
  52. -- if image size is zero, gm.Imge may throw error, we need to dispose it later.
  53. local image = gm.Image("test_images/" .. test_lines[i])
  54. --print(test_lines[i])
  55. image:size(width, height)
  56. local img_yuv = image:toTensor(‘float‘, ‘YUV‘, ‘DHW‘)
  57. --print(img_yuv:size())
  58. test_features[i] = img_yuv
  59. local label_name = test_lines[i]:match("([^,]+)/([^,]+)")
  60. test_labels[i] = labels_id[label_name]
  61. --print(test_labels[i])
  62. if(i % 100 == 0) then
  63. print("test data: " .. i)
  64. end
  65. end
  66. testData = {
  67. data = test_features:transpose(3,4),
  68. labels = test_labels,
  69. --size = function() return #test_lines end
  70. size = function() return #test_lines end
  71. }
  72. trsize = #train_lines
  73. tesize = #test_lines
height = 200
width = 200
--see if the file exists
function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

function read_file (file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    lines[#lines + 1] = line
  end
  return lines
end

-- read all label name. hash them to id.
labels_id = {}
label_lines = read_file(‘labels.txt‘)
for i = 1, #label_lines do
  labels_id[label_lines[i]] = i
end

-- read train data. iterate train.txt

local train_lines = read_file("train.txt")
local train_features = torch.Tensor(#train_lines, 3, height, width) -- dimension: sample number, YUV, height, width
local train_labels = torch.Tensor(#train_lines) -- dimension: sample number

for i = 1, #train_lines do
  local image = gm.Image("/train_images/" .. train_lines[i])
  image:size(width, height)
  img_yuv = image:toTensor(‘float‘, ‘YUV‘, ‘DHW‘)
  --print(img_yuv:size())
  --print(img_yuv:size())
  train_features[i] = img_yuv
  local label_name = train_lines[i]:match("([^,]+)/([^,]+)")
  train_labels[i] = labels_id[label_name]
  --print(train_labels[i])
  if(i % 100 == 0) then
    print("train data: " .. i)
  end
end

trainData = {
  data = train_features:transpose(3,4),
  labels = train_labels,
  --size = function() return #train_lines end
  size = function() return #train_lines end
}

-- read test data. iterate test.txt
local test_lines = read_file("test.txt")

local test_features = torch.Tensor(#test_lines, 3, height, width) -- dimension: sample number, YUV, height, width
local test_labels = torch.Tensor(#test_lines) -- dimension: sample number

for i = 1, #test_lines do
  -- if image size is zero, gm.Imge may throw error, we need to dispose it later.
  local image = gm.Image("test_images/" .. test_lines[i])
  --print(test_lines[i])

  image:size(width, height)
  local img_yuv = image:toTensor(‘float‘, ‘YUV‘, ‘DHW‘)
  --print(img_yuv:size())
  test_features[i] = img_yuv
  local label_name = test_lines[i]:match("([^,]+)/([^,]+)")
  test_labels[i] = labels_id[label_name]
  --print(test_labels[i])
  if(i % 100 == 0) then
    print("test data: " .. i)
  end
end

testData = {
  data = test_features:transpose(3,4),
  labels = test_labels,
  --size = function() return #test_lines end
  size = function() return #test_lines end
}
trsize = #train_lines
tesize = #test_lines

由于图像的大小从32*32变成了200*200, 因此需要修改相应的model中的每一层的大小。

假定其他层没有变化,最后一层需要修改:

[plain] view plaincopyprint?

  1. -- stage 3 : standard 2-layer neural network
  2. model:add(nn.Reshape(nstates[2]*47*47))
  3. model:add(nn.Linear(nstates[2]*47*47, nstates[3]))
  4. model:add(nn.Tanh())
  5. model:add(nn.Linear(nstates[3], noutputs))
时间: 2024-10-07 22:52:28

【转载】Torch7 教程 Supervised Learning CNN的相关文章

(转载)[机器学习] Coursera ML笔记 - 监督学习(Supervised Learning) - Representation

[机器学习] Coursera ML笔记 - 监督学习(Supervised Learning) - Representation http://blog.csdn.net/walilk/article/details/50922854

Supervised Learning 的本质

转载自知乎:http://www.zhihu.com/question/23194489   但根据知乎惯例,答案还是要继续扩展的. 首先看什么是学习(learning)?一个成语就可概括:举一反三.此处以高考为例,高考的题目在上考场前我们未必做过,但在高中三年我们做过很多很多题目,懂解题方法,因此考场上面对陌生问题也可以算出答案.机器学习的思路也类似:我们能不能利用一些训练数据(已经做过的题),使机器能够利用它们(解题方法)分析未知数据(高考的题目)? 最简单也最普遍的一类机器学习算法就是分类

Stanford机器学习课程笔记(1) Supervised Learning and Unsupervised Learning

最近跟完了Andrew Ng的Machine Learning前三周的课,主要讲解了机器学习中的线性回归(Linear Regression)和逻辑回归(Logistic Regression)模型.在这里做一下记录. 另外推荐一本统计学习的书,<统计学习方法>李航,书短小精悍,才200多页,但是内容基本上覆盖了机器学习中的理论基础. 笔记<1> 主要了解一下监督学习和无监督学习 机器学习:是关于计算机基于数据 构建概率统计模型 并运用模型对数据进行预测与分析的一门学科. 机器学习

1. Supervised Learning - Linear Regression

Linear Regression线性回归 Notation 给定一个样本集T 样本总数为m 每个样本记做 其中为输入变量,也称为特征变量:为我们要预测的输出变量,也称为目标变量 表示第个样本. 问题描述 给定一个样本集,学习一个函数 使得是对相应y的一个好的预测. 因为某些历史原因,h被称为假设(hypothesis). 整个过程如下图所示: 如果我们想要预测的目标变量是连续值,称为回归问题(regression): 当目标变量是少数离散值时,称为分类问题(classification). 如

2. Supervised Learning - Logistic Regression

Logistic Regression 逻辑回归解决问题类型 二分类问题(classification) Notation 给定一个样本集T 样本总数为m 每个样本记做 其中为输入变量,也称为特征变量:为我们要预测的输出变量,也称为目标变量 表示第个样本. Hypothesis的作用是,对于给定的输入变量,根据选择的参数计算输出变量=1的可能性 也就是 最终,当大于等于0.5时,预测y=1,当小于0.5时,预测y=0 假设是一下形式: 其中称为Logistic函数或者sigmoid函数,函数图象

A Brief Review of Supervised Learning

There are a number of algorithms that are typically used for system identification, adaptive control, adaptive signal processing, and machine learning. These algorithms all have particular similarities and differences. However, they all need to proce

【转载】Distributed Deep Learning on MPP and Hadoop

Distributed Deep Learning on MPP and Hadoop DECEMBER 17, 2014 | FEATURES | BY REGUNATHAN RADHAKRISHNAN Joint work performed by Regunathan Radhakrishnan, Gautam Muralidhar, Ailey Crow, and Sarah Aerni of Pivotal’s Data Science Labs. Deep learning grea

【转载】Project on Learning Deep Belief Nets

Project on Learning Deep Belief Nets Deep Belief Nets (DBN's) will be explained in the lecture on Oct 29. Instead of learning layers of features by backpropagating errors, they learn one layer at a time by trying to build a generative model of the da

Machine Learning Algorithms Study Notes(2)--Supervised Learning

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知错能改"演算法    4 2.2    Linear Regression    6 2.2.1    线性回归模型    6 2.2.2    最小二乘法( le