Source code for pymlp.mlp.NeuralNetworkAdapter

import numpy as np
from FFNetwork import FFNetwork
import os


[docs]class FFNetworkAdapter: """Adapter for the self implemented feed-forward neural network.""" def __init__(self, layout, transferFunctions, learningRate): self.layout = layout self.alpha = learningRate self.transferFunctions = transferFunctions self.network = FFNetwork(layout, transferFunctions, learningRate) self.initialNetwork = self.network.clone()
[docs] def clone(self): clonedNetwork = FFNetworkAdapter(self.layout, self.transferFunctions, self.alpha) clonedNetwork.network = self.network.clone() return clonedNetwork
[docs] def forward(self, inpt): assert inpt.ndim == 2, "%s == 2" % inpt.ndim results = map(lambda x: self.network.feedForward(np.array([x])), inpt) # turn to array and remove internal layer results = np.array(results).reshape((len(results), results[0].shape[1])) assert results.ndim == 2, "%s == 2" % results.ndim return results
[docs] def train(self, inpt, target, numberOfIterations, batch=False): assert len(inpt) == len(target) if not batch: for j in xrange(numberOfIterations): for i, datapoint in enumerate(inpt): activations = self.forward(np.array([datapoint])) assert activations.ndim == 2, "%s == 2" % activations.ndim e = target[i] - activations self.network.propagateBack(e ) else: for i in xrange(numberOfIterations): allActivations = list() allStates = list() for i, datapoint in enumerate(inpt): activations = self.forward(np.array([datapoint])) allActivations.append(activations) allStates.append(self.network.O) for i, activations in enumerate(allActivations): o = allStates[i] assert activations.ndim == 2, "%s == 2" % activations.ndim e = target[i] - activations self.network.propagateBackBatch(e, o)
[docs] def saveInitialNetwork(self, template): """Saves the network at the state after construction.""" # TODO 2014-07-08: Check if this method is really necessary self.initialNetwork.save(template)
[docs] def saveCurrentNetwork(self, template): """Saves the network at the current state.""" self.network.save(template)
[docs]class FFNetworkFactory: """Reads the initial weights of the storage files and returns a FFNetworkAdapter based on them.""" def __init__(self, folder): """folder is the folder in which the neural networks are to be stored.""" self.folder = folder
[docs] def createNetwork(self, name, layout, transferFunctions, learningRate, replicationNumber): path = "%s/r%s" % (self.folder, replicationNumber) try: os.makedirs(path) except OSError: # documentation says: raised if file already exists pass files = os.listdir(path) files = filter(lambda x: name in x, files) network = FFNetworkAdapter(layout, transferFunctions, learningRate) if (len(files) > 0): assert len(layout) - 1 == len(files), "%s == %s" % (len(layout) - 1, len(files)) files = map(lambda x: "%s/%s" % (path, x), files) files.sort() network.network.load(files) else: self.saveNetwork(network, name, self.folder, replicationNumber) return network
[docs] def saveNetwork(self, network, name, folder, uniqueIdentifier): """ Saves the network with the given name in the given folder. Parameters: network -- the network to save name -- the name of the network folder -- folder in which to store the network uniqueIdentifier -- an identifier for this particular network, e.g. the replication number """ path = "%s/r%s" % (folder, uniqueIdentifier) try: os.makedirs(path) except OSError: # documentation says: raised if file already exists pass network.saveCurrentNetwork("%s/%s_{0}" % (path, name))