def train_unsupervised_hierarchy(X1, X2, n_epochs=30): g1 = gwr_h_unimodal(n_layers=2, window_size=[3, 3], gwr_pars=pars) g2 = gwr_h_unimodal(n_layers=2, window_size=[3, 3], gwr_pars=pars) g3 = gwr(**pars) X1_ = g1.train(X1, n_epochs=n_epochs) X2_ = g2.train(X2, n_epochs=n_epochs) X = np.hstack((X1_, X2_)) g3.train(X, n_epochs=n_epochs) return g1, g2, g3
def __init__(self, n_layers = 2, window_size = [3, 1], lab_ratio =1 / 2, gwr_pars = []): ''' NOTE: The data X is used in training without window for layer 1, then window_size[0] is used to get the trajectory from the first to the second layer. If you have only two layers, window_size[1] is the output trajectory already windowed. ''' self.n_layers = n_layers self.window_size = window_size self.lab_ratio = lab_ratio if isinstance(gwr_pars, list): self.H = [] for i in range(self.n_layers): try: par = gwr_pars[i] self.H.append(gwr(**par)) except IndexError: self.H.append(gwr()) elif isinstance(gwr_pars, dict): self.H = [gwr(**gwr_pars) for _ in range(self.n_layers)]
import numpy as np import networkx as nx import matplotlib.pyplot as plt from neuralgas.oss_gwr import gwr gwr = gwr(tau_b=0.3, tau_n=0.1, kappa=1.05) gwr.G = nx.Graph() gwr.G.add_node(0, attr_dict={'pos': np.array([1, 1]), 'fir': 1}) gwr.G.add_node(1, attr_dict={'pos': np.array([1, 1]), 'fir': 1}) gwr.G.add_edge(0, 1) fir, fir_n = [1], [1] for i in range(10): gwr._update_firing(0) fir.append(gwr.G.node[0]['fir']) fir_n.append(gwr.G.node[1]['fir']) f, ax = plt.subplots(1, 1) ax.set_title('Synapse habituation') ax.plot(fir, label='Best matching synapse') ax.plot(fir_n, label='Neighbor synapse') ax.legend()
import numpy as np import sklearn.datasets import matplotlib.pyplot as plt from neuralgas.oss_gwr import gwr ''' Growing two 2D neural gases to learn the distribution of Gaussian data with different standard deviations. The visualization shows only the positions of the neurons (and not the connections). ''' iris = sklearn.datasets.load_iris() X1 = np.random.normal(loc=2, scale=1.0, size=[1000, 2]) X2 = np.random.normal(loc=2, scale=2, size=[1000, 2]) g1 = gwr(act_thr=0.75, random_state=None, max_size=30) g1.train(X1, n_epochs=3) Xg1 = g1.get_positions() g2 = gwr(act_thr=0.75, random_state=None, max_size=30) g2.train(X2, n_epochs=3) Xg2 = g2.get_positions() marks = 8 f, ax = plt.subplots(2, 2) ax[0, 0].scatter(X1[:, 0], X1[:, 1], s=marks) ax[1, 0].scatter(X2[:, 0], X2[:, 1], s=marks) ymin, ymax = ax[1, 0].get_ylim() xmin, xmax = ax[1, 0].get_xlim() ax[0, 1].scatter(Xg1[:, 0], Xg1[:, 1], s=marks)
from __future__ import division import sklearn.datasets import matplotlib.pyplot as plt from neuralgas.oss_gwr import gwr iris = sklearn.datasets.load_iris() X = iris.data y = iris.target g1 = gwr(act_thr=0.75, random_state=12345) g1.train(X[:, :2], n_epochs=1) Xg1 = g1.get_positions() g1.train(X[:, :2], n_epochs=19, warm_start=True) Xg1_ = g1.get_positions() g2 = gwr(act_thr=0.75, random_state=1234) g2.train(X[:, 2:], n_epochs=1) Xg2 = g2.get_positions() g2.train(X[:, 2:], n_epochs=19, warm_start=True) Xg2_ = g2.get_positions() marks = 15 f, ax = plt.subplots(2, 3) ax[0, 0].scatter(X[:, 0], X[:, 1], s=marks) ymin, ymax = ax[0, 0].get_ylim() xmin, xmax = ax[0, 0].get_xlim() ax[0, 1].scatter(Xg1[:, 0], Xg1[:, 1], s=marks) ax[0, 2].scatter(Xg1_[:, 0], Xg1_[:, 1], s=marks) for i in range(1, 3):
X = iris.data X1 = X[:, :2] X2 = X[:, 2:] y = iris.target g1 = gwr_h_unimodal(n_layers=2, window_size=[1, 2], gwr_pars=[{ 'act_thr': 0.7 }, { 'act_thr': 0.7 }]) g2 = gwr_h_unimodal(n_layers=2, window_size=[1, 2], gwr_pars=[{ 'act_thr': 0.7 }, { 'act_thr': 0.7 }]) g3 = gwr() XX1 = g1.train(X1) XX2 = g2.train(X2) X3 = np.hstack((XX1, XX2)) g3.train(X3) Xf = _propagate_trajectories(X3, network=g3, ws=2) # then you can take a oss_gwr