def predict(self): """ Retruns the forecasts of the run function """ #import pdb; pdb.set_trace() #classifier = self.optimization()[1] self.optimization() classifier = pickle.load(open('best_model3.pkl', 'rb')) predict_model = theano.function( inputs=[classifier.input], outputs=classifier.LinearRegression.y_pred) # We can test it on some examples from test test data = LoadData(self.link) datasets = data.load_data() #import pdb; pdb.set_trace() x_test, y_test = datasets[2] predicted_values = predict_model(x_test.get_value()) fig = figure() _ = plt.scatter(x_test.get_value(), predicted_values, c = 'red', label='Predicted Values') _ = plt.scatter(x_test.get_value(), y_test.get_value(), facecolors='none', edgecolors='r', label='Sample Points') _ = plt.legend() #plt.show() return fig
def predict(self): """ Retruns the forecasts of the run function """ #import pdb; pdb.set_trace() #classifier = self.optimization()[1] self.optimization() classifier = pickle.load(open('best_model3.pkl', 'rb')) predict_model = theano.function( inputs=[classifier.input], outputs=classifier.LinearRegression.y_pred) # We can test it on some examples from test test data = LoadData(self.link) datasets = data.load_data() #import pdb; pdb.set_trace() x_test, y_test = datasets[2] predicted_values = predict_model(x_test.get_value()) fig = figure() _ = plt.scatter(x_test.get_value(), predicted_values, c='red', label='Predicted Values') _ = plt.scatter(x_test.get_value(), y_test.get_value(), facecolors='none', edgecolors='r', label='Sample Points') _ = plt.legend() #plt.show() return fig
def optimize(self): """ Doing the actual optimiziation """ batch_size, finetune_lr = self.batch_size, self.finetune_lr batch_size, n_epochs = self.batch_size, self.training_epochs data = LoadData(self.link) datasets = data.load_data() train_set_x = datasets[0][0] n_train_batches = train_set_x.get_value( borrow=True).shape[0] // batch_size # numpy random generator numpy_rng = numpy.random.RandomState(123) # construct the Deep Belief Network dbn = DBN(numpy_rng=numpy_rng, output_layer=LinearRegression, n_ins=1, hidden_layers_sizes=[3, 3], n_outs=1) # Pretraining pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_x, batch_size=batch_size, k=k) self.__pretraining(dbn.n_layers, pretraining_fns, n_train_batches) # Backpropagation train_fn, validate_model, test_model = dbn.build_finetune_functions( datasets=datasets, batch_size=batch_size, learning_rate=finetune_lr) models = (train_fn, validate_model, test_model) finetuning = Optimization(batch_size=batch_size, n_epochs=n_epochs) finetuning.Backpropagation(models, datasets) test = theano.function(inputs=[dbn.x], outputs=dbn.output_layer.y_pred) prediction = test(datasets[2][0].get_value()) import pdb pdb.set_trace() return dbn
def optimize(self): """ Doing the actual optimiziation """ batch_size, finetune_lr = self.batch_size, self.finetune_lr batch_size, n_epochs = self.batch_size, self.training_epochs data = LoadData(self.link) datasets = data.load_data() train_set_x = datasets[0][0] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size # numpy random generator numpy_rng = numpy.random.RandomState(123) # construct the Deep Belief Network dbn = DBN(numpy_rng=numpy_rng, output_layer = LinearRegression, n_ins=1, hidden_layers_sizes=[3, 3], n_outs=1) # Pretraining pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_x, batch_size=batch_size, k=k) self.__pretraining(dbn.n_layers, pretraining_fns, n_train_batches) # Backpropagation train_fn, validate_model, test_model = dbn.build_finetune_functions( datasets = datasets, batch_size = batch_size, learning_rate = finetune_lr ) models = (train_fn, validate_model, test_model) finetuning = Optimization(batch_size = batch_size, n_epochs = n_epochs) finetuning.Backpropagation(models, datasets) test = theano.function(inputs = [dbn.x], outputs = dbn.output_layer.y_pred) prediction = test(datasets[2][0].get_value()) import pdb; pdb.set_trace() return dbn
def optimization(self): """ Does the optimization """ batch_size, n_epochs = self.batch_size, self.n_epochs data = LoadData(self.link) datasets = data.load_data() x_train, y_train = datasets[0] x_validate, y_validate = datasets[1] classifier, train_model, validate_model = self.__models(datasets) n_train_batches = x_train.get_value(borrow=True).shape[0] // batch_size n_valid_batches = x_validate.get_value( borrow=True).shape[0] // batch_size done_looping = False epoch = 0 train_loss, validation_loss = [], [] patience = 500000 # 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) best_validation_loss = np.inf test_score = 0. while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in range(n_train_batches): #import pdb; pdb.set_trace() minibatch_avg_cost = train_model(minibatch_index) iter = (epoch - 1) * n_train_batches + minibatch_index if (iter + 1) % validation_frequency == 0: validation_losses = [ validate_model(i) for i in range(n_valid_batches) ] this_validation_loss = np.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % (epoch, minibatch_index + 1, n_train_batches, this_validation_loss * 100.)) # 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 # test it on the test set # test_losses = [test_model(i) # for i in range(n_test_batches)] # test_score = np.mean(test_losses) # # print( # ( # ' epoch %i, minibatch %i/%i, test error of' # ' best model %f %%' # ) % # ( # epoch, # minibatch_index + 1, # n_train_batches, # test_score * 100. # ) # ) # save the best model with open('best_model3.pkl', 'wb') as f: pickle.dump(classifier, f) if patience <= iter: done_looping = True break
def sgd_optimization(learning_rate=0.13, n_epochs=1000, dataset='mnist.pkl.gz', batch_size=20): """ Demonstrate stochastic gradient descent optimization of a log-linear model This is demonstrated on MNIST. :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: the path of the MNIST dataset file from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz """ link = '/home/fabian/Documents/DeepLearningTutorials/data/DeepQ.npy' data = LoadData(link) datasets = data.load_data() x_train, y_train = datasets[0] x_valid, y_valid = datasets[1] x = T.matrix('x') index = T.lscalar('index') y = T.vector('y') n_in = 1 n_out = 1 batch_size = 20 import pdb pdb.set_trace() # compute number of minibatches for training, validation and testing n_train_batches = x_train.get_value().shape[0] // batch_size n_valid_batches = x_valid.get_value().shape[0] // batch_size print('... building the model') classifier = PiecewiseLinear_Reinforcement(n_in=1, input=x, n_out=n_out) # the cost we minimize during training is the negative log likelihood of # the model in symbolic format cost = classifier.loss(y) error = classifier.error(y) validate_model = theano.function( inputs=[index], outputs=[cost, error], givens={ x: x_valid[index * batch_size:(index + 1) * batch_size], y: y_valid[index * batch_size:(index + 1) * batch_size] }) # compute the gradient of cost with respect to theta = (W,b) g_W = T.grad(cost=cost, wrt=classifier.W) g_b = T.grad(cost=cost, wrt=classifier.b) # start-snippet-3 # specify how to update the parameters of the model as a list of # (variable, update expression) pairs. updates = [(classifier.W, classifier.W - learning_rate * g_W), (classifier.b, classifier.b - learning_rate * g_b)] # compiling a Theano function `train_model` that returns the cost, but in # the same time updates the parameter of the model based on the rules # defined in `updates` train_model = theano.function( inputs=[index], outputs=[cost, error], updates=updates, givens={ x: x_train[index * batch_size:(index + 1) * batch_size], y: y_train[index * batch_size:(index + 1) * batch_size] }) test = [train_model(i) for i in range(n_train_batches)] 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.005 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience // 2) best_expected_utility = -1 * numpy.inf test_score = 0. #start_time = timeit.default_timer() done_looping = False epoch = 0 while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in range(n_train_batches): minibatch_avg_cost = train_model(minibatch_index)[0] # iteration number iter = (epoch - 1) * n_train_batches + minibatch_index if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set expected_utility = [ -1 * validate_model(i)[0] for i in range(n_valid_batches) ] validation_error = [ validate_model(i)[1] for i in range(n_valid_batches) ] this_expected_utility = numpy.mean(expected_utility) this_validation_error = numpy.mean(validation_error) print( 'epoch %i, minibatch %i/%i, expected Utility %f validation error %f %%' % (epoch, minibatch_index + 1, n_train_batches, this_expected_utility, this_validation_error * 100.)) # if we got the best validation score until now if this_expected_utility > best_expected_utility: #improve patience if loss improvement is good enough if this_expected_utility > best_expected_utility * ( 1 + improvement_threshold): patience = max(patience, iter * patience_increase) best_expected_utility = this_expected_utility # test it on the test set # test_losses = [test_model(i) # for i in range(n_test_batches)] # test_score = numpy.mean(test_losses) # # print( # ( # ' epoch %i, minibatch %i/%i, test error of' # ' best model %f %%' # ) % # ( # epoch, # minibatch_index + 1, # n_train_batches, # test_score * 100. # ) # ) # save the best model # with open('best_model.pkl', 'wb') as f: # # pickle.dump(classifier, f) if patience <= iter: done_looping = True break end_time = timeit.default_timer()
def optimization(self): """ Does the optimization """ batch_size, n_epochs = self.batch_size, self.n_epochs data = LoadData(self.link) datasets = data.load_data() x_train, y_train = datasets[0] x_validate, y_validate = datasets[1] classifier, train_model, validate_model = self.__models(datasets) n_train_batches = x_train.get_value(borrow=True).shape[0] // batch_size n_valid_batches = x_validate.get_value(borrow=True).shape[0] // batch_size done_looping = False epoch = 0 train_loss, validation_loss = [], [] patience = 500000 # 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) best_validation_loss = np.inf test_score = 0. while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in range(n_train_batches): #import pdb; pdb.set_trace() minibatch_avg_cost = train_model(minibatch_index) iter = (epoch - 1) * n_train_batches + minibatch_index if (iter + 1) % validation_frequency == 0: validation_losses = [validate_model(i) for i in range(n_valid_batches)] this_validation_loss = np.mean(validation_losses) print( 'epoch %i, minibatch %i/%i, validation error %f %%' % ( epoch, minibatch_index + 1, n_train_batches, this_validation_loss * 100. ) ) # 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 # test it on the test set # test_losses = [test_model(i) # for i in range(n_test_batches)] # test_score = np.mean(test_losses) # # print( # ( # ' epoch %i, minibatch %i/%i, test error of' # ' best model %f %%' # ) % # ( # epoch, # minibatch_index + 1, # n_train_batches, # test_score * 100. # ) # ) # save the best model with open('best_model3.pkl', 'wb') as f: pickle.dump(classifier, f) if patience <= iter: done_looping = True break
print(" val loss: %f" % (val_loss / n_batch)) if acc is not None: print(" val acc: %f" % (val_acc / n_batch)) else: print("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep)) print("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep)) print("Total training time: %fs" % (time.time() - start_time_begin)) if __name__ == "__main__": # load data # data_loader = LoadData(root_path="data/rgbd-dataset_eval") data_loader = LoadData(root_path="/home/share/rgbd") all_train_rgb_samples, all_train_depth_samples, all_train_labels, all_test_rgb_samples, all_test_depth_samples, all_test_labels = data_loader.load_data( ) session = get_session() # define placeholder x = tf.placeholder(tf.float32, shape=[None, 48, 48, 3], name='x') x_depth = tf.placeholder(tf.float32, shape=[None, 48, 48, 1], name='x_depth') y_ = tf.placeholder(tf.int64, shape=[ None, ], name='y_') # X ############# net_x = tl.layers.InputLayer(x, name='input_x')
print(" val acc: %f" % (val_acc / n_batch)) else: print("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep)) print("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep)) print("Total training time: %fs" % (time.time() - start_time_begin)) y_test = [confusion[i, 0] for i in range(confusion.shape[0])] y_pred = [confusion[i, 1] for i in range(confusion.shape[0])] confusion_matrix(y_test, y_pred) if __name__ == "__main__": # load data data_loader = LoadData(root_path="/home/share/rgbd") all_train_rgb_samples, all_train_depth_samples, all_train_labels, all_test_rgb_samples, all_test_depth_samples, all_test_labels = ( data_loader.load_data() ) session = get_session() # define placeholder x = tf.placeholder(tf.float32, shape=[None, 48, 48, 3], name="x") x_depth = tf.placeholder(tf.float32, shape=[None, 48, 48, 1], name="x_depth") y_ = tf.placeholder(tf.int64, shape=[None], name="y_") # define model(fill your code) def model(x, reuse=tf.AUTO_REUSE, flag="rgb"): W_init = tf.truncated_normal_initializer(stddev=5e-2) W_init2 = tf.truncated_normal_initializer(stddev=0.04) b_init2 = tf.constant_initializer(value=0.1) with tf.variable_scope("model"): net = Conv2d(x, 64, (5, 5), (1, 1), act=tf.nn.relu, padding="SAME", W_init=W_init, name="cnn1" + flag)
def sgd_optimization(learning_rate=0.13, n_epochs=1000, dataset='mnist.pkl.gz', batch_size=20): """ Demonstrate stochastic gradient descent optimization of a log-linear model This is demonstrated on MNIST. :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: the path of the MNIST dataset file from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz """ link = '/home/fabian/Documents/DeepLearningTutorials/data/DeepQ.npy' data = LoadData(link) datasets = data.load_data() x_train, y_train = datasets[0] x_valid, y_valid = datasets[1] x = T.matrix('x') index = T.lscalar('index') y = T.vector('y') n_in = 1 n_out = 1 batch_size = 20 import pdb; pdb.set_trace() # compute number of minibatches for training, validation and testing n_train_batches = x_train.get_value().shape[0] // batch_size n_valid_batches = x_valid.get_value().shape[0] // batch_size print('... building the model') classifier = PiecewiseLinear_Reinforcement(n_in = 1, input = x, n_out = n_out) # the cost we minimize during training is the negative log likelihood of # the model in symbolic format cost = classifier.loss(y) error = classifier.error(y) validate_model = theano.function( inputs=[index], outputs=[cost, error], givens={ x: x_valid[index * batch_size: (index + 1) * batch_size], y: y_valid[index * batch_size: (index + 1) * batch_size] } ) # compute the gradient of cost with respect to theta = (W,b) g_W = T.grad(cost=cost, wrt=classifier.W) g_b = T.grad(cost=cost, wrt=classifier.b) # start-snippet-3 # specify how to update the parameters of the model as a list of # (variable, update expression) pairs. updates = [(classifier.W, classifier.W - learning_rate * g_W), (classifier.b, classifier.b - learning_rate * g_b)] # compiling a Theano function `train_model` that returns the cost, but in # the same time updates the parameter of the model based on the rules # defined in `updates` train_model = theano.function( inputs=[index], outputs=[cost, error], updates=updates, givens={ x: x_train[index * batch_size: (index + 1) * batch_size], y: y_train[index * batch_size: (index + 1) * batch_size] } ) test = [train_model(i) for i in range(n_train_batches)] 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.005 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience // 2) best_expected_utility = -1 * numpy.inf test_score = 0. #start_time = timeit.default_timer() done_looping = False epoch = 0 while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in range(n_train_batches): minibatch_avg_cost = train_model(minibatch_index)[0] # iteration number iter = (epoch - 1) * n_train_batches + minibatch_index if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set expected_utility = [-1*validate_model(i)[0] for i in range(n_valid_batches)] validation_error = [validate_model(i)[1] for i in range(n_valid_batches)] this_expected_utility = numpy.mean(expected_utility) this_validation_error = numpy.mean(validation_error) print( 'epoch %i, minibatch %i/%i, expected Utility %f validation error %f %%' % ( epoch, minibatch_index + 1, n_train_batches, this_expected_utility, this_validation_error * 100. ) ) # if we got the best validation score until now if this_expected_utility > best_expected_utility: #improve patience if loss improvement is good enough if this_expected_utility > best_expected_utility * ( 1 + improvement_threshold): patience = max(patience, iter * patience_increase) best_expected_utility = this_expected_utility # test it on the test set # test_losses = [test_model(i) # for i in range(n_test_batches)] # test_score = numpy.mean(test_losses) # # print( # ( # ' epoch %i, minibatch %i/%i, test error of' # ' best model %f %%' # ) % # ( # epoch, # minibatch_index + 1, # n_train_batches, # test_score * 100. # ) # ) # save the best model # with open('best_model.pkl', 'wb') as f: # # pickle.dump(classifier, f) if patience <= iter: done_looping = True break end_time = timeit.default_timer()