def load_tensor_params_ae(ae): ae = load(ae) W1 = ae.W1 W2 = ae.W2 b1 = ae.b1 b2 = ae.b2 G = ae.G return W1, W2, b1, b2, G
def load_tensor_params_ae(ae): ae = load(ae) W1=ae.W1 W2=ae.W2 b1=ae.b1 b2=ae.b2 G =ae.G return W1,W2,b1,b2,G
def load_numerical_params_ae(ae): ae = load(ae) W1 = ae.W1.get_value(borrow=True) W2 = ae.W2.get_value(borrow=True) b1 = ae.b1.get_value(borrow=True) b2 = ae.b2.get_value(borrow=True) G = ae.G.get_value(borrow=True) return W1, W2, b1, b2, G
def load_numerical_params_ae(ae): ae = load(ae) W1=ae.W1.get_value(borrow=True) W2=ae.W2.get_value(borrow=True) b1=ae.b1.get_value(borrow=True) b2=ae.b2.get_value(borrow=True) G =ae.G.get_value(borrow= True) return W1,W2,b1,b2,G
def load_tensor_params_ae(ae): ae = load(ae) W1 = ae.W1 W2 = ae.W2 b1 = ae.b1 b2 = ae.b2 G = ae.G G_decay = ae.G_decay multi_sparse_weight = ae.multi_sparse_weight multi_sparsity = ae.multi_sparsity return W1, W2, b1, b2, G, G_decay, multi_sparsity, multi_sparse_weight
def load_tensor_params_ae(ae): ae = load(ae) W1=ae.W1 W2=ae.W2 b1=ae.b1 b2=ae.b2 G =ae.G G_decay = ae.G_decay multi_sparse_weight = ae.multi_sparse_weight multi_sparsity = ae.multi_sparsity return W1,W2,b1,b2,G,G_decay,multi_sparsity,multi_sparse_weight
def load_numerical_params_sae(sae): sae = load(sae) W1 = sae.W1.get_value(borrow=True) W2 = sae.W2.get_value(borrow=True) b1 = sae.b1.get_value(borrow=True) b2 = sae.b2.get_value(borrow=True) G = sae.G.get_value(borrow=True) W3 = sae.W3.get_value(borrow=True) W4 = sae.W4.get_value(borrow=True) b3 = sae.b3.get_value(borrow=True) b4 = sae.b4.get_value(borrow=True) G_share = sae.G_share.get_value(borrow=True) return W1, W2, b1, b2, G, W3, W4, b3, b4, G_share
def load_numerical_params_sae(sae): sae = load(sae) W1=sae.W1.get_value(borrow=True) W2=sae.W2.get_value(borrow=True) b1=sae.b1.get_value(borrow=True) b2=sae.b2.get_value(borrow=True) G =sae.G.get_value(borrow= True) W3=sae.W3.get_value(borrow=True) W4=sae.W4.get_value(borrow=True) b3=sae.b3.get_value(borrow=True) b4=sae.b4.get_value(borrow=True) G_share =sae.G_share.get_value(borrow= True) return W1,W2,b1,b2,G,W3,W4,b3,b4,G_share
def test_AE(data='', validationdata='', param_list=None, missing=True, missing_rate=0.4, learning_rate=0.08, training_epochs=1000, batch_size=3000, output_folder='dA_plots'): ################################### # Initializing training dataset # #################################### datasets,indi_matrix,data_test,indi_matrix_test,n_train_batches,numMod,raw,trainstats_list,visible_size_Mod = \ load_data(param_list,data,batch_size,missing_rate,train = True) #################################### # Initializing validation dataset # #################################### valid_batch_size = 306 validationset,indi_matrix_validation,n_valid_batches = \ load_data(param_list,validationdata,valid_batch_size, missing_rate,train = False) # start-snippet-2 # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch x = T.matrix('x') y = T.matrix('y') # the data is presented as rasterized images # end-snippet-2 #################################### # BUILDING THE MODEL NO CORRUPTION # #################################### rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2**30)) # indi_matrix = theano.shared(numpy.asarray(indi_matrix,dtype=theano.config.floatX),name='indi_matrix', borrow=True) # # datasets = datasets * indi_matrix ae = AE( numpy_rng=rng, theano_rng=theano_rng, input=x, indi_matrix=y, bias_matrix=None, n_visible=raw.shape[1], n_hidden=raw.shape[1], W1=None, W2=None, bhid=None, bvis=None, missing=missing, param_list=param_list, ) cost, updates = ae.get_cost_updates(learning_rate) train_ae = theano.function( [index], cost, updates=updates, givens={ x: datasets[index * batch_size:(index + 1) * batch_size], y: indi_matrix[index * batch_size:(index + 1) * batch_size] }, on_unused_input='warn', ) validate_cost = ae.get_cost() validate_ae = theano.function( [index], validate_cost, givens={ x: validationset[index * valid_batch_size:(index + 1) * valid_batch_size], y: indi_matrix_validation[index * valid_batch_size:(index + 1) * valid_batch_size] }, on_unused_input='warn', ) ############ # TRAINING # ############ # go through training epochs ############### # TRAIN MODEL # ############### print('... training the model') # early-stopping parameters patience = 5000 # look as this many examples regardless patience_increase = 2 # wait this much longer when a new best is # found improvement_threshold = 0.995 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience / 2) # go through this many # minibatche before checking the network # on the validation set; in this case we # check every epoch best_validation_loss = numpy.inf start_time = timeit.default_timer() done_looping = False epoch = 0 best_epoch = 0 while (epoch < training_epochs) and (not done_looping): epoch = epoch + 1 c = [] for minibatch_index in range(int(n_train_batches)): a = train_ae(minibatch_index) c.append(a) # iteration number indicate how many batches we have already runned on iter = (epoch - 1) * n_train_batches + minibatch_index if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [ validate_ae(i) for i in range(int(n_valid_batches)) ] this_validation_loss = numpy.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation cost %f ' % (epoch, minibatch_index + 1, n_train_batches, this_validation_loss)) # if we got the best validation score until now if this_validation_loss < best_validation_loss: #improve patience if loss improvement is good enough if this_validation_loss < best_validation_loss * \ improvement_threshold: patience = max(patience, iter * patience_increase) best_validation_loss = this_validation_loss # save the best model print('saving the model for epoch %i' % epoch) best_epoch = epoch #f = open('../Result_test/best_model_epoch_' +str() + '.txt', 'w') save( '../Result_test/best_model_epoch_' + str(epoch) + '.pkl', ae) if patience <= iter: done_looping = True break end_time = timeit.default_timer() print(('Optimization complete with best validation score of %f') % (best_validation_loss)) print( sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.1fs' % ((end_time - start_time)))) #################################### # computing RMSE and error ratio # #################################### print('Best epoch is %i' % best_epoch) print('Now we starting computing the RMSE and error ratio') ae = load('../Result_test/best_model_epoch_' + str(best_epoch) + '.pkl') W1 = ae.W1.get_value(borrow=True) W2 = ae.W2.get_value(borrow=True) b1 = ae.b1.get_value(borrow=True) b2 = ae.b2.get_value(borrow=True) G = ae.G.get_value(borrow=True) bias_matrix = None if missing: bias_matrix = ae.bias_matrix.get_value(borrow=True) y = get_hidden_values(data_test, indi_matrix_test, W1, b1, G, bias_matrix, missing) reconstruction = get_reconstructed_input(y, W2, b2, G, missing) print(reconstruction) numpy.savetxt('../Result_test/output_' + str(best_epoch) + '.txt', reconstruction, delimiter=',') # output = ae.get_cost(get_reconstruction=True) # # reconstruction_ae = theano.function( # [index], # output, # givens={ # x: datasets[index * batch_size: (index + 1) * batch_size], # y: indi_matrix[index * batch_size: (index + 1) * batch_size] # }, # on_unused_input='warn', # ) # # for minibatch_index in range(int(n_train_batches)): # # reconstruction = reconstruction_ae(minibatch_index) # # print(reconstruction.shape) #### denormalize the data f = open('../Result_test/AE_' + str(best_epoch) + '.txt', 'w') for i in range(int(numMod)): numpy.savetxt('../Result_test/Raw_' + str(i) + '.txt', raw[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], delimiter=',') numpy.savetxt('../Result_test/Recstru_' + str(i) + '_' + str(best_epoch) + '.txt', denormActiv( reconstruction[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], trainstats_list[i]), delimiter=',') print( f, 'AE RMSE for Modality', i, str( RMSE( raw[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], denormActiv( reconstruction[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], trainstats_list[i])))) f.write('AE RMSE for Modality' + '\t' + str(i) + '\t' + str( RMSE( raw[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], denormActiv( reconstruction[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], trainstats_list[i]))) + '\n') print( f, 'AE error ratio for Modality', i, str( error_ratio( raw[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], denormActiv( reconstruction[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], trainstats_list[i])))) f.write('AE error ratio for Modality' + '\t' + str(i) + '\t' + str( error_ratio( raw[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], denormActiv( reconstruction[:, i * visible_size_Mod:(i + 1) * visible_size_Mod], trainstats_list[i]))) + '\n')