2013/11/16

bash で二重に trap

Q. 最初のスクリプトでtrapを呼び、そこから呼び出したスクリプト内でもtrapを呼ぶとどうなるのか?

A. 両方が呼び出される。以下、実験結果。

[test.sh]
#!/bin/bash
trap "echo xxx" 2
./sub.sh
sleep 5

[sub.sh]
#!/bin/bash
trap "echo yyy" 2
sleep 5

test.shを実行して、5秒以内にCtrl+Cを押すと、端末に
^Cyyy
xxx
が表示される。

5秒から10秒の間にCtrl+Cを押すと、端末に
^Cxxx
が表示される。



ここにtrapの解説がある。
http://shellscript.sunone.me/signal_and_trap.html

2013/10/19

perl で連想配列に配列を記録する方法

いつも忘れるのでメモ。

%x2y = ();
# 読み込み
while(<>)
{
    $_ =~ s/[\r\n]//g;   # 改行除去
    @c = split / /;   # スペースで分割
    push(@{$x2y{$c[0]}}, $c[1]); # キーが$c[0]の配列に$c[1]を追加
}
# 表示
while(($k,$v)=each(%x2y))
{
    print $k." --> ".join(", ",@$v)."\n";
}

このスクリプトに標準入力から

a a
b b
b c
c d
c e
c f

を入力すると、

c --> d, e, f
a --> a
b --> b, c

が得られる。

Brzozowski さんの読み方

http://d.hatena.ne.jp/m-hiyama/20131017/1381998987
によると、「ゾゾウスキイ」とのこと。

しかしよくよく音声を
http://ja.forvo.com/word/brzozowski/
で聞いてみると最初のzが発音されず、

ブロゾウスキー
プロゾウスキー
ブロゾフスキー
プロゾフスキー

のどれかに近いように聞こえる。

「ゾ」にアクセントをおいて発音すると
forvoにある音声と似たような発音ができそうだ。

2013/05/30

読めない苗字

こんな苗字を見つけた。

Ljolje

ここにたくさんある。
http://www.linkedin.com/pub/dir/+/Ljolje

どうやらクロアチア語圏かそのあたりの苗字のようだ。

http://croatia.kororo.jp/lang/01accent.html
によると、Ljは「リュ」の音が近いようなので、
仮にクロアチア語だとしてカタカナ読みをすると、

「リョーリュェ」とか「リョーレ」

と読めばいいのだろうか。

2013/04/21

RVOと右辺値参照

C++11のお話。

RVO(Return Value Optimization)と右辺値参照が競合する場合は、
RVOが優先される。

#include <iostream>
class test
{
public:
  test(void) : val(0)
  {
    std::cout << "ctor with no args" << std::endl;
  }
  ~test(void)
  {
    std::cout << "dtor : " << val << std::endl;
  }
  test(int a) : val(a)
  {
    std::cout << "ctor : " << a << std::endl;
  }
  test(const test &) = delete; // undef copy constructor
  test(test &&a) : val(a.val)
  {
    std::cout << "move ctor : " << val << std::endl;
    a.val = -val;
  }
  const test& operator=(test &&a)
  {
    std::cout << "move oper= " << a.val << std::endl;
    val = a.val;
    a.val = -val;
    return *this;
  }
  
private:
  int val;
};

test ret_test(int a)
{
  test t(a);
  return t;
}

int main(void)
{
  {
    test t1 = ret_test(1); // RVO
    t1 = ret_test(2); // rvalue reference
  }
  std::cout << "---" << std::endl;
  {
    test t3 = std::move(ret_test(3)); // rvalue reference by std::move
  }
  return 0;
}



処理した結果は次のようになる。g++ ver.4.7.2 と Debian clang version 3.0-6.2
のどちらでも同じ結果となった。

ctor : 1
ctor : 2
move oper= 2
dtor : -2
dtor : 2
---
ctor : 3
move ctor : 3
dtor : -3
dtor : 3

2013/03/25

LibreOffice で条件付き書式

条件付き書式で数値ではなく文字列を指定するには、

[セルの値が] [次の値に等しい] "ABC"

のようにすればよい。この例の場合、セルの値がABCの場合に
条件を満たす。指定したい文字列を " で括る必要がある。
' で括っても動作しないし、単に文字列を書いただけでも動作しない。

2013/02/08

[Python] numpy の行列

numpy の行列の各要素へのアクセス方法をいろいろと試した。
まず、numpyを読み込んで、npという名前でアクセスできるようにする。

>>> import numpy as np
さらに、行列Mを作成する。
>>> M=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
1行目1列目が1、1行目2列目が2である。


2行目を表示する。
>>> print M[1]
[[4 5 6]]

2行目3列目の値を表示する。
>>> print M[1,2]
6

以降、i行列j列目を(i,j)と書くこととする。

(3,2)と(1,2)の成分を取り出す。
>>> print M[[2,0],[1,1]]
[[8 2]]
(3,1)と(2,2)の成分を取り出すわけではないことに注意。

(1,3)と(2,2)、(3,1)の成分を取り出す。
>>> print M[[0,1,2],[2,1,0]]
[[3 5 7]]

(2,3)と(2,2)、(2,1)の成分を取り出す。
>>> print M[[1,1,1],[2,1,0]]
[[6 5 4]]
省略して書くこともできる。
>>> print M[1,[2,1,0]]
[[6 5 4]]
これも同様。
>>> print M[[1],[2,1,0]]
[[6 5 4]]

次の書き方はエラーとなる。
>>> print M[[1,2],[2,1,0]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/numpy/matrixlib/defmatrix.py", line 305, in __getitem__
    out = N.ndarray.__getitem__(self, index)
ValueError: shape mismatch: objects cannot be broadcast to a single shape

(3,2)と(2,2)、(1,2)の成分を取り出す。
>>> print M[[2,1,0],[1,1,1]]
[[8 5 2]]

同様に省略できるが、形式が変わる。
>>> print M[[2,1,0],1]
[[8]
 [5]
 [2]]

次の3つはすべて同じ結果となる。
>>> print M[[2],1]
[[8]]
>>> print M[[2],[1]]
[[8]]
>>> print M[2,[1]]
[[8]]

しかし、[]が[]内にないと、スカラーの値が表示される。
>>> print M[2,1]
8

次の2つは同じ。
>>> print M[np.arange(3),np.arange(3)]
[[1 5 9]]
>>> print M[[0,1,2],[0,1,2]]
[[1 5 9]]

1行目と2行目を取り出す。
>>> print M[[0,1]]
[[1 2 3]
 [4 5 6]]

転置をとる。
>>> print M.T
[[1 4 7]
 [2 5 8]
 [3 6 9]]

2013/02/01

mnist.pkl.gz の読み込み方

http://deeplearning.net/tutorial/gettingstarted.html
に記載されている MNIST Dataset
http://deeplearning.net/data/mnist/mnist.pkl.gz
のデータ構造は次のようになっている。

[mnist.pkl] = [[train_set], [valid_set], [test_set]]
[*_set] = [[*_setの入力], [*_setの正解]]
[*_setの入力] = [[784個の浮動小数点の配列], ... ]
[*_setの正解] = [[0〜9の整数], ... ]

ここで、[784個の浮動小数点の配列]は28x28の手書き数字画像、
*_setはtrain_set、valid_set、test_setのいずれかである。

読み込んで端末に表示してみるコード(load.py)は次の通り。

import cPickle, gzip, numpy, sys
if len(sys.argv) != 2:
  quit()
data_index=int(sys.argv[1])
f=gzip.open('../data/mnist.pkl.gz','rb')
train_set, valid_set, test_set=cPickle.load(f)
train_set_x, train_set_y=train_set # setの中身が
for i in range(data_index,data_index+1):
  for y in range(0,28):
    for x in range(0,28):
      if train_set_x[i][y*28+x]<0.5:
        sys.stdout.write(" ")
      elif train_set_x[i][y*28+x]<0.8:
        sys.stdout.write("+")
      else:
        sys.stdout.write("*")
    sys.stdout.write("\n")
  print "correct =",train_set_y[i]
  print "--------------------------------"


8番目のデータを表示する実行例は次の通り。
$ python load.py 7





              ******+
          +**********+
         +************
          ***+++++****
                  ***+
                  ***+
                 +***
               *****+
           +++*****
         *********+
         **********
                +**
                 **
                 **
                ***
      +*       ****
     ***++++++***+
     ***********+
      +*******+
        **+



correct = 3
--------------------------------
データを読めていることが確認できる。

2013/01/30

[Python] theano.tensor.grad

theano.tensor で書いた関数を自動的に微分することができる。
ただし、関数で計算した結果得られる値はスカラーでなければならない。
ベクトルや行列になっているとエラーとなる。

gradを使った例を次に示す。

import numpy
import theano
import theano.tensor as T

x=T.dscalar('x')
v=T.dvector('v')
M=T.dmatrix('M')

y1=x**3
y2=T.dot(v,v)*x
y3=T.sum(T.dot(M,M))

gy1=T.grad(y1,x)
gy2=T.grad(y2,v)
gy3=T.grad(y3,M)

f1=theano.function([x],gy1)
f2=theano.function([x,v],gy2)
f3=theano.function([M],gy3)

print "f1  :",theano.pp(f1.maker.fgraph.outputs[0])
print "f2  :",theano.pp(f2.maker.fgraph.outputs[0])
print "f3  :",theano.pp(f3.maker.fgraph.outputs[0])

print "f1(2) =",f1(2)
print "f2(2,[3,5]) =",f2(2,[3,5])
print "f3([[1,2],[3,4]]) =",f3([[1,2],[3,4]])

実行すると、
f1  : Elemwise{Composite{[mul(i0, sqr(i1))]}}(TensorConstant{3.0}, x)
f2  : Elemwise{Composite{[add(*1 -> mul(i0, i1), *1)]}}(x, v)
f3  : gemm_inplace(_dot22(alloc(TensorConstant{(1, 1) of 1.0}, Shape_i{0}(M), Shape_i{1}(M)), M.T), TensorConstant{1.0},
 M.T, alloc(TensorConstant{(1, 1) of 1.0}, Shape_i{0}(M), Shape_i{1}(M)), TensorConstant{1.0})
f1(2) = 12.0
f2(2,[3,5]) = [ 12.  20.]
f3([[1,2],[3,4]]) = [[  7.  11.]
 [  9.  13.]]
が得られる。f1 の出力を見ると、mul が掛け算で sqr が2乗、i0=TensorConstant{3.0}、
i1=x であるから、y1 を x で微分した結果である 3*x^2 が得られていることがわかる。
f2 と f3 は複雑すぎて読み方がわからないが、具体的な値を入れた結果は
正しく出力されている。



エラーが出る例として、ベクトルが戻り値になるような関数を試してみる。
import numpy
import theano
import theano.tensor as T

x=T.dscalar('x')
v=T.dvector('v')
y=v*x
gy=T.grad(y,x)
すると、
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    gy=T.grad(y,x)
  File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 411, in grad
    raise TypeError("cost must be a scalar.")
TypeError: cost must be a scalar.
といわれて微分できない。

2013/01/24

[Python] theano.tensor.fill

fill の挙動がわからないので調べてみた。

import numpy
import theano
import theano.tensor as T

x=T.dscalar('x')
xc=T.dscalar('xc')
y1=3*x
y2=xc*x
y3=T.fill(xc,3)*x

f1=theano.function([x],y1)
f2=theano.function([x,xc],y2)
f3=theano.function([x,xc],y3)

print "x=",theano.pp(x)
print "y1=",theano.pp(y1)
print "y2=",theano.pp(y2)
print "y3=",theano.pp(y3)
print "f1=",theano.pp(f1.maker.fgraph.outputs[0])
print "f2=",theano.pp(f2.maker.fgraph.outputs[0])
print "f3=",theano.pp(f3.maker.fgraph.outputs[0])
print "f1(2)=",f1(2)
print "f2(2,3)=",f2(2,3)
print "f3(2,3)=",f3(2,3)
print "f3(2,100)=",f3(2,100)

実行すると、
x= x
y1= (TensorConstant{3} * x)
y2= (xc * x)
y3= (fill(xc, TensorConstant{3}) * x)
f1= (TensorConstant{3.0} * x)
f2= (xc * x)
f3= (TensorConstant{3.0} * x)
f1(2)= 6.0
f2(2,3)= 6.0
f3(2,3)= 6.0
f3(2,100)= 6.0
が得られた。

f3(2,3)もf3(2,100)も同じ値を返していることから、
fill は第1引数を第2引数の値に固定した結果を返しているように見える。
何に使うのだろうか。

ところで、
f3=theano.function([x,xc],y3)
f3=theano.function([x],y3)
に置き換えると
theano.gof.fg.MissingInputError: ('An input of the graph, used to compute Elemwise{second,no_inplace}(xc, TensorConstant{3}), was not provided and not given a value', xc)
というエラーが出力される。
値が与えられてない変数が残っていると、たとえfillを使って値を与えていても
関数化するのは無理なようだ。

2013/01/20

流行のDNN

なんか、DNNが流行っているらしい。
というわけで、探してみると日本語の概要があった。
http://www.slideshare.net/mokemokechicken/pythondeep-learning
http://www.slideshare.net/tushuhei/121227deep-learning-iitsuka

Python+Theanoを使うと簡単なそうな。
実装例が
http://deeplearning.net/tutorial/
にあるので、まずは準備で、
http://deeplearning.net/tutorial/gettingstarted.html
に書いてあるとおり、githubからcloneでソースを取得する。

チュートリアルどおり、実験用の手書き数字画像としてMNIST datasetをダウンロードする。
gitで取ってきたなかの、
data/download.sh
を実行するとデータがダウンロードされる。

また、

$ cd doc
$ make
を実行すると チュートリアルのpdf版が html/deeplearning.pdf に作成される。

使い方はまだよく調べていないが、次のように実行すると、
とりあえずテスト実行できるようだ。
$ python
>>> import test
>>> test.speed()


実行した結果は、以下。一部省略。
-- logistic_sgd のログの一部(float64) --
epoch 30, minibatch 83/83, validation error 8.031250 %
     epoch 30, minibatch 83/83, test error of best model 7.843750 %
Optimization complete with best validation score of 8.031250 %,with test performance 7.843750 %
The code run for 30 epochs, with 1.299264 epochs/sec
The code for file logistic_sgd.pyc ran for 23.1s

-- logistic_cg のログの一部(float64) --
validation error 7.927083 %
Optimization complete with best validation score of 7.927083 %, with test performance 8.041667 %
The code for file logistic_cg.pyc ran for 53.4s

-- mlp のログの一部(float64) --
epoch 5, minibatch 2500/2500, validation error 7.300000 %
     epoch 5, minibatch 2500/2500, test error of best model 7.590000 %
Optimization complete. Best validation score of 7.300000 % obtained at iteration 14999, with test performance 7.590000 %
The code for file mlp.pyc ran for 2.88m

-- convolutional_mlp のログの一部(float64) --
epoch 5, minibatch 100/100, validation error 6.430000 %
     epoch 5, minibatch 100/100, test error of best model 6.920000 %
Optimization complete.
Best validation score of 6.430000 % obtained at iteration 599,with test performance 6.920000 %
The code for file convolutional_mlp.pyc ran for 2.26m

-- dA のログ(float64) --
... loading data
Training epoch 0, cost  63.2891694201
Training epoch 1, cost  55.7866565443
The no corruption code for file dA.pyc ran for 2.05m
Training epoch 0, cost  81.7714190632
Training epoch 1, cost  73.4285756365
The 30% corruption code for file dA.pyc ran for 2.06m

-- SdA のログ(float64) --
... loading data
... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  194.503144937
Pre-training layer 1, epoch 0, cost  695.507788509
Pre-training layer 2, epoch 0, cost  529.03645135
The pretraining code for file SdA.pyc ran for 6.89m
... getting the finetuning functions
... finetunning the model
epoch 0, minibatch 166/166, validation error 14.868687 %
     epoch 0, minibatch 166/166, test error of best model 15.727273 %
epoch 1, minibatch 166/166, validation error 11.595960 %
     epoch 1, minibatch 166/166, test error of best model 11.717172 %
Optimization complete with best validation score of 11.595960 %,with test performance 11.717172 %
The training code for file SdA.pyc ran for 6.70m

-- DBN のログ(float64) --
... loading data
... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  -165.566548661
Pre-training layer 1, epoch 0, cost  -620.030667461
Pre-training layer 2, epoch 0, cost  -133.876169806
The pretraining code for file DBN.pyc ran for 7.84m
... getting the finetuning functions
... finetunning the model
epoch 1, minibatch 166/166, validation error 28.373737 %
     epoch 1, minibatch 166/166, test error of best model 29.848485 %
epoch 2, minibatch 166/166, validation error 20.272727 %
     epoch 2, minibatch 166/166, test error of best model 21.424242 %
Optimization complete with best validation score of 20.272727 %,with test performance 21.424242 %
The fine tuning code for file DBN.pyc ran for -64.90m

-- rbm のログ(float64) --
... loading data
WARNING (theano.tensor.opt): Your current code is fine, but Theano versions prior to 0.5rc2 might have given an incorrect result. To disable this warning, set the Theano flag warn.subtensor_merge_bug to False.

Training epoch 0, cost is  -53.1045703345
Training took 17.727500 minutes
WARNING (theano.tensor.opt): Your current code is fine, but Theano versions prior to 0.5rc2 might have given an incorrect result. To disable this warning, set the Theano flag warn.subtensor_merge_bug to False.
 ... plotting sample  0
['logistic_sgd', 'logistic_cg', 'mlp', 'convolutional_mlp', 'dA', 'SdA', 'DBN', 'rbm']
float64 times [   24.00578618    55.60583591   175.75342607   136.53151321   248.71974111
   823.29348183   874.02775431  1079.12984514]
float64 expected [10.300000000000001, 23.699999999999999, 78.099999999999994, 73.700000000000003, 116.40000000000001, 346.89999999999998, 381.89999999999998, 558.10000000000002]
float64 % expected/get [ 0.42906322  0.42621426  0.44437256  0.53980212  0.46799663  0.42135643
  0.43694265  0.51717595]

-- float32の場合の最後の方のログ --
 ... plotting sample  0
['logistic_sgd', 'logistic_cg', 'mlp', 'convolutional_mlp', 'dA', 'SdA', 'DBN', 'rbm']
float32 times [   21.94568563    49.98527789   177.91128731   136.48723054   291.68081236
  1048.11308694  1128.05673623  1302.78659558]
float32 expected [11.6, 29.600000000000001, 47.200000000000003, 66.5, 71.0, 191.19999999999999, 226.80000000000001, 432.80000000000001]
float32 % expected/get [ 0.5285777   0.59217436  0.26530076  0.48722507  0.24341677  0.18242306
  0.20105372  0.33221097]
float64/float32 [ 1.09387269  1.11244427  0.98787114  1.00032445  0.85271204  0.78550062
  0.77480833  0.82832434]

Duplicate the timing to have everything in one place
['logistic_sgd', 'logistic_cg', 'mlp', 'convolutional_mlp', 'dA', 'SdA', 'DBN', 'rbm']
float64 times [   24.00578618    55.60583591   175.75342607   136.53151321   248.71974111
   823.29348183   874.02775431  1079.12984514]
float64 expected [10.300000000000001, 23.699999999999999, 78.099999999999994, 73.700000000000003, 116.40000000000001, 346.89999999999998, 381.89999999999998, 558.10000000000002]
float64 % expected/get [ 0.42906322  0.42621426  0.44437256  0.53980212  0.46799663  0.42135643
  0.43694265  0.51717595]
float32 times [   21.94568563    49.98527789   177.91128731   136.48723054   291.68081236
  1048.11308694  1128.05673623  1302.78659558]
float32 expected [11.6, 29.600000000000001, 47.200000000000003, 66.5, 71.0, 191.19999999999999, 226.80000000000001, 432.80000000000001]
float32 % expected/get [ 0.5285777   0.59217436  0.26530076  0.48722507  0.24341677  0.18242306
  0.20105372  0.33221097]
float64/float32 [ 1.09387269  1.11244427  0.98787114  1.00032445  0.85271204  0.78550062
  0.77480833  0.82832434]
expected float64/float32 [ 0.46934054  0.47413961  0.43898283  0.53997725  0.39906636  0.33097574
  0.3385468   0.42838942]
ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.

 ... plotting sample  0
['logistic_sgd', 'logistic_cg', 'mlp', 'convolutional_mlp', 'dA', 'SdA', 'DBN', 'rbm']
gpu times [   23.64237881    50.57395744   185.60482621   139.12935448   289.82814217
  1050.09374905  1123.013515    1268.43072391]
gpu expected [3.0766348799999998, 7.5552349100000002, 18.992267850000001, 9.5999999999999996, 24.130070450000002, 20.399999999999999, 56.0, 302.60000000000002]
gpu % expected/get [ 0.1301322   0.14938983  0.10232637  0.06900054  0.08325648  0.01942684
  0.04986583  0.2385625 ]
float64/gpu [ 1.01537102  1.09949545  0.94692272  0.98132787  0.85816284  0.78401903
  0.77828783  0.85075978]

Duplicate the timing to have everything in one place
['logistic_sgd', 'logistic_cg', 'mlp', 'convolutional_mlp', 'dA', 'SdA', 'DBN', 'rbm']
float64 times [   24.00578618    55.60583591   175.75342607   136.53151321   248.71974111
   823.29348183   874.02775431  1079.12984514]
float64 expected [10.300000000000001, 23.699999999999999, 78.099999999999994, 73.700000000000003, 116.40000000000001, 346.89999999999998, 381.89999999999998, 558.10000000000002]
float64 % expected/get [ 0.42906322  0.42621426  0.44437256  0.53980212  0.46799663  0.42135643
  0.43694265  0.51717595]
float32 times [   21.94568563    49.98527789   177.91128731   136.48723054   291.68081236
  1048.11308694  1128.05673623  1302.78659558]
float32 expected [11.6, 29.600000000000001, 47.200000000000003, 66.5, 71.0, 191.19999999999999, 226.80000000000001, 432.80000000000001]
float32 % expected/get [ 0.5285777   0.59217436  0.26530076  0.48722507  0.24341677  0.18242306
  0.20105372  0.33221097]
gpu times [   23.64237881    50.57395744   185.60482621   139.12935448   289.82814217
  1050.09374905  1123.013515    1268.43072391]
gpu expected [3.0766348799999998, 7.5552349100000002, 18.992267850000001, 9.5999999999999996, 24.130070450000002, 20.399999999999999, 56.0, 302.60000000000002]
gpu % expected/get [ 0.1301322   0.14938983  0.10232637  0.06900054  0.08325648  0.01942684
  0.04986583  0.2385625 ]
float64/float32 [ 1.09387269  1.11244427  0.98787114  1.00032445  0.85271204  0.78550062
  0.77480833  0.82832434]
expected float64/float32 [ 0.46934054  0.47413961  0.43898283  0.53997725  0.39906636  0.33097574
  0.3385468   0.42838942]
float64/gpu [ 1.01537102  1.09949545  0.94692272  0.98132787  0.85816284  0.78401903
  0.77828783  0.85075978]
expected float64/gpu [ 0.43565836  0.46862063  0.42078647  0.52972286  0.40161731  0.33035146
  0.34006715  0.4399925 ]
float32/gpu [ 0.92823509  0.98836003  0.95854882  0.98100959  1.00639231  0.99811382
  1.00449079  1.02708534]
expected float32/gpu [ 0.49064437  0.58528147  0.25430373  0.47797246  0.24497276  0.18207898
  0.20195661  0.34120902]
speed_failure_float64=8
speed_failure_float32=8
speed_failure_gpu=8



仮想マシン上のlinuxなので、CUDAが使えない…