SlideShare a Scribd company logo
ディープラーニングライブラリ
Coyoteの開発(CNN編)
棚橋 耕太郎
2015.10.16
シルバーウィークの宿題
前回までの実装
• fully connected(FC)レイヤーとlogistic回帰を実装
• mnistデータセットで正答率97%
0,1,7,3,…
logistic classifierfully connected
layer
fully connected
layer
handwritten digits
images
labels
今回やったこと
• データ構造の再設計
• Convolutional Neural Network(CNN)を実装
• mnistデータセットで正答率98%
stackoverflow.com
CNNの構造
CNNの特徴
rgb input convolution
filter size=5
depth=5
pooling
pool size=2
width=28
height=28
depth=3
width=(28-5+1)=24
height=(28-5+1)=24
depth=5
width=24/2=12
height=24/2=12
depth=5
28
28
24
24
5
2
2
5
全体 詳細
• FCと異なるデータ構造(2次元の階層構造を持つ)
• 出力されるデータの次元が入力画像の次元に依存
(出力されるデータの次元が簡単にわからない)
FCのInterface
以前までのFCの実装
# make neural network
layers=[]
layers.append(FullyConnect(n_in=28 * 28, n_out=100, activation='tanh'))
layers.append(FullyConnect(n_in=100, n_out=50, activation='tanh'))
layers.append(LogisticRegression(n_in=50, n_out=10))
各レイヤーへの入力次元を指定しないといけなかった
->各レイヤーへの入力次元は自動計算へ
(つまり、レイヤーを上から順番に作成するようにした)
CNNのinterface# make neural network
layers = []
layers.append(ImageInput(out_sx=28, out_sy=28, out_depth=1))
layers.append(LeNetConvPoolLayer(out_depth=7, filter_size=5))
layers.append(LeNetConvPoolLayer(out_depth=3, filter_size=5))
layers.append(Flatten())
layers.append(LogisticRegression(n_out=10))
• CNNは最低限の設定out depthとfilter sizeを指定するだけ
• 便宜的に、扱うデータを⑴画像vectorと⑵1dim vetorに分ける
• flattenで⑴->⑵は変換可能
• 画像の入力はImageInputで行う
rgb input convolution
filter size=5
depth=5
pooling
pool size=2
width=28
height=28
depth=3
width=(28-5+1)=24
height=(28-5+1)=24
depth=5
width=24/2=12
height=24/2=12
depth=5
28
28
24
24
5
2
2
5
phirosophy
適当に書けばとりあえず動く!!
legoのように
たった1つのシンプルなルール:
同じ種類の矢印同士はつなぐことができる
FullyConnect
RNN RNN Repeat
LeNetConv Flatten
LogisticRegression
Input ImageInput SequenceInput
image vector
1 dim vector
sequence vector
Layer class
class Layer(object):
def __init__(self):
self.input=T.matrix()
self.output=None
self.batch_size = 1
self.in_sx = 1
self.in_sy = 1
self.in_depth = 1
def in_shape(self):
return self.batch_size, self.in_sx, self.in_sy, self.in_depth
def out_shape(self):
return self.batch_size, self.in_depth, self.in_sx, self.in_sy
def set_in_shape(self, pre_out_shape):
self.batch_size = pre_out_shape[0]
self.in_depth = pre_out_shape[1]
self.in_sx = pre_out_shape[2]
self.in_sy = pre_out_shape[3]
すべてのneural netはlayerクラスを継承して作られている
例) Flatten
class Flatten(Layer):
def __init__(self):
super(Flatten,self).__init__()
self.input = T.matrix()
def out_shape(self):
return self.batch_size, self.in_sx * self.in_sy * self.in_depth, 1, 1
def set_input_image_shape(self, image_shape):
self.image_shape = image_shape
self.n_out = self.image_shape[2] * self.image_shape[3] * self.in_depth
def get_output(self):
tmp = self.input.flatten(3)
return tmp.reshape((self.batch_size,self.in_sx * self.in_sy * self.in_depth,1,1))
(1)画像vectorから(2)1dim ベクトルへの変換に伴って
out shapeのsx,syを1にしている
例)LeNetConvPoolLayer
class LeNetConvPoolLayer(Layer):
"""Pool Layer of a convolutional network """
def __init__(self, out_depth, filter_size, poolsize=(2, 2)):
super(LeNetConvPoolLayer,self).__init__()
self.image_shape = None
self.in_depth = None
self.input = T.matrix()
self.rng = np.random.RandomState(4711)
self.poolsize = poolsize
self.filter_size = filter_size
self.out_depth = out_depth
def init_params(self):
self.filter_shape = (self.out_depth, self.in_depth, self.filter_size,
self.filter_size)
# there are "num input feature maps * filter height * filter width"
# inputs to each hidden unit
fan_in = np.prod(self.filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# "num output feature maps * filter height * filter width" /
# pooling size
fan_out = (self.filter_shape[0] * np.prod(self.filter_shape[2:]) /
np.prod(self.poolsize))
# initialize weights with random weights
W_bound = np.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(
np.asarray(
self.rng.uniform(low=-W_bound, high=W_bound,
size=self.filter_shape),
dtype=theano.config.floatX
),
borrow=True
)
# the bias is a 1D tensor -- one bias per output feature map
b_values = np.zeros((self.filter_shape[0],),
dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
# store parameters of this layer
self.params = [self.W, self.b]
def set_input_image_shape(self, image_shape):
self.image_shape = image_shape
def get_output_image_shape(self):
conv_out_shape = self.image_shape[2] -
self.filter_size + 1, self.image_shape[3] - self.filter_size +
1
return self.batch_size, self.out_depth,
conv_out_shape[0]/self.poolsize[0], conv_out_shape[1]/
self.poolsize[1]
def out_shape(self):
conv_out_shape = self.in_sx - self.filter_size + 1,
self.in_sy - self.filter_size + 1
return self.batch_size, self.out_depth,
conv_out_shape[0]/self.poolsize[0], conv_out_shape[1]/
self.poolsize[1]
def get_output(self):
# convolve input feature maps with filters
print "self.image_shape",self.image_shape
self.conv_out = conv.conv2d(
input=self.input,
filters=self.W,
filter_shape=self.filter_shape,
image_shape=self.image_shape
)
# downsample each feature map individually, using
maxpooling
self.pooled_out = downsample.max_pool_2d(
input=self.conv_out,
ds=self.poolsize,
ignore_border=True
)
return T.tanh(self.pooled_out +
self.b.dimshuffle('x', 0, 'x', 'x'))
model classの概要
# set batch size at the initial input layer
self.layers[0].set_batchsize(batch_size)
for i,layer in enumerate(self.layers[1:]):
pre_layer = self.layers[i]
layer.set_in_shape(pre_layer.out_shape())
# init parameters
for layer in self.layers:
layer.init_params()
# set input and output
self.layers[0].setInput(self.x2)
for i,layer in enumerate(self.layers[1:]):
layer.setInput(self.layers[i].get_output())
for layer in self.layers:
if hasattr(layer,'params'):
try:
self.params += layer.params
except:
self.params = layer.params
self.cost = self.layers[-1].get_cost(self.y)
self.grads = T.grad(self.cost, self.params)
self.updates = [
(param_i, param_i - self.learning_rate * grad_i)
for param_i, grad_i in zip(self.params, self.grads)
]
<-上から順番にshape(次元)を計算
<-それぞれパラメータの初期化
<-上のlayerの出力を下のlayerの入力
に代入
<-各layerのパラメータをまとめる
<-分類layerからcostを取り出す
<-パラメータの更新方法を指定
つづく

More Related Content

What's hot

What's hot (20)

Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnn
 
Gentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowGentlest Introduction to Tensorflow
Gentlest Introduction to Tensorflow
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Memory management
Memory managementMemory management
Memory management
 
Image Recognition with Neural Network
Image Recognition with Neural NetworkImage Recognition with Neural Network
Image Recognition with Neural Network
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
Efnsjdnfsuies
EfnsjdnfsuiesEfnsjdnfsuies
Efnsjdnfsuies
 
NTU ML TENSORFLOW
NTU ML TENSORFLOWNTU ML TENSORFLOW
NTU ML TENSORFLOW
 
Program implementation and testing
Program implementation and testingProgram implementation and testing
Program implementation and testing
 
Google TensorFlow Tutorial
Google TensorFlow TutorialGoogle TensorFlow Tutorial
Google TensorFlow Tutorial
 
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
 
Computational Linguistics week 10
 Computational Linguistics week 10 Computational Linguistics week 10
Computational Linguistics week 10
 
Animasi di andengine
Animasi di andengineAnimasi di andengine
Animasi di andengine
 
Deep learning study 3
Deep learning study 3Deep learning study 3
Deep learning study 3
 
Computer programming 2 Lesson 14
Computer programming 2  Lesson 14Computer programming 2  Lesson 14
Computer programming 2 Lesson 14
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & Matplotlib
 
Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2
 
Java program-to-calculate-area-and-circumference-of-circle
Java program-to-calculate-area-and-circumference-of-circleJava program-to-calculate-area-and-circumference-of-circle
Java program-to-calculate-area-and-circumference-of-circle
 

Viewers also liked

変分推論法(変分ベイズ法)(PRML第10章)
変分推論法(変分ベイズ法)(PRML第10章)変分推論法(変分ベイズ法)(PRML第10章)
変分推論法(変分ベイズ法)(PRML第10章)
Takao Yamanaka
 
Convolutional Neural Network @ CV勉強会関東
Convolutional Neural Network @ CV勉強会関東Convolutional Neural Network @ CV勉強会関東
Convolutional Neural Network @ CV勉強会関東
Hokuto Kagaya
 
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
Takashi Abe
 

Viewers also liked (13)

SWARでpop countをしよう
SWARでpop countをしようSWARでpop countをしよう
SWARでpop countをしよう
 
変数の入れ替え(SWAPPING)で最速の方法は?
変数の入れ替え(SWAPPING)で最速の方法は?変数の入れ替え(SWAPPING)で最速の方法は?
変数の入れ替え(SWAPPING)で最速の方法は?
 
How nosql fundamentally changed machine learning?
How nosql fundamentally changed machine  learning?How nosql fundamentally changed machine  learning?
How nosql fundamentally changed machine learning?
 
2.2. map reduce and the new software stack
2.2. map reduce and the new software stack2.2. map reduce and the new software stack
2.2. map reduce and the new software stack
 
DSP開発におけるSpark MLlibの活用
DSP開発におけるSpark MLlibの活用DSP開発におけるSpark MLlibの活用
DSP開発におけるSpark MLlibの活用
 
Deeplearning4.4 takmin
Deeplearning4.4 takminDeeplearning4.4 takmin
Deeplearning4.4 takmin
 
Cvim saisentan-6-4-tomoaki
Cvim saisentan-6-4-tomoakiCvim saisentan-6-4-tomoaki
Cvim saisentan-6-4-tomoaki
 
NIPS Paper Reading, Data Programing
NIPS Paper Reading, Data ProgramingNIPS Paper Reading, Data Programing
NIPS Paper Reading, Data Programing
 
変分推論法(変分ベイズ法)(PRML第10章)
変分推論法(変分ベイズ法)(PRML第10章)変分推論法(変分ベイズ法)(PRML第10章)
変分推論法(変分ベイズ法)(PRML第10章)
 
WSDM2016読み会 Collaborative Denoising Auto-Encoders for Top-N Recommender Systems
WSDM2016読み会 Collaborative Denoising Auto-Encoders for Top-N Recommender SystemsWSDM2016読み会 Collaborative Denoising Auto-Encoders for Top-N Recommender Systems
WSDM2016読み会 Collaborative Denoising Auto-Encoders for Top-N Recommender Systems
 
Convolutional Neural Network @ CV勉強会関東
Convolutional Neural Network @ CV勉強会関東Convolutional Neural Network @ CV勉強会関東
Convolutional Neural Network @ CV勉強会関東
 
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
多層NNの教師なし学習 コンピュータビジョン勉強会@関東 2014/5/26
 
実装ディープラーニング
実装ディープラーニング実装ディープラーニング
実装ディープラーニング
 

Similar to deep learning library coyoteの開発(CNN編)

#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
computersmartdwarka
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
ssuserb4d806
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on Netezza
Ajay Ohri
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
actexerode
 

Similar to deep learning library coyoteの開発(CNN編) (20)

Tutorial on convolutional neural networks
Tutorial on convolutional neural networksTutorial on convolutional neural networks
Tutorial on convolutional neural networks
 
Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNetScaling Deep Learning with MXNet
Scaling Deep Learning with MXNet
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
 
B.tech_project_ppt.pptx
B.tech_project_ppt.pptxB.tech_project_ppt.pptx
B.tech_project_ppt.pptx
 
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNetAlex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to Chainer
 
RNN sharing at Trend Micro
RNN sharing at Trend MicroRNN sharing at Trend Micro
RNN sharing at Trend Micro
 
Hybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskitHybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskit
 
Introduction to convolutional networks .pptx
Introduction to convolutional networks .pptxIntroduction to convolutional networks .pptx
Introduction to convolutional networks .pptx
 
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
 
On-the-fly Visual Category Search in Web-scale Image Collections
On-the-fly Visual Category Search in Web-scale Image CollectionsOn-the-fly Visual Category Search in Web-scale Image Collections
On-the-fly Visual Category Search in Web-scale Image Collections
 
Scalable Deep Learning Using Apache MXNet
Scalable Deep Learning Using Apache MXNetScalable Deep Learning Using Apache MXNet
Scalable Deep Learning Using Apache MXNet
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on Netezza
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Machine Learning from a Software Engineer's perspective
Machine Learning from a Software Engineer's perspectiveMachine Learning from a Software Engineer's perspective
Machine Learning from a Software Engineer's perspective
 
Machine learning from a software engineer's perspective - Marijn van Zelst - ...
Machine learning from a software engineer's perspective - Marijn van Zelst - ...Machine learning from a software engineer's perspective - Marijn van Zelst - ...
Machine learning from a software engineer's perspective - Marijn van Zelst - ...
 

More from Kotaro Tanahashi (6)

Deep Q-Network 論文輪読会
Deep Q-Network 論文輪読会Deep Q-Network 論文輪読会
Deep Q-Network 論文輪読会
 
Cythonの一喜一憂
Cythonの一喜一憂Cythonの一喜一憂
Cythonの一喜一憂
 
recurrent models of visual attentionメモ
recurrent models of visual attentionメモrecurrent models of visual attentionメモ
recurrent models of visual attentionメモ
 
情強アルゴリズムDIMSUM
情強アルゴリズムDIMSUM情強アルゴリズムDIMSUM
情強アルゴリズムDIMSUM
 
LBFGSの実装
LBFGSの実装LBFGSの実装
LBFGSの実装
 
Fokker–Planck equation and DPD simulations
Fokker–Planck equation and DPD simulationsFokker–Planck equation and DPD simulations
Fokker–Planck equation and DPD simulations
 

Recently uploaded

Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
AADYARAJPANDEY1
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
PirithiRaju
 
The importance of continents, oceans and plate tectonics for the evolution of...
The importance of continents, oceans and plate tectonics for the evolution of...The importance of continents, oceans and plate tectonics for the evolution of...
The importance of continents, oceans and plate tectonics for the evolution of...
Sérgio Sacani
 
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
muralinath2
 
platelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptxplatelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptx
muralinath2
 

Recently uploaded (20)

The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...
 
GEOLOGICAL FIELD REPORT On Kaptai Rangamati Road-Cut Section.pdf
GEOLOGICAL FIELD REPORT  On  Kaptai Rangamati Road-Cut Section.pdfGEOLOGICAL FIELD REPORT  On  Kaptai Rangamati Road-Cut Section.pdf
GEOLOGICAL FIELD REPORT On Kaptai Rangamati Road-Cut Section.pdf
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
 
Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
 
FAIRSpectra - Towards a common data file format for SIMS images
FAIRSpectra - Towards a common data file format for SIMS imagesFAIRSpectra - Towards a common data file format for SIMS images
FAIRSpectra - Towards a common data file format for SIMS images
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
 
Predicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdfPredicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdf
 
Transport in plants G1.pptx Cambridge IGCSE
Transport in plants G1.pptx Cambridge IGCSETransport in plants G1.pptx Cambridge IGCSE
Transport in plants G1.pptx Cambridge IGCSE
 
NuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final versionNuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final version
 
word2vec, node2vec, graph2vec, X2vec: Towards a Theory of Vector Embeddings o...
word2vec, node2vec, graph2vec, X2vec: Towards a Theory of Vector Embeddings o...word2vec, node2vec, graph2vec, X2vec: Towards a Theory of Vector Embeddings o...
word2vec, node2vec, graph2vec, X2vec: Towards a Theory of Vector Embeddings o...
 
The importance of continents, oceans and plate tectonics for the evolution of...
The importance of continents, oceans and plate tectonics for the evolution of...The importance of continents, oceans and plate tectonics for the evolution of...
The importance of continents, oceans and plate tectonics for the evolution of...
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
 
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
 
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
 
Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
 
Topography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of BengalTopography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of Bengal
 
A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on Earth
 
Richard's entangled aventures in wonderland
Richard's entangled aventures in wonderlandRichard's entangled aventures in wonderland
Richard's entangled aventures in wonderland
 
platelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptxplatelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptx
 

deep learning library coyoteの開発(CNN編)

  • 2. 前回までの実装 • fully connected(FC)レイヤーとlogistic回帰を実装 • mnistデータセットで正答率97% 0,1,7,3,… logistic classifierfully connected layer fully connected layer handwritten digits images labels
  • 3. 今回やったこと • データ構造の再設計 • Convolutional Neural Network(CNN)を実装 • mnistデータセットで正答率98% stackoverflow.com CNNの構造
  • 4. CNNの特徴 rgb input convolution filter size=5 depth=5 pooling pool size=2 width=28 height=28 depth=3 width=(28-5+1)=24 height=(28-5+1)=24 depth=5 width=24/2=12 height=24/2=12 depth=5 28 28 24 24 5 2 2 5 全体 詳細 • FCと異なるデータ構造(2次元の階層構造を持つ) • 出力されるデータの次元が入力画像の次元に依存 (出力されるデータの次元が簡単にわからない)
  • 5. FCのInterface 以前までのFCの実装 # make neural network layers=[] layers.append(FullyConnect(n_in=28 * 28, n_out=100, activation='tanh')) layers.append(FullyConnect(n_in=100, n_out=50, activation='tanh')) layers.append(LogisticRegression(n_in=50, n_out=10)) 各レイヤーへの入力次元を指定しないといけなかった ->各レイヤーへの入力次元は自動計算へ (つまり、レイヤーを上から順番に作成するようにした)
  • 6. CNNのinterface# make neural network layers = [] layers.append(ImageInput(out_sx=28, out_sy=28, out_depth=1)) layers.append(LeNetConvPoolLayer(out_depth=7, filter_size=5)) layers.append(LeNetConvPoolLayer(out_depth=3, filter_size=5)) layers.append(Flatten()) layers.append(LogisticRegression(n_out=10)) • CNNは最低限の設定out depthとfilter sizeを指定するだけ • 便宜的に、扱うデータを⑴画像vectorと⑵1dim vetorに分ける • flattenで⑴->⑵は変換可能 • 画像の入力はImageInputで行う rgb input convolution filter size=5 depth=5 pooling pool size=2 width=28 height=28 depth=3 width=(28-5+1)=24 height=(28-5+1)=24 depth=5 width=24/2=12 height=24/2=12 depth=5 28 28 24 24 5 2 2 5
  • 9. Layer class class Layer(object): def __init__(self): self.input=T.matrix() self.output=None self.batch_size = 1 self.in_sx = 1 self.in_sy = 1 self.in_depth = 1 def in_shape(self): return self.batch_size, self.in_sx, self.in_sy, self.in_depth def out_shape(self): return self.batch_size, self.in_depth, self.in_sx, self.in_sy def set_in_shape(self, pre_out_shape): self.batch_size = pre_out_shape[0] self.in_depth = pre_out_shape[1] self.in_sx = pre_out_shape[2] self.in_sy = pre_out_shape[3] すべてのneural netはlayerクラスを継承して作られている
  • 10. 例) Flatten class Flatten(Layer): def __init__(self): super(Flatten,self).__init__() self.input = T.matrix() def out_shape(self): return self.batch_size, self.in_sx * self.in_sy * self.in_depth, 1, 1 def set_input_image_shape(self, image_shape): self.image_shape = image_shape self.n_out = self.image_shape[2] * self.image_shape[3] * self.in_depth def get_output(self): tmp = self.input.flatten(3) return tmp.reshape((self.batch_size,self.in_sx * self.in_sy * self.in_depth,1,1)) (1)画像vectorから(2)1dim ベクトルへの変換に伴って out shapeのsx,syを1にしている
  • 11. 例)LeNetConvPoolLayer class LeNetConvPoolLayer(Layer): """Pool Layer of a convolutional network """ def __init__(self, out_depth, filter_size, poolsize=(2, 2)): super(LeNetConvPoolLayer,self).__init__() self.image_shape = None self.in_depth = None self.input = T.matrix() self.rng = np.random.RandomState(4711) self.poolsize = poolsize self.filter_size = filter_size self.out_depth = out_depth def init_params(self): self.filter_shape = (self.out_depth, self.in_depth, self.filter_size, self.filter_size) # there are "num input feature maps * filter height * filter width" # inputs to each hidden unit fan_in = np.prod(self.filter_shape[1:]) # each unit in the lower layer receives a gradient from: # "num output feature maps * filter height * filter width" / # pooling size fan_out = (self.filter_shape[0] * np.prod(self.filter_shape[2:]) / np.prod(self.poolsize)) # initialize weights with random weights W_bound = np.sqrt(6. / (fan_in + fan_out)) self.W = theano.shared( np.asarray( self.rng.uniform(low=-W_bound, high=W_bound, size=self.filter_shape), dtype=theano.config.floatX ), borrow=True ) # the bias is a 1D tensor -- one bias per output feature map b_values = np.zeros((self.filter_shape[0],), dtype=theano.config.floatX) self.b = theano.shared(value=b_values, borrow=True) # store parameters of this layer self.params = [self.W, self.b] def set_input_image_shape(self, image_shape): self.image_shape = image_shape def get_output_image_shape(self): conv_out_shape = self.image_shape[2] - self.filter_size + 1, self.image_shape[3] - self.filter_size + 1 return self.batch_size, self.out_depth, conv_out_shape[0]/self.poolsize[0], conv_out_shape[1]/ self.poolsize[1] def out_shape(self): conv_out_shape = self.in_sx - self.filter_size + 1, self.in_sy - self.filter_size + 1 return self.batch_size, self.out_depth, conv_out_shape[0]/self.poolsize[0], conv_out_shape[1]/ self.poolsize[1] def get_output(self): # convolve input feature maps with filters print "self.image_shape",self.image_shape self.conv_out = conv.conv2d( input=self.input, filters=self.W, filter_shape=self.filter_shape, image_shape=self.image_shape ) # downsample each feature map individually, using maxpooling self.pooled_out = downsample.max_pool_2d( input=self.conv_out, ds=self.poolsize, ignore_border=True ) return T.tanh(self.pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
  • 12. model classの概要 # set batch size at the initial input layer self.layers[0].set_batchsize(batch_size) for i,layer in enumerate(self.layers[1:]): pre_layer = self.layers[i] layer.set_in_shape(pre_layer.out_shape()) # init parameters for layer in self.layers: layer.init_params() # set input and output self.layers[0].setInput(self.x2) for i,layer in enumerate(self.layers[1:]): layer.setInput(self.layers[i].get_output()) for layer in self.layers: if hasattr(layer,'params'): try: self.params += layer.params except: self.params = layer.params self.cost = self.layers[-1].get_cost(self.y) self.grads = T.grad(self.cost, self.params) self.updates = [ (param_i, param_i - self.learning_rate * grad_i) for param_i, grad_i in zip(self.params, self.grads) ] <-上から順番にshape(次元)を計算 <-それぞれパラメータの初期化 <-上のlayerの出力を下のlayerの入力 に代入 <-各layerのパラメータをまとめる <-分類layerからcostを取り出す <-パラメータの更新方法を指定