User Tools

Site Tools


neurali:pybrain_costruire_una_rete

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
neurali:pybrain_costruire_una_rete [2015/07/05 18:56] profproneurali:pybrain_costruire_una_rete [2020/06/08 22:20] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +======Costruire una rete======
 +In questo tutorial vengono utilizzate delle funzioni matematiche python, come:
  
 +  * numpy.random.multivariate_normal (funzione gaussiana)
 +  * numpy.diag
 +    * permette di costruire un'array diagonale oppure di estrarre la diagonale da un array
 +  * range
 +    * For example, range(4) returns [0, 1, 2, 3].
 +  * xrange
 +    * Like range(), but instead of returning a list, returns an object that generates the numbers in the range on demand
 +  * numpy.arange
 +    * Like range(), but instead of returning a list, returns an ndarray
 +
 +======classe FeedForwardNetwork======
 +
 +  - creare la rete (costruttore)
 +  - creare i layer (costruttore specificando: n. percettroni e funzione attivazione)
 +  - aggiungere i //moduli// alla rete a partire dai layer (segnando quali sono di input o di output)
 +  - creare le connessioni tra percettroni i diversi moduli (esempio: [[neurali:fully connected]])
 +  - aggiungere le //connessioni// alla rete
 +  - ordinare i moduli
 +  - (i pesi sono inizialmente posti random?)
 +  - attivazione (fornisci un input, ottieni un output)
 +
 +Tutti i seguenti hanno nome iniziale maiuscola: sono costruttori: 
 +  - FeedForwardNetwork
 +  - LinearLayer (vedere perché usare [[neurali:funzione attivazione linear]]
 +  - SigmoidLayer ((vedere perché usare [[neurali:funzione attivazione logistic sigmoid]]
 +  - FullConnection
 +
 +<code python>
 +  import pybrain.structure
 +  n = pybrain.structure.FeedForwardNetwork()
 +
 +  layerInput = pybrain.structure.LinearLayer(2)
 +  layerNasocosto = pybrain.structure.SigmoidLayer(3)
 +  layerOutput = pybrain.structure.LinearLayer(1)
 +</code>
 +
 +{{ :neurali:pybrain-tut1.png?nolink&200 |}}
 +
 +In  alternativa, si potrebbe specificare anche il nome del layer
 +  layerInput = pybrain.structure.LinearLayer(2), name="prova")
 +
 +<code python>
 +  n.addInputModule(layerInput)
 +  n.addModule(layerNascosto)
 +  n.addOutputModule(layerOutput)
 +  
 +
 +  in_to_hidden = pybrain.structure.connections.full.FullConnection(layerInput, layerNascosto)
 +  hidden_to_out = pybrain.structure.connections.full.FullConnection(layerNascosto,layerOutput)
 +
 +
 +  n.addConnection(in_to_hidden)
 +  n.addConnection(hidden_to_out)
 +
 +  n.sortModules()  # importante
 +</code>
 +  
 +  
 +{{ :neurali:pybrain-tut2.png?nolink&200 |}}
 +
 +<code python>
 +  print n # stampa i _nomi_ degli elementi, che non sono stati definiti, quindi sono random
 +</code>
 +  
 +Output
 +
 +<code> 
 +FeedForwardNetwork-6
 + Modules:
 +    [<LinearLayer 'LinearLayer-3'>, 
 +     <SigmoidLayer 'SigmoidLayer-7'>, 
 +     <LinearLayer 'LinearLayer-8'>]
 + Connections:
 +    [<FullConnection 'FullConnection-4': 'LinearLayer-3' -> 'SigmoidLayer-7'>, 
 +     <FullConnection 'FullConnection-5': 'SigmoidLayer-7' -> 'LinearLayer-8'>]
 +</code>
 +
 +
 +In alternativa esiste una funzione scorciatoia
 +  import buildNetwork
 +  net = buildNetwork(2, 3, 1)
 +
 +
 +Questa istruzione esegue la funzione di rete fornendo in input la coppia (1,2)
 +
 +<code python> 
 +  print n.activate([1, 2])   # stampa output rete (un valore scalare in questo esempio)
 +</code>
 +
 +L'output sarà sempre diverso, anche fornendo in input sempre la stessa coppia (1,2) perché ogni volta che si crea la rete, i pesi delle connessioni sono inizializzati random. Se si desidera visualizzare tali pesi, si usa il comando
 +
 +<code python>
 +  print n.params   # stampa i pesi delle connessioni (9 linee in nero nella figura)
 +</code>
 +
 +{{ :neurali:pybrain-tut3.png?nolink&200 |}}
 +
 +Esempio di output dei due precedenti comandi
 +
 +<code python>
 +[-2.3085972]
 +
 +[-0.62274409 
 + -0.39921839 
 + -0.23909193 
 +  1.00736063
 + -1.24427458 
 +  1.06840813
 + -0.47068614
 + -0.53789443
 + -2.47679769]
 +</code>
 +
 +<code python>
 +print in_to_hidden.params # mostra solo i pesi delle prime 6 connessioni
 +</code>
 +
 +<code python>
 +
 +</code>
neurali/pybrain_costruire_una_rete.txt · Last modified: 2020/06/08 22:20 by 127.0.0.1