LoginSignup
0
1

More than 5 years have passed since last update.

Python > fully_connected > weight, biasからconvolutionを計算しようとしている > v0.1-v0.4

Last updated at Posted at 2016-12-07
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.

sine curveを学習した時のweightとbiasをもとに自分でネットワークを再現して出力を計算しようとしている。

convolutionを実装することになりそう。

Pythonのndarrayはほとんどわからないが、とりあえず実装を試みている。

資料 https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
資料 http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html
参考 http://qiita.com/7of9/items/586f62fa46dc3409d954 のコメント
参考 http://qiita.com/7of9/items/06fee8f425c7a514377b#comment-70abc9bc48ed226fd6c5

code v0.1

read_mode_var.py
import numpy as np

model_var = np.load('model_variables.npy')

print "all shape:",(model_var.shape)

def calc_conv(src, weight, bias):
    wgt = weight.shape
    print wgt # debug
    conv = list(range(bias.size))
    if wgt[0] == 1:
        for idx2 in range(wgt[1]):
            conv[idx2] = src * weight[0,idx2]
    else:
        for idx1 in range(wgt[0]):
            for idx2 in range(wgt[1]):
                conv[idx2] = src[idx1] * weight[idx1,idx2]
    return conv # return list

indata = 2.718  # for (1,N) e.g. (1,7)
inp1_weight = model_var[2]
inp1_bias = model_var[3]

#indata = [1,2,3,4,5,6,7] # for (N,N) e.g. (7,7)
#inp1_weight = model_var[4]
#inp1_bias = model_var[5]

conv1 = calc_conv(indata, inp1_weight, inp1_bias)
print (inp1_weight)
print (inp1_bias)
print ('conv:',conv1)

結果は以下。

$ python read_model_var.py
all shape: (10,)
(1, 7)
[[-4.58573723 -4.27450418 -3.75889063 4.51949883 -4.02780342 -4.10122681
4.0842309 ]]
[ 0.97008353 0.70625514 0.27048966 -0.83405548 0.57475132 0.64893931
-0.45576799]
('conv:', [-12.464033786773681, -11.618102374076843, -10.216664729118348, 12.283997806549072, -10.947569698333741, -11.14713446044922, 11.10093958568573])

conv:に関して、google spreadsheetと近い値は出た。

qiita.png

課題

if wgt[0] == 1:として処理を分けている。

else:の処理だけでしようとすると以下のように怒られた。

Traceback (most recent call last):
File "read_model_var.py", line 30, in
conv1 = calc_conv(indata, inp1_weight, inp1_bias)
File "read_model_var.py", line 19, in calc_conv
conv[idx2] = src[idx1] * weight[idx1,idx2]
TypeError: 'float' object has no attribute 'getitem'

if wgt[0] == 1:なしで実装する方法は調査を要する。

code v0.2

そもそも
indata = 2.718 # for (1,N) e.g. (1,7)

indata = [ 2.718 ] # for (1,N) e.g. (1,7)
が正しかった。
1つのfloatではなく、1要素のリストを扱うのだった。

以下のコードになった。

read_model_var.py
#import sys
import numpy as np

model_var = np.load('model_variables.npy')

print "all shape:",(model_var.shape)

def calc_conv(src, weight, bias):
    wgt = weight.shape
    print wgt # debug
    conv = list(range(bias.size))
    for idx1 in range(wgt[0]):
        for idx2 in range(wgt[1]):
            conv[idx2] = src[idx1] * weight[idx1,idx2]
    return conv # return list

indata = [ 2.718 ] # for (1,N) e.g. (1,7)
inp1_weight = model_var[2]
inp1_bias = model_var[3]

#indata = [1,2,3,4,5,6,7] # for (N,N) e.g. (7,7)
#inp1_weight = model_var[4]
#inp1_bias = model_var[5]

conv1 = calc_conv(indata, inp1_weight, inp1_bias)
print (inp1_weight)
print (inp1_bias)
print ('conv:',conv1)

code v0.3 (add bias)

read_model_var.py
'''
v0.3 Dec. 07, 2016
    - calc_conv() > add bias
v0.2 Dec. 07, 2016
    - fix calc_conv() treating src as a list
v0.1 Dec. 07, 2016
    - add calc_conv()
'''

import numpy as np

model_var = np.load('model_variables.npy')

print "all shape:",(model_var.shape)

def calc_conv(src, weight, bias):
    wgt = weight.shape
    print wgt # debug
    #conv = list(range(bias.size))
    conv = [0.0] * bias.size
    # weight
    for idx1 in range(wgt[0]):
        for idx2 in range(wgt[1]):
            conv[idx2] = conv[idx2] + src[idx1] * weight[idx1,idx2]
    # bias
    for idx2 in range(wgt[1]):
        conv[idx2] = conv[idx2] + bias[idx2]
    return conv # return list

indata = [ 2.718 ] # for (1,N) e.g. (1,7)
inp1_weight = model_var[2]
inp1_bias = model_var[3]

#indata = [1,2,3,4,5,6,7] # for (N,N) e.g. (7,7)
#inp1_weight = model_var[4]
#inp1_bias = model_var[5]

conv1 = calc_conv(indata, inp1_weight, inp1_bias)
print (inp1_weight)
print (inp1_bias)
print ('conv:',conv1)

code v0.4 > 2x2 network example added

入力が複数の場合の例として、2x2のネットワーク計算用データを追加。

ndarrayの用意の仕方は以下を参考にしました。情報感謝です。 
http://qiita.com/richi40/items/6b3af6f4b00d62dbe8e1

read_model_var.py
'''
v0.4 Dec. 10, 2016
    - add 2x2 network example
v0.3 Dec. 07, 2016
    - calc_conv() > add bias
v0.2 Dec. 07, 2016
    - fix calc_conv() treating src as a list
v0.1 Dec. 07, 2016
    - add calc_conv()
'''

import numpy as np

model_var = np.load('model_variables.npy')

print "all shape:",(model_var.shape)

def calc_conv(src, weight, bias):
    wgt = weight.shape
    print wgt # debug
    #conv = list(range(bias.size))
    conv = [0.0] * bias.size
    # weight
    for idx1 in range(wgt[0]):
        for idx2 in range(wgt[1]):
            conv[idx2] = conv[idx2] + src[idx1] * weight[idx1,idx2]
    # bias
    for idx2 in range(wgt[1]):
        conv[idx2] = conv[idx2] + bias[idx2]
    return conv # return list

#indata = [ 2.718 ] # for (1,N) e.g. (1,7)
#inp1_weight = model_var[2]
#inp1_bias = model_var[3]

#indata = [1,2,3,4,5,6,7] # for (N,N) e.g. (7,7)
#inp1_weight = model_var[4]
#inp1_bias = model_var[5]

indata = [ 3, 1 ] # for 2x2 network
inp1_weight = np.array([ [4,1], [5,9] ], dtype=np.float)
inp1_bias = np.array([ 2, 6 ], dtype=np.float)

conv1 = calc_conv(indata, inp1_weight, inp1_bias)
print (inp1_weight)
print (inp1_bias)
print ('conv:',conv1)
結果
$ python read_model_var.py 
all shape: (10,)
(2, 2)
[[ 4.  1.]
 [ 5.  9.]]
[ 2.  6.]
('conv:', [19.0, 18.0])

試したネットワークは以下。1,3はinput。6,2はbias。線上の値はweight。

3x4 + 1x5 + 2 = 19
3x1 + 1x9 + 6 = 18

合っていそう。
問題はTensorFlowの実行結果のweightのオーダー(column major order / row major orderのどちらか)。
実際に計算をして大幅にずれないか確認することになりそうか。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1