PyBrain - Koneksi

Koneksi berfungsi mirip dengan lapisan; satu-satunya perbedaan adalah ia menggeser data dari satu node ke node lainnya dalam jaringan.

Dalam bab ini, kita akan belajar tentang -

  • Memahami Koneksi
  • Membuat Koneksi

Memahami Koneksi

Berikut adalah contoh kerja koneksi yang digunakan saat membuat jaringan.

Contoh

ffy.py

from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection

network = FeedForwardNetwork()

#creating layer for input => 2 , hidden=> 3 and output=>1
inputLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outputLayer = LinearLayer(1)

#adding the layer to feedforward network
network.addInputModule(inputLayer)
network.addModule(hiddenLayer)
network.addOutputModule(outputLayer)

#Create connection between input ,hidden and output
input_to_hidden = FullConnection(inputLayer, hiddenLayer)
hidden_to_output = FullConnection(hiddenLayer, outputLayer)

#add connection to the network
network.addConnection(input_to_hidden)
network.addConnection(hidden_to_output)
network.sortModules()

print(network)

Keluaran

C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-6
Modules:
[<LinearLayer 'LinearLayer-3'>, <SigmoidLayer 'SigmoidLayer-7'>, 
   <LinearLayer 'LinearLayer-8'>]
Connections:
[<FullConnection 'FullConnection-4': 'SigmoidLayer-7' -> 'LinearLayer-8'>, 
   <FullConnection 'FullConnection-5': 'LinearLayer-3' -> 'SigmoidLayer-7'>]

Membuat Koneksi

Di Pybrain, kita dapat membuat koneksi dengan menggunakan modul koneksi seperti yang ditunjukkan di bawah ini -

Contoh

connect.py

from pybrain.structure.connections.connection import Connection
class YourConnection(Connection):
   def __init__(self, *args, **kwargs):
      Connection.__init__(self, *args, **kwargs)
   def _forwardImplementation(self, inbuf, outbuf):
      outbuf += inbuf
   def _backwardImplementation(self, outerr, inerr, inbuf):
      inerr += outer

Untuk membuat koneksi, ada 2 metode - _forwardImplementation () dan _backwardImplementation () .

The _forwardImplementation () disebut dengan buffer output dari modul masuk yang inbuf , dan buffer masukan dari modul keluar disebut outbuf . The inbuf ditambahkan ke modul keluar outbuf .

The _backwardImplementation () disebut dengan outerr , inerr , dan inbuf . Kesalahan modul keluar ditambahkan ke kesalahan modul masuk di _backwardImplementation () .

Sekarang mari kita gunakan YourConnection dalam jaringan.

testconnection.py

from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from connect import YourConnection

network = FeedForwardNetwork()

#creating layer for input => 2 , hidden=> 3 and output=>1
inputLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outputLayer = LinearLayer(1)

#adding the layer to feedforward network
network.addInputModule(inputLayer)
network.addModule(hiddenLayer)
network.addOutputModule(outputLayer)

#Create connection between input ,hidden and output
input_to_hidden = YourConnection(inputLayer, hiddenLayer)
hidden_to_output = YourConnection(hiddenLayer, outputLayer)

#add connection to the network
network.addConnection(input_to_hidden)
network.addConnection(hidden_to_output)
network.sortModules()

print(network)

Keluaran

C:\pybrain\pybrain\src>python testconnection.py
FeedForwardNetwork-6
Modules:
[<LinearLayer 'LinearLayer-3'>, <SigmoidLayer 'SigmoidLayer-7'>, 
   <LinearLayer 'LinearLayer-8'>]
Connections:
[<YourConnection 'YourConnection-4': 'LinearLayer-3' -> 'SigmoidLayer-7'>, 
   <YourConnection 'YourConnection-5': 'SigmoidLayer-7' -> 'LinearLayer-8'>]