some logs

nothing

installing file without sodu:

1
2
3
4
5
6
wget ftp://ftp.cwru.edu/pub/bash/readline-6.3.tar.gz
http://download.zeromq.org/zeromq-4.0.3.tar.gz
tar xfz zeromq-4.0.3.tar.gz
cd zeromq-4.0.3/
./configure --prefix=~/local
make install
1
2
3
4
5
6
7
8
9
10
11
12
# Install zmq library
#-------------------
export LD_LIBRARY_PATH=~/local/lib/
export PKG_CONFIG_PATH=~/local/lib/pkgconfig/
export C_INCLUDE_PATH=$C_INCLUDE_PATH:~/usr/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:~/usr/include
When building packages, use ./configure --prefix=/home/<username>/usr

# in some OS's you may need this too:
export CXXFLAGS="-I ~/local/include" // solve the problem of headfile not found

export LDFLAGS="-L ~/local/lib/ -Wl,-rpath=~/local/lib/"

LD_LIBRARY_PATH

source .bashrc after setting the path, update the file

git clone https://github.com/facebook/iTorch.git

1
2
3
4
5
6
// install zmq
luarocks install lzmq ZMQ_DIR=~/.npm-packages/etc/node_modules/zmq/windows/
// install ipython
luarocks make --local ZMQ_DIR=~/.npm-packages/etc/node_modules/zmq/windows/

luarocks install --local loadcaffe

neural_style

h neural_style.lua -style_image ./examples/inputs/the_scream.jpg -content_image ./examples/inputs/frida_kahlo.jpg

apt-get install p7zip-full
dpkg -i –force-not-root –root=$HOME package.deb

PATH=$PATH:~/Support/7z/p7zip-9.20.1~dfsg.1/bin

wget -x –load-cookies cookies.txt https://www.kaggle.com/c/cifar-10/download/test.7z

in lua add package path

1
package.path = package.path .. ";C:\\Users\\Me\\MyLuaProject"

count the number of file

ls -1 targetdir | wc -l
ls -1 . | wc -l

cifar.torch

https://github.com/szagoruyko/cifar.torch

data processing:

1
2
3
4
5
OMP_NUM_THREADS=2 
th -i provider.lua
provider = Provider()
provider:normalize()
torch.save('provider.t7',provider)

training

1
2
3
CUDA_VISIBLE_DEVICES=0 
th train.lua --model vgg_bn_drop_simple -s logs/vgg_simple
th train.lua --model cnnft_net -s logs/0112
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
debug: 	
torch.FloatTensor
torch.FloatTensor
/home/xizeng/torch/install/bin/luajit: ...xizeng/.luarocks/share/lua/5.1/nn/SpatialConvolution.lua:100: bad argument #1 (field finput is not a torch.FloatTensor)
stack traceback:
[C]: in function 'SpatialConvolutionMM_updateOutput'
...xizeng/.luarocks/share/lua/5.1/nn/SpatialConvolution.lua:100: in function 'updateOutput'
/home/xizeng/.luarocks/share/lua/5.1/nn/Sequential.lua:44: in function 'updateOutput'
/home/xizeng/.luarocks/share/lua/5.1/nn/Sequential.lua:44: in function 'forward'
train.lua:110: in function 'opfunc'
/home/xizeng/torch/install/share/lua/5.1/optim/sgd.lua:44: in function 'sgd'
train.lua:119: in function 'train'
train.lua:194: in main chunk
[C]: in function 'dofile'
...zeng/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk
[C]: at 0x00405ab0

solv: cast the inputs model criterion into Float by

1
2
3
model = model: float()
inputs = inputs: float()
criterion = inputs: float()

untar:

1
tar -xvf public_html-14-09-12.tar

screen command

1
2
3
4
5
6
#screen <run something>

// come back:
# screen -ls
# screen -r seq
// screen接收以C-a开始的命令

layer1 Conv1:
kernel :32 ,kernel size : 5 , activation : relu ,dropout : 0.25
layer2 Conv2:
kernel: 32,kernel size: 5, activation : relu ,dropout : 0.25
layer3 MaxPooling1: poolsize:2
layer4 Conv3:
kernel: 64,kernel size: 3, activation : relu ,dropout : 0.25
layer5 MaxPooling2: poolsize:2
layer6: full connect 512 ,activation :tanh
layer7: softmax

http://www.bubuko.com/infodetail-1066303.html

##
find library
ldconfig -p | grep libjpeg

environment variable affecting GCC

https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html

LD_LIBRARY_PATH for own shared object file

http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

setting shell configure

in $HOME/.profile run
. /home/x…. activate file

in which export the environment variable and append new variable to it

install blas

1
2
3
4
mv make.inc.example make.inc
make

gfortran: error: ../../librefblas.a: No such file or directory

reason: in Makefile, the installation of blaslib is comment, uncomment it will can solve

install torch without sodu

./install.sh PREFIX=/home/usrname/local

install lapack and openblas

1
for openblas, set NO_LAPACK=0

1
2
3
export CMAKE_LIBRARY_PATH=/path/to/lapack-3.4.0:$CMAKE_LIBRARY_PATH 

luarocks install torch

ERROR lapack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
u/torch/install/bin/luajit: syev : Lapack library not found in compile time
at /tmp/luarocks_torch-scm-1-5326/torch7/lib/TH/generic/THLapack.c:68
stack traceback:
[C]: at 0x7ff39fdc7930
[C]: in function 'symeig'
/home/ee532_stu/cifar10_handsome/lib/preprocessing.lua:16: in function 'pcacov'
/home/ee532_stu/cifar10_handsome/lib/preprocessing.lua:32: in function 'zca_whiten'
/home/ee532_stu/cifar10_handsome/lib/preprocessing.lua:74: in function 'zca'
/home/ee532_stu/cifar10_handsome/lib/preprocessing.lua:92: in function 'preprocessing'
validate.lua:63: in function 'validation'
validate.lua:85: in main chunk
[C]: in function 'dofile'
..._stu/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:131: in main chunk
[C]: at 0x00406670

solve: install openblas and lapack, re install torch

1
2
3
4
INSTALL OPENBLAS:

make NO_LAPACK=0 USE_OPENMP=1
make PREFIX=/PATH/TO/OPENBLAS install

error:

1
2
OpenBLAS Warning : Detect OpenMP Loop and this application may hang. Please rebuild the library with USE_OPENMP=1 option.
// when running train.lua

solution: export OMP_NUM_THREADS=1

error:

kaggle: size mismatch
cifar-10: cur-target less than 0

torch syntax

1
2
3
4
5
6
7
8
-- for torch.tensor
a:size()

-- nn.Sequential, type: <table>
a = medel.modules[1]
// if more than one module in the image

a.output // print the output of layer 1

parallel table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
th> para2 = nn.ParallelTable()
[0.0002s]
th> para2
nn.ParallelTable {
input
... -> output
}
[0.0001s]
th> para2:add(nn.Linear(3,1))
nn.ParallelTable {
input
|`-> (1): nn.Linear(3 -> 1)
... -> output
}
[0.0004s]
th> para2:add(nn.Linear(4,1))
nn.ParallelTable {
input
|`-> (1): nn.Linear(3 -> 1)
|`-> (2): nn.Linear(4 -> 1)
... -> output
}
[0.0004s]
th> para2:forward{torch.Tensor(3),torch.Tensor(4)}
{
1 : DoubleTensor - size: 1
2 : DoubleTensor - size: 1
}

split table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
th> para3:forward(torch.Tensor(4,1))
{
1 : DoubleTensor - size: 4
}
[0.0004s]
th> para3:forward(torch.Tensor(4,2))
{
1 : DoubleTensor - size: 4
2 : DoubleTensor - size: 4
}
[0.0006s]
th> para3:forward(torch.Tensor(4,4))
{
1 : DoubleTensor - size: 4
2 : DoubleTensor - size: 4
3 : DoubleTensor - size: 4
4 : DoubleTensor - size: 4
}
[0.0004s]
th> para3:forward(torch.Tensor(3,3))
{
1 : DoubleTensor - size: 3
2 : DoubleTensor - size: 3
3 : DoubleTensor - size: 3
}
[0.0005s]
th> para3 = nn.SplitTable(2)

combine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
mlp=nn.Sequential();       --Create a network that takes a Tensor as input
mlp:add(nn.SplitTable(2))
c=nn.ParallelTable() --The two Tensors go through two different Linear
c:add(nn.Linear(10,3)) --Layers in Parallel
c:add(nn.Linear(10,7))
mlp:add(c) --Outputing a table with 2 elements
p=nn.ParallelTable() --These tables go through two more linear layers
p:add(nn.Linear(3,2)) -- separately.
p:add(nn.Linear(7,1))
mlp:add(p)
mlp:add(nn.JoinTable(1)) --Finally, the tables are joined together and output.

th> mlp:forward(torch.Tensor(10,2))
-0.3382
0.3077
-0.1244
[torch.DoubleTensor of size 3]

[0.0011s]
th> mlp
nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> output]
(1): nn.SplitTable
(2): nn.ParallelTable {
input
|`-> (1): nn.Linear(10 -> 3)
|`-> (2): nn.Linear(10 -> 7)
... -> output
}
(3): nn.ParallelTable {
input
|`-> (1): nn.Linear(3 -> 2)
|`-> (2): nn.Linear(7 -> 1)
... -> output
}
(4): nn.JoinTable
}
[0.0004s]

net operation - read para and output

1
2
3
4
net ... def

net:getParameters()
net:parameters()

the simplest case: linear regression:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
th> linear
nn.Linear(3 -> 1)
[0.0001s
th> linear:forward(torch.Tensor(3):fill(2))
0.7441
[torch.DoubleTensor of size 1]

[0.0005s]
th> linear:parameters()
{ -- parameters
1 : DoubleTensor - size: 1x3
2 : DoubleTensor - size: 1
}
{ -- gradParameters
1 : DoubleTensor - size: 1x3 -- gradWeights
2 : DoubleTensor - size: 1 -- gradBias
}
[0.0004s]
th> linear:getParameters()
-- flatten parameters:
-0.1055
0.1763
0.1545
0.2935
[torch.DoubleTensor of size 4]

-- flatten grasParameters: grad wrt to bias and weights
2.6499e+180
4.9592e+92
1.7894e+161
1.2806e+213
[torch.DoubleTensor of size 4]

[0.0017s]
th> linear.output
0.7441
[torch.DoubleTensor of size 1]

tensor get col:

1
2
3
4
input = torch.Tensor(8,20) -- 8 rows, 20 cols
input[1] -- return first row
input[{{}, 5}] -- return the 5th col
input[{{}, {4,8}}}] -- return the 4,5,6,7,8 col

import csv

1
2
3
4
5
6
7
8
9
10
11
12
import csv
text_file = open("label.txt", "w")
with open('label.csv' ,'rb') as csvfile:
reader = csv.reader(csvfile)
i = 1
for row in reader:
name = 'imgs/train{:05d}.jpg'.format(i)
label = row[0]
text_file.writelines('%s %s \n' % (name, label))
i = i + 1

text_file.close()

minibatch SGD

cost function:
training + testing
parameter updating
gradient descent

训练过程有训练步数(n_epoch)的设置,

每个epoch会遍历所有的训练数据(training_set),本程序中也就是320个人脸图。
n_epochs训练步数,每一步都会遍历所有batch,即所有样本

batch_size,这里设置为500,即每遍历完500个样本,才计算梯度并更新参数

还有迭代次数iter,一次迭代遍历一个batch里的所有样本,具体为多少要看所设置的batch_size。

learning rate

设得太大的话算法可能永远都优化不了,设得太小会使算法优化得太慢,而且可能还会掉入局部最优。
1) error 一直停在某个值, 可能是学习速率太大, 跳过最优

how to save parameter in torch

input shape(batchsize, depth, w, h)

SpatialConvolution(depth_origin, depth_update, kh, kw, steph, stepw, padh, pahw)

1
2
3
4
5
6
7
8
9
nn:add
nn:remove(2)
nn:insert(nn.Spa..., 2)

-- info of the trained network
conv_nodes = model:findModules('nn.SpatialConvolution')
for i = 1, #conv_nodes do
print(conv_nodes[i].output:size())
end

view is reshape

linear linear transform form R^m to R^n
can be used at the final layer of classification, tranform to num or categories

1
2
3
4
5
-- return maximun of 1d tensor p, and its index
maxi, index = torch.max(p, 1)

-- if p is (3x5) then return maxi of each cols
maxi is 1x5 and index is 1x5
1
2
-- lua
table.getn(c)
1
2
3
4
5
6
7
8
// c library function:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

size: bytes of each element
nmemb: number of elements:
in total: size*nmemb bytes

stream: pointer to FILE
1
2
3
4
th> a = torch.data(tensor)

th> a
cdata<double *>: 0x022bebe0

read csv in lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local function split(str, sep)
sep = sep or ','
fields={}
local matchfunc = string.gmatch(str, "([^"..sep.."]+)")
if not matchfunc then return {str} end
for str in matchfunc do
table.insert(fields, str)
end
return fields
end

---------------------------------------------------------------------
function read(path, sep, tonum)
tonum = tonum or true
sep = sep or ','
local csvFile = {}
local file = assert(io.open(path, "r"))
for line in file:lines() do
fields = split(line, sep)
if tonum then -- convert numeric fields to numbers
for i=1,#fields do
fields[i] = tonumber(fields[i]) or fields[i]
end
end
table.insert(csvFile, fields)
end
file:close()
return csvFile
end

http://nocurve.com/simple-csv-read-and-write-using-lua/

batch size 20: all 3

torch load class

1
dofile '...lua' -- load the class defined in the file