Skip to content

Commit

Permalink
Get rid of network module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Shevchuk committed Nov 25, 2016
1 parent 92f4702 commit 2c6f295
Show file tree
Hide file tree
Showing 49 changed files with 202 additions and 222 deletions.
4 changes: 4 additions & 0 deletions neupy/algorithms/__init__.py
@@ -1,3 +1,4 @@
# Algorithms
from .gd.base import *
from .gd.lev_marq import *
from .gd.quasi_newton import *
Expand Down Expand Up @@ -46,3 +47,6 @@
from .linear.perceptron import *

from .rbm import *

# Other
from .utils import StopTrainingException
5 changes: 2 additions & 3 deletions neupy/algorithms/associative/base.py
Expand Up @@ -3,8 +3,7 @@
from neupy import init
from neupy.utils import format_data
from neupy.core.properties import IntProperty, ParameterProperty, ArrayProperty
from neupy.network import BaseNetwork
from neupy.network.utils import step_function
from neupy.algorithms.base import BaseNetwork


__all__ = ('BaseStepAssociative',)
Expand Down Expand Up @@ -164,7 +163,7 @@ def init_layers(self):
def predict(self, input_data):
input_data = format_data(input_data, is_feature1d=False)
raw_output = input_data.dot(self.weight) + self.bias
return step_function(raw_output)
return np.where(raw_output > 0, 1, 0)

def train(self, input_train, *args, **kwargs):
input_train = format_data(input_train, is_feature1d=False)
Expand Down
2 changes: 1 addition & 1 deletion neupy/algorithms/associative/oja.py
Expand Up @@ -2,7 +2,7 @@

from neupy.utils import format_data, NotTrainedException
from neupy.core.properties import IntProperty, ParameterProperty
from neupy.network.base import BaseNetwork
from neupy.algorithms.base import BaseNetwork
from neupy import init


Expand Down
4 changes: 2 additions & 2 deletions neupy/network/base.py → neupy/algorithms/base.py
Expand Up @@ -13,7 +13,7 @@
Property)
from .summary_info import SummaryTable, InlineSummary
from .utils import (iter_until_converge, shuffle, normalize_error,
StopNetworkTraining)
StopTrainingException)


__all__ = ('BaseNetwork',)
Expand Down Expand Up @@ -435,7 +435,7 @@ def train(self, input_train, target_train=None, input_test=None,

is_first_iteration = False

except StopNetworkTraining as err:
except StopTrainingException as err:
# TODO: This notification breaks table view in terminal.
# I need to show it in a different way.
logs.message("TRAIN", "Epoch #{} stopped. {}"
Expand Down
2 changes: 1 addition & 1 deletion neupy/algorithms/competitive/art.py
Expand Up @@ -5,7 +5,7 @@
from neupy.utils import format_data
from neupy.core.properties import (ProperFractionProperty,
IntProperty)
from neupy.network.base import BaseNetwork
from neupy.algorithms.base import BaseNetwork


__all__ = ('ART1',)
Expand Down
Expand Up @@ -15,9 +15,9 @@
is_sequential)
from neupy.helpers import table
from neupy.core.properties import ChoiceProperty
from neupy.network import errors
from .learning import SupervisedLearningMixin
from .base import BaseNetwork
from neupy.algorithms.base import BaseNetwork
from neupy.algorithms.learning import SupervisedLearningMixin
from .gd import errors


__all__ = ('ConstructableNetwork',)
Expand Down
4 changes: 2 additions & 2 deletions neupy/algorithms/gd/base.py
Expand Up @@ -10,9 +10,9 @@

from neupy.core.config import Configurable
from neupy.core.properties import Property, BoundedProperty
from neupy.network import ConstructableNetwork
from neupy.utils import as_tuple
from . import addon_types
from neupy.algorithms.constructor import ConstructableNetwork
from neupy.algorithms.gd import addon_types


__all__ = ('GradientDescent', 'MinibatchGradientDescent')
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions neupy/algorithms/gd/lev_marq.py
Expand Up @@ -5,10 +5,9 @@
import numpy as np

from neupy.utils import asfloat
from neupy.network import errors
from neupy.core.properties import BoundedProperty, ChoiceProperty
from neupy.algorithms import GradientDescent
from neupy.algorithms.gd import NoStepSelection
from neupy.algorithms.gd import NoStepSelection, errors
from neupy.algorithms.utils import (parameters2vector, iter_parameter_values,
setup_parameter_updates)

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions neupy/algorithms/linear/base.py
@@ -1,7 +1,7 @@
from neupy.utils import is_list_of_integers
from neupy.layers.connections import NetworkConnectionError, LayerConnection
from neupy.network import ConstructableNetwork
from neupy.layers import Step, Input
from neupy.layers.connections import NetworkConnectionError, LayerConnection
from neupy.algorithms.constructor import ConstructableNetwork


__all__ = ('BaseLinearNetwork',)
Expand Down
3 changes: 1 addition & 2 deletions neupy/algorithms/memory/bam.py
Expand Up @@ -3,8 +3,7 @@
import numpy as np

from neupy.utils import format_data, NotTrainedException
from neupy.network.utils import step_function
from .utils import bin2sign, hopfield_energy
from .utils import bin2sign, hopfield_energy, step_function
from .base import DiscreteMemory


Expand Down
4 changes: 2 additions & 2 deletions neupy/algorithms/memory/cmac.py
Expand Up @@ -2,8 +2,8 @@

from neupy.utils import format_data
from neupy.core.properties import IntProperty
from neupy.network.learning import SupervisedLearningMixin
from neupy.network.base import BaseNetwork
from neupy.algorithms.learning import SupervisedLearningMixin
from neupy.algorithms.base import BaseNetwork


__all__ = ('CMAC',)
Expand Down
3 changes: 1 addition & 2 deletions neupy/algorithms/memory/discrete_hopfield_network.py
Expand Up @@ -3,9 +3,8 @@
import numpy as np

from neupy.utils import format_data
from neupy.network.utils import step_function
from neupy.core.properties import Property
from .utils import bin2sign, hopfield_energy
from .utils import bin2sign, hopfield_energy, step_function
from .base import DiscreteMemory


Expand Down
17 changes: 16 additions & 1 deletion neupy/algorithms/memory/utils.py
Expand Up @@ -2,7 +2,7 @@
from numpy.core.umath_tests import inner1d


__all__ = ('bin2sign', 'hopfield_energy')
__all__ = ('bin2sign', 'hopfield_energy', 'step_function')


def bin2sign(matrix):
Expand Down Expand Up @@ -43,3 +43,18 @@ def hopfield_energy(weight, input_data, output_data):
Hopfield energy for specific data and weights.
"""
return -0.5 * inner1d(input_data.dot(weight), output_data)


def step_function(input_value):
"""
Step function.
Parameters
----------
input_value : array-like
Returns
-------
array-like
"""
return np.where(input_value > 0, 1, 0)
4 changes: 2 additions & 2 deletions neupy/algorithms/rbfn/grnn.py
Expand Up @@ -2,8 +2,8 @@

from neupy.utils import format_data, NotTrainedException
from neupy.core.properties import BoundedProperty
from neupy.network.base import BaseNetwork
from neupy.network.learning import LazyLearningMixin
from neupy.algorithms.base import BaseNetwork
from neupy.algorithms.learning import LazyLearningMixin
from .utils import pdf_between_data


Expand Down
4 changes: 2 additions & 2 deletions neupy/algorithms/rbfn/pnn.py
Expand Up @@ -2,8 +2,8 @@

from neupy.utils import format_data, NotTrainedException
from neupy.core.properties import BoundedProperty
from neupy.network.base import BaseNetwork
from neupy.network.learning import LazyLearningMixin
from neupy.algorithms.base import BaseNetwork
from neupy.algorithms.learning import LazyLearningMixin
from neupy.algorithms.gd.base import MinibatchTrainingMixin

from .utils import pdf_between_data
Expand Down
2 changes: 1 addition & 1 deletion neupy/algorithms/rbfn/rbf_kmeans.py
Expand Up @@ -7,7 +7,7 @@
from neupy.utils import format_data
from neupy.core.properties import IntProperty
from neupy.algorithms.gd import NoStepSelection
from neupy.network.base import BaseNetwork
from neupy.algorithms.base import BaseNetwork


__all__ = ('RBFKMeans',)
Expand Down
4 changes: 2 additions & 2 deletions neupy/algorithms/rbm.py
Expand Up @@ -4,8 +4,8 @@
import numpy as np

from neupy.core.properties import IntProperty, ParameterProperty
from neupy.network.constructor import BaseAlgorithm
from neupy.network.base import BaseNetwork
from neupy.algorithms.base import BaseNetwork
from neupy.algorithms.constructor import BaseAlgorithm
from neupy.algorithms.gd.base import (MinibatchTrainingMixin,
average_batch_errors)
from neupy.layers.base import create_shared_parameter
Expand Down
File renamed without changes.
107 changes: 105 additions & 2 deletions neupy/algorithms/utils.py
@@ -1,10 +1,19 @@
from itertools import chain

import numpy as np
import theano.tensor as T


__all__ = ('parameters2vector', 'iter_parameter_values',
'setup_parameter_updates')
__all__ = ('StopTrainingException', 'shuffle', 'parameters2vector',
'iter_parameter_values', 'setup_parameter_updates',
'iter_until_converge', 'normalize_error')


class StopTrainingException(Exception):
"""
Exception that needs to be triggered in case of
early training interruption.
"""


def iter_parameter_values(network):
Expand Down Expand Up @@ -78,3 +87,97 @@ def setup_parameter_updates(parameters, parameter_update_vector):
start_position = end_position

return updates


def iter_until_converge(network, epsilon, max_epochs):
logs = network.logs

# Trigger first iteration and store first error term
yield network.last_epoch

previous_error = error_delta = network.errors.last()
epoch = network.last_epoch

while error_delta > epsilon:
epoch = epoch + 1
network.last_epoch += 1

yield epoch

last_error = network.errors.last()
error_delta = abs(last_error - previous_error)
previous_error = last_error

if epoch >= max_epochs and error_delta > epsilon:
logs.message("TRAIN", "Epoch #{} interrupted. Network didn't "
"converge after {} iterations"
"".format(epoch, max_epochs))
return

if np.isnan(error_delta) or np.isinf(error_delta):
logs.message("TRAIN", "Epoch #{} interrupted. Network error value is "
"NaN or Inf.".format(epoch))
else:
logs.message("TRAIN", "Epoch #{} interrupted. Network converged."
"".format(epoch))


def shuffle(*arrays):
"""
Make a random shuffle for all arrays.
Parameters
----------
*arrays
List of arrays that should be shuffled.
Returns
-------
list
List of arrays that contain shuffeled input data.
"""
if not arrays:
return tuple()

arrays_without_none = [array for array in arrays if array is not None]

if not arrays_without_none:
return arrays

first = arrays_without_none[0]
n_samples = first.shape[0]

for array in arrays_without_none:
if n_samples != array.shape[0]:
raise ValueError("Cannot shuffle matrices. All matrices should "
"have the same number of rows")

indices = np.arange(n_samples)
np.random.shuffle(indices)

arrays = list(arrays)
for i, array in enumerate(arrays):
arrays[i] = array[indices] if array is not None else None

if len(arrays) == 1:
return arrays[0]

return arrays


def normalize_error(output):
"""
Normalize error output when result is non-scalar.
Parameters
----------
output : array-like
Input can be any numpy array or matrix.
Returns
-------
int, float, None
Return sum of all absolute values.
"""
if output is not None:
return np.sum(output)
2 changes: 1 addition & 1 deletion neupy/datasets/reber.py
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from neupy.network.utils import shuffle
from neupy.algorithms.utils import shuffle


__all__ = ('make_reber', 'is_valid_by_reber', 'make_reber_classification')
Expand Down
2 changes: 1 addition & 1 deletion neupy/estimators.py
@@ -1,6 +1,6 @@
from functools import wraps

from neupy.network import errors
from neupy.algorithms.gd import errors
from neupy.utils import format_data, number_type


Expand Down
4 changes: 0 additions & 4 deletions neupy/network/__init__.py

This file was deleted.

0 comments on commit 2c6f295

Please sign in to comment.