Pybrain-layers

提供:Dev Guides
移動先:案内検索

PyBrain-レイヤー

レイヤーは基本的に、ネットワークの非表示レイヤーで使用される一連の機能です。

この章では、レイヤーに関する次の詳細を説明します-

  • レイヤーを理解する
  • Pybrainを使用してレイヤーを作成する

レイヤーを理解する

次のようにレイヤーを使用した例を以前に見ました-

  • TanhLayer
  • SoftmaxLayer

TanhLayerを使用した例

以下は、ネットワークを構築するためにTanhLayerを使用した1つの例です-

*testnetwork.py*
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

# Create a network with two inputs, three hidden, and one output
nn = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)

# Create a dataset that matches network input and output sizes:
norgate = SupervisedDataSet(2, 1)

# Create a dataset to be used for testing.
nortrain = SupervisedDataSet(2, 1)

# Add input and target values to dataset
# Values for NOR truth table
norgate.addSample((0, 0), (1,))
norgate.addSample((0, 1), (0,))
norgate.addSample((1, 0), (0,))
norgate.addSample((1, 1), (0,))

# Add input and target values to dataset
# Values for NOR truth table
nortrain.addSample((0, 0), (1,))
nortrain.addSample((0, 1), (0,))
nortrain.addSample((1, 0), (0,))
nortrain.addSample((1, 1), (0,))

#Training the network with dataset norgate.
trainer = BackpropTrainer(nn, norgate)

# will run the loop 1000 times to train it.
for epoch in range(1000):
   trainer.train()
trainer.testOnData(dataset=nortrain, verbose = True)

出力

上記のコードの出力は次のとおりです-

*python testnetwork.py*
C:\pybrain\pybrain\src>python testnetwork.py
Testing on data:
('out: ', '[0.887 ]')
('correct:', '[1 ]')
error: 0.00637334
('out: ', '[0.149 ]')
('correct:', '[0 ]')
error: 0.01110338
('out: ', '[0.102 ]')
('correct:', '[0 ]')
error: 0.00522736
('out: ', '[-0.163]')
('correct:', '[0 ]')
error: 0.01328650
('All errors:', [0.006373344564625953, 0.01110338071737218,
   0.005227359234093431, 0.01328649974219942])
('Average error:', 0.008997646064572746)
('Max error:', 0.01328649974219942, 'Median error:', 0.01110338071737218)

SoftMaxLayerを使用した例

以下は、SoftmaxLayerを使用してネットワークを構築した1つの例です-

from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import SoftmaxLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

# Create a network with two inputs, three hidden, and one output
nn = buildNetwork(2, 3, 1, bias=True, hiddenclass=SoftmaxLayer)

# Create a dataset that matches network input and output sizes:
norgate = SupervisedDataSet(2, 1)

# Create a dataset to be used for testing.
nortrain = SupervisedDataSet(2, 1)

# Add input and target values to dataset
# Values for NOR truth table
norgate.addSample((0, 0), (1,))
norgate.addSample((0, 1), (0,))
norgate.addSample((1, 0), (0,))
norgate.addSample((1, 1), (0,))

# Add input and target values to dataset
# Values for NOR truth table
nortrain.addSample((0, 0), (1,))
nortrain.addSample((0, 1), (0,))
nortrain.addSample((1, 0), (0,))
nortrain.addSample((1, 1), (0,))

#Training the network with dataset norgate.
trainer = BackpropTrainer(nn, norgate)

# will run the loop 1000 times to train it.
for epoch in range(1000):
trainer.train()
trainer.testOnData(dataset=nortrain, verbose = True)

出力

出力は次のとおりです-

C:\pybrain\pybrain\src>python example16.py
Testing on data:
('out: ', '[0.918 ]')
('correct:', '[1 ]')
error: 0.00333524
('out: ', '[0.082 ]')
('correct:', '[0 ]')
error: 0.00333484
('out: ', '[0.078 ]')
('correct:', '[0 ]')
error: 0.00303433
('out: ', '[-0.082]')
('correct:', '[0 ]')
error: 0.00340005
('All errors:', [0.0033352368788838365, 0.003334842961037291,
   0.003034328685718761, 0.0034000458892589056])
('Average error:', 0.0032761136037246985)
('Max error:', 0.0034000458892589056, 'Median error:', 0.0033352368788838365)

Pybrainでレイヤーを作成する

Pybrainでは、次のように独自のレイヤーを作成できます-

レイヤーを作成するには、 _ NeuronLayer class_ を基本クラスとして使用して、すべてのタイプのレイヤーを作成する必要があります。

from pybrain.structure.modules.neuronlayer import NeuronLayer
class LinearLayer(NeuronLayer):
   def _forwardImplementation(self, inbuf, outbuf):
      outbuf[:] = inbuf
   def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
      inerr[:] = outer

レイヤーを作成するには、2つのメソッドforwardImplementation()_およびbackwardImplementation()_を実装する必要があります。

*__ forwardImplementation()は2つの引数inbuf_* およびoutbufを取ります。これらはScipy配列です。 そのサイズは、レイヤーの入力および出力の次元に依存します。

__backwardImplementation()_は、指定された入力に対する出力の導関数を計算するために使用されます。

だからPybrainでレイヤーを実装するには、これはレイヤークラスのスケルトンです-

from pybrain.structure.modules.neuronlayer import NeuronLayer
class NewLayer(NeuronLayer):
   def _forwardImplementation(self, inbuf, outbuf):
      pass
   def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
      pass

あなたが層として二次多項式関数を実装したい場合は、次のようにすることができます-

多項式関数があるとします-

f(x) = 3x2

上記の多項式関数の導関数は次のようになります-

f(x) = 6 x

上記の多項式関数の最終層クラスは次のようになります-

*testlayer.py*
from pybrain.structure.modules.neuronlayer import NeuronLayer
class PolynomialLayer(NeuronLayer):
   def _forwardImplementation(self, inbuf, outbuf):
      outbuf[:] = 3*inbuf**2
   def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
      inerr[:] = 6*inbuf*outerr

今、下に示すように作成されたレイヤーを利用しましょう-

*testlayer1.py*
from testlayer import PolynomialLayer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tests.helpers import gradientCheck

n = buildNetwork(2, 3, 1, hiddenclass=PolynomialLayer)
n.randomize()

gradientCheck(n)

GradientCheck()は、レイヤーが正常に機能しているかどうかをテストします。レイヤーが使用されているネットワークをgradientCheck(n)に渡す必要があります。

出力

C:\pybrain\pybrain\src>python testlayer1.py
Perfect gradient