0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TensorFlow > sine curveの学習 > weight,biasからの学習結果の再現 v0.1 (失敗)

Last updated at Posted at 2016-12-09
動作環境
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.

関連 http://qiita.com/7of9/items/b364d897b95476a30754

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

http://qiita.com/7of9/items/ee9d866eee453c94d519
にてconvolutionを実装したので、実際にsine curveを再現しようとした。

code v0.1

reproduce_sine.py
'''
v0.1 Dec. 10, 2016
	- add calc_sigmoid()
	- add fully_connected network
	- add input data for sine curve
=== [read_model_var.py] branched to [reproduce_sine.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
import math

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

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

def calc_sigmoid(x):
	return 1.0 / (1.0 + math.exp(x))

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]
	# activation function
	for idx2 in range(wgt[1]):
		conv[idx2] = calc_sigmoid(conv[idx2])

	return conv # return list

inpdata = np.linspace(0, 1, 10).astype(float).tolist()

for din in inpdata:
	# input layer (1 node)
	inlist = [ din ]
	outdata = calc_conv(inlist, model_var[0], model_var[1])
	# hidden layer 1 (1 node)
	outdata = calc_conv(outdata, model_var[2], model_var[3])
	# hidden layer 2 (7 node)
	outdata = calc_conv(outdata, model_var[4], model_var[5])
	# hidden layer 3 (7 node)
	outdata = calc_conv(outdata, model_var[6], model_var[7])
	# output layer (1 node)
	outdata = calc_conv(outdata, model_var[8], model_var[9])
	dout = outdata[0] # ouput is 1 node
	print '%.3f, %.3f' % (din,dout)

いでよ!マイsine curve!!!

結果
$ python reproduce_sine.py 
all shape: (10,)
0.000, 0.705
0.111, 0.991
0.222, 0.999
0.333, 0.999
0.444, 0.999
0.556, 0.999
0.667, 0.999
0.778, 0.999
0.889, 0.999
1.000, 0.999

きれいなsine curveが再現されました(棒読み)。

TODO

以下のいずれか

  • TensorFlowで途中計算の値を出力する
  • TensorBoardを利用するようにして、計算過程を確認する
    • TensorBoardの扱いが面倒だったりする
  • Fully connectedの計算資料を探す
    • sigmoid関数の適用・不適用など確認
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?