# Training num_epochs = 100 # Initialize session to make computations on the Tensorflow graph with tf.Session() as sess: # Initialize model parameters sess.run(fis.init_variables) trn_costs = [] val_costs = [] time_start = time.time() for epoch in range(num_epochs): # Run an update step trn_loss, trn_pred = fis.train(sess, trnData, trnLbls) # Evaluate on validation set val_pred, val_loss = fis.infer(sess, chkData, chkLbls) if epoch % 10 == 0: print("Train cost after epoch %i: %f" % (epoch, trn_loss)) if epoch == num_epochs - 1: time_end = time.time() print("Elapsed time: %f" % (time_end - time_start)) print("Validation loss: %f" % val_loss) # Plot real vs. predicted pred = np.vstack((np.expand_dims(trn_pred, 1), np.expand_dims(val_pred, 1))) plt.figure(1) plt.plot(mg_series) plt.plot(pred) trn_costs.append(trn_loss) val_costs.append(val_loss) # Plot the cost over epochs
# Training num_epochs = 100 # Initialize session to make computations on the Tensorflow graph with tf.Session() as sess: # Initialize model parameters sess.run(fis.init_variables) trn_costs = [] val_costs = [] time_start = time.time() for epoch in range(num_epochs): # Run an update step trn_loss, trn_pred = fis.train(sess, trnData, trnLbls) # Evaluate on validation set val_pred, val_loss = fis.infer(sess, chkData, chkLbls) if epoch % 10 == 0: print("Train cost after epoch %i: %f" % (epoch, trn_loss)) if epoch == num_epochs - 1: time_end = time.time() print("Elapsed time: %f" % (time_end - time_start)) print("Validation loss: %f" % val_loss) # Plot real vs. predicted pred = np.vstack((np.expand_dims(trn_pred, 1), np.expand_dims(val_pred, 1))) plt.figure(1) plt.plot(mg_series) plt.plot(pred) trn_costs.append(trn_loss) val_costs.append(val_loss) # Plot the cost over epochs plt.figure(2)
# Sets the FIS parameters to the ones on the genome fis.setmfs(mus, sigmas, y) pred = fis.infer(data) loss = 1 - nse(pred, lbls) return loss # Runs the evolution cycle start_time = time.time() result = list( differential_evolution(eval_objective, bounds=[(-2, 2)] * n_params, gens=10)) end_time = time.time() print('Evolution time: %f' % (end_time - start_time)) # Gets the last genome best_params = result[-1][0] best_mus = best_params[0:fis.m * fis.n] best_sigmas = best_params[fis.m * fis.n:2 * fis.m * fis.n] best_y = best_params[2 * fis.m * fis.n:2 * fis.m * fis.n + fis.m] # Sets the FIS parameters to the ones of the last genome fis.setmfs(best_mus, best_sigmas, best_y) # Predicts output for the training set best_pred = fis.infer(data) # Plots the real and predicted one series plt.plot(mg_series) plt.plot(best_pred) plt.legend(['Real', 'Predicted']) plt.show() print('Best fitness: %f' % result[-1][1])