Page MenuHomec4science

main.py
No OneTemporary

File Metadata

Created
Fri, May 10, 08:49
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=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()

Event Timeline