show_result = True # If save_result_filename == None or "" then we don't save an image save_result_filename = "result-1-1024-10000" # Append the datetime to the save_result_filename import datetime d = datetime.datetime.now() save_result_filename += "-" + str(d.year) + "-" + str(d.month) + "-" + str(d.day) save_result_filename += "-" + str(d.hour) + str(d.minute) print "Saving results to", save_result_filename # read_amat_file is in helper_functions [X, Y] = read_amat_file(training_file_name, sample_size) print "Training File Read, starting fit" ae = CAE(epochs=num_epochs, n_hiddens=num_hidden_units, schatten_p = schatten_p_value, save_results_file=save_result_filename) ae.fit(X, True) r_X = ae.reconstruct(X[0]) # Show the first image and the reconstructed image fig = plt.figure(1, (1,2)) grid = ImageGrid(fig, 111, nrows_ncols = (1, 2), axes_pad=0.1) grid[0].imshow(numpy.reshape(X[0], (28,-1))) grid[1].imshow(numpy.reshape(r_X, (28,-1))) if save_result_filename != None and save_result_filename != "": plt.savefig(save_result_filename + ".png") if show_result: plt.show()
from train_cae import fit_adagrad, fit_sgd def generate_data(n=10000): t = 12*np.random.rand(n) + 3 x = (t)*0.04*np.sin(t) y = (t)*0.04*np.cos(t) X = np.vstack((x,y)).T return X X = generate_data() cae = CAE(n_hiddens=1000, W=None, c=None, b=None, jacobi_penalty=0.0) cae.init_weights(X.shape[1], dtype=np.float64) theta_sgd = fit_sgd(cae, X, epochs=30, verbose=True, learning_rate=0.1) lim = 0.5 lims = np.arange(-lim, lim, 0.1) x, y = np.meshgrid(lims, lims) gridX = np.vstack( (x.flatten(), y.flatten())).T rX = cae.reconstruct(gridX) dX = rX-gridX plt.close('all') plt.scatter(X[:,0],X[:,1]) plt.quiver(gridX[:,0], gridX[:, 1], dX[:, 0], dX[:,1]) plt.show() #plt.show()