diff --git a/main.py b/main.py index bb739e8..973b657 100644 --- a/main.py +++ b/main.py @@ -1,82 +1,82 @@ import torch import numpy as np import random import argparse # own package from weightmatrices.utils import utils # read command line args and kwargs parser = argparse.ArgumentParser() parser.add_argument("--nhidden", nargs="*", type=int, help="number of hidden neurons", default=100) parser.add_argument("--methods", nargs="*", help="methods to be applied to create weight matrix", default='all') parser.add_argument("--save", type=bool, help="whether to save results or not", default=True) args = parser.parse_args() # data import print("loading data") data_loader = utils.load_data() data_matrix = utils.getbigdatamatrix(data_loader) n_in_features = data_matrix.shape[1] # weight matrix creation with different methods if 'pca' in args.methods or 'all' in args.methods: from weightmatrices.algos import pca print("creating weight matrix using PCA") for n_h in args.nhidden: if n_h <= n_in_features: # Number of requested components <= input dimensionality W_pca = pca.get_weightmatrices_pca(data_loader, n_h) if args.save: utils.saveweightmatrix('pca'+str(n_h), W_pca) if 'ica' in args.methods or 'all' in args.methods: from weightmatrices.algos import ica print("creating weight matrix using ICA") for n_h in args.nhidden: if n_h <= n_in_features: # Number of requested components <= input dimensionality W_ica = ica.get_weightmatrices_ica(data_matrix, n_h) if args.save: utils.saveweightmatrix('ica'+str(n_h), W_ica) if 'sc' in args.methods or 'all' in args.methods: from weightmatrices.algos import sc print("creating weight matrix using SC") for n_h in args.nhidden: - W_sc = sc.get_weightmatrices_sc(data_matrix, n_h, getsparsity=True) + W_sc = sc.get_weightmatrices_sc(data_matrix, n_h, getsparsity=False) if args.save: utils.saveweightmatrix('sc'+str(n_h), W_sc) if 'rg' in args.methods or 'all' in args.methods: from weightmatrices.algos import rg print("creating weight matrix using RG") for n_h in args.nhidden: W_rg = rg.get_weightmatrices_rg(data_matrix, n_h) if args.save: utils.saveweightmatrix('rg'+str(n_h), W_rg) if 'rp' in args.methods or 'all' in args.methods: from weightmatrices.algos import rp print("creating weight matrix using RP") for n_h in args.nhidden: W_rp = rp.get_weightmatrices_rp(data_matrix, n_h) if args.save: utils.saveweightmatrix('rp'+str(n_h), W_rp) # jump to interactive mode import code code.interact(local=locals()) # sample plotting #import matplotlib #matplotlib.use('TkAgg') #import matplotlib.pyplot as plt #W = W_pca #plt.ion() #plt.imshow(W[random.sample(range(0, args.nhidden[-1]), 1)[0], :].reshape(28, 28), cmap = 'gray') #plt.show() diff --git a/weightmatrices/algos/ica.py b/weightmatrices/algos/ica.py index b11d239..1f18e59 100644 --- a/weightmatrices/algos/ica.py +++ b/weightmatrices/algos/ica.py @@ -1,17 +1,17 @@ # Independent Component Analysis from sklearn.decomposition import FastICA from weightmatrices.utils import utils def get_ica_trafo_matrix(data_matrix, n_h): transformer = FastICA(n_components=n_h, tol=0.001) data_transformed = transformer.fit_transform(data_matrix) return transformer.components_ def get_weightmatrices_ica(data_matrix, n_h): - print("Creating weigth matrix for ICA for "+str(n_h)+" hidden neurons...") + print("creating weigth matrix for ICA for "+str(n_h)+" hidden neurons...") n_in_features = data_matrix.shape[1] assert n_h <= n_in_features, "Number of requested independent components higher than input dimensionality!" W = get_ica_trafo_matrix(data_matrix, n_h) W = utils.normalise_weightmatrix(W) return W diff --git a/weightmatrices/algos/rg.py b/weightmatrices/algos/rg.py index 9619e41..7d54e4c 100644 --- a/weightmatrices/algos/rg.py +++ b/weightmatrices/algos/rg.py @@ -1,30 +1,31 @@ # Random Gabors import numpy as np import random from weightmatrices.utils import utils def get_gabor_kernel(lbda, theta, psi, sigma, gamma, N): w = np.zeros((N, N)) rm = np.array([[np.cos(theta), np.sin(theta)],[-np.sin(theta), np.cos(theta)]]) for x in range(N): for y in range(N): r = np.matmul(rm, np.array([x, y])-(N/2)) w[x, y] = np.exp(-(r[0]**2 + gamma**2*r[1]**2)/(2*sigma**2)) * np.cos(2*np.pi*r[0]/lbda + psi) return w # these are heuristic boundaries for random Gabors def get_random_gabor(N): return get_gabor_kernel((2*N-N/4)*random.random()+N/4, 2*np.pi*random.random(), 2*np.pi*random.random(), (N-N/8)*random.random()+N/8, random.random(), N) def get_weightmatrices_rg(data_matrix, n_h): + print("creating weigth matrix for RG for "+str(n_h)+" hidden neurons...") N = int(np.sqrt(data_matrix.shape[1])) W = np.zeros((n_h, N**2)) for i in range(n_h): W[i, :] = get_random_gabor(N).flatten() W = utils.normalise_weightmatrix(W) return W diff --git a/weightmatrices/algos/rp.py b/weightmatrices/algos/rp.py index df4a3f9..075e7c7 100644 --- a/weightmatrices/algos/rp.py +++ b/weightmatrices/algos/rp.py @@ -1,10 +1,11 @@ # Random Projections import numpy as np from weightmatrices.utils import utils def get_weightmatrices_rp(data_matrix, n_h): + print("creating weigth matrix for RP for "+str(n_h)+" hidden neurons...") N = data_matrix.shape[1] W = np.random.randn(n_h, N) W = utils.normalise_weightmatrix(W) return W