def main(): # read in the data base_path = os.path.dirname(__file__) file_path = os.path.join(base_path, "data", "patches.mat") data = loadmat(file_path)['X'] # preprocess the data data -= data.mean(axis=0) data = whiten(data) data = np.float32(data) # define the model variables weight_dims = (100, 256) # network architecture w = init_weights(weight_dims) # random weights x = t.fmatrix() # symbolic variable for data # build model and define training functions model = SparseFilter(w, x) train_fn = training_functions(data, model, weight_dims) # train the model using L-BFGS weights = minimize(train_fn, model.w.eval().flatten(), method='L-BFGS-B', jac=True, options={'maxiter': 100, 'disp': True}) # grab the weights and visualize them weights = weights.x.reshape(weight_dims[0], weight_dims[1]) visualize.drawplots(weights.T, 'y', 'n', 1)
#################### MAIN SCRIPT ######################### # load data, normalize, and convert to float32 basepath = os.path.dirname(__file__) filename = 'patches.mat' filepath = os.path.join(basepath, "data", filename) data = loadmat(filepath)['data'] data -= data.mean(axis=0) data = np.float32(data) # construct the network gSize = 2 step = 1 wDims = [[225, 256]] model = sparse_filtering.network(model_type='groupSF', wDims=wDims, p=None, gMat=None, gSize=gSize, step=step, lr=0.01) train = model.training_functions(data) # train the sparse filtering network maxIter = 100 cost_master = [] for i in range(maxIter): cost, w = train[0]() cost_master.append(cost) print("The cost at iteration %i: %f" %(i, cost)) plotCost(cost_master) drawplots(w.T)
filename = 'patches.mat' filepath = os.path.join(basepath, "data", filename) data = loadmat(filepath)['data'] data -= data.mean(axis=0) data = np.float32(data) # construct the network gSize = 2 step = 1 wDims = [[225, 256]] model = sparse_filtering.network(model_type='groupSF', wDims=wDims, p=None, gMat=None, gSize=gSize, step=step, lr=0.01) train = model.training_functions(data) # train the sparse filtering network maxIter = 100 cost_master = [] for i in range(maxIter): cost, w = train[0]() cost_master.append(cost) print("The cost at iteration %i: %f" % (i, cost)) plotCost(cost_master) drawplots(w.T)