Skip to content

Commit

Permalink
"corrected rando
Browse files Browse the repository at this point in the history
m state handling. Added a new html theme for documentation. Modified default
columns and rows value in NeuralMap constructor"
  • Loading branch information
FrancoBobadilla committed Oct 23, 2020
1 parent c6b9cd0 commit 0908dcb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
10 changes: 9 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# html_theme = 'alabaster'
html_theme = "sphinx_rtd_theme"

html_theme_options = {
"navigation_depth": 3,
"logo_only": True,
}

html_logo = "logo.png"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
27 changes: 17 additions & 10 deletions neural_map/_neural_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class NeuralMap:
def __init__(self,
variables,
metric,
columns=20,
rows=20,
columns=10,
rows=10,
hexagonal=True,
toroidal=False,
relative_positions=None,
Expand Down Expand Up @@ -147,7 +147,6 @@ def distance(first_array, second_array):
self.width = self.columns
self.height = self.rows
self.seed = seed
random.seed(self.seed)

if weights is not None:
_check_inputs.ndarray_and_shape(weights, (self.columns, self.rows, self.variables))
Expand Down Expand Up @@ -181,6 +180,7 @@ def distance(first_array, second_array):
self.current_epoch = None
self._unified_distance_matrix_cache = None
self._hdbscan_cache = [(None, None, None)] * self.columns * self.rows
random.seed(self.seed)

def pca_weights_init(self, data):
"""
Expand All @@ -190,6 +190,7 @@ def pca_weights_init(self, data):
and use the same normalization for the training data.
"""
random.seed(self.seed)
principal_components_length, principal_components = eig(cov(transpose(data)))
components_order = argsort(-principal_components_length)

Expand All @@ -204,20 +205,23 @@ def uniform_weights_init(self):
Randomly initialize the weights with values between 0 and 1.
"""
random.seed(self.seed)
self.weights = random.rand(self.columns, self.rows, self.variables)

def standard_weights_init(self):
"""
Randomly initialize the weights with a random distribution with mean of 0 and std of 1.
"""
random.seed(self.seed)
self.weights = random.normal(0., 1., (self.columns, self.rows, self.variables))

def pick_from_data_weights_init(self, data):
"""
Initialize the weights picking random samples from the input data.
"""
random.seed(self.seed)
indices = arange(data.shape[0])

for i in range(self.columns):
Expand All @@ -228,6 +232,7 @@ def train(self,
data,
eval_data=None,
n_epochs=100,
initial_epoch=0,
weights_init_method='standard',
initial_learning_rate=1.0,
final_learning_rate=0.01,
Expand All @@ -254,6 +259,10 @@ def train(self,
n_epochs: int (optional, default 100)
Number of epochs to train.
In each epoch, all observations are processed exactly once, in a random sequence.
initial_epoch: int (optional, default 0)
Initial training epoch.
This might be useful when multiple training sessions are applied
over the same instance.
weights_init_method: string (optional, default 'standard')
Method to initialize weights. It should be one of the followings:
* pca_weights_init
Expand Down Expand Up @@ -387,6 +396,7 @@ def train(self,

_check_inputs.value_type(n_epochs, int)
_check_inputs.positive(n_epochs)
_check_inputs.value_type(initial_epoch, int)
_check_inputs.value_type(initial_learning_rate, float)
_check_inputs.positive(initial_learning_rate)
_check_inputs.value_type(final_learning_rate, float)
Expand Down Expand Up @@ -417,12 +427,6 @@ def train(self,
epochs_quantization_error = zeros(n_epochs)
epochs_topographic_error = zeros(n_epochs)

# generate observation indices to iterate over
indices = arange(len(data))

# set random state
random.seed(self.seed)

# the next variables before the for loop will be used only in case of toroidal topology

# array with map dimensions, do not confuse with the number of columns and rows
Expand All @@ -439,11 +443,14 @@ def train(self,
correction = zeros(self.rows, dtype='int')
correction[center[1] % 2:: 2] = (center[1] % 2) * 2 - 1

for epoch in range(n_epochs):
for epoch in range(initial_epoch, n_epochs):

# set current epoch
self.current_epoch = epoch

# generate observation indices to iterate over
indices = arange(len(data))

# shuffles all observations
random.shuffle(indices)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='neural_map',
version="0.0.4",
version="0.0.5",
author="Franco José Bobadilla",
author_email='1709673@ucc.edu.ar',
description='NeuralMap is a data analysis tool based on Self-Organizing Maps',
Expand Down

0 comments on commit 0908dcb

Please sign in to comment.