def analize(XX, yy):

    y_pred = regr.predict(XX)
    # orig_y_true = scaler.inverse_transform(yy)
    # orig_y_pred = scaler.inverse_transform(y_pred)
    plot_result(yy, y_pred)
    full_report(yy, y_pred)
Esempio n. 2
0
def analzie(data_set):
    ## Select training example

    y_true = data_set[:, -1, -1]
    predictions = lstm_model.predict(data_set[:, :, :-1])
    y_pred = predictions[:, -1, -1]
    full_report(y_true, y_pred)
    plot_result(y_true, y_pred)
def analzie(data_set, save=False):
    ## Select training example

    y_true = data_set[:, -1, -1]
    predictions = lstm_model.predict(data_set[:, :, :-1])
    y_pred = predictions[:, -1, -1]
    if save:
        np.save('lstm', y_pred)
    full_report(y_true, y_pred)
    plot_result(y_true, y_pred)
Esempio n. 4
0
def analzie(data_loader):
    ## Select training example
    y_pred = []
    y_true = []
    for batch in data_loader:
        x = batch[:, :, :-1]
        y = batch[:, -1, -1]
        with torch.no_grad():
            netout = net(x)
        true = y.cpu().numpy()
        y_true.extend(true)
        pred = netout[:, -1, -1]
        pred = pred.cpu().numpy()
        # print(pred)
        y_pred.extend(pred)
    y_true = np.array(y_true)
    y_pred = np.array(y_pred)
    # orig_y_true = scaler.inverse_transform(y_true)
    # orig_y_pred = scaler.inverse_transform(y_pred)
    full_report(y_true, y_pred)

    plot_result(y_true, y_pred)
Esempio n. 5
0
    hidden_layers = [width] * depth

    new_dir = "{}_{}_{}/N={}/{}_{}/".format(lower_bound, upper_bound,
                                            num_element, N, depth, width)
    dump_dir = working_dir + new_dir
    if not os.path.exists(dump_dir):
        os.makedirs(dump_dir)
    shutil.copy(working_dir + "paras.txt", dump_dir)

    model = CBSolutionNN(N, pivot, hidden_layers)

    start_time = time.time()
    epoch, loss_history = NNUtil.train(model, max_epoch)
    elapsed = time.time() - start_time
    print('Training time: %.4f' % elapsed)

    x_test, u_pred, u_truth = NNUtil.mnn_predict(model, num_tested_per_element)

    error_u = np.sqrt(np.square(u_pred - u_truth).mean())
    print('Error u: %e' % error_u)

    prediction = np.hstack((u_truth, u_pred))

    np.savetxt(dump_dir + 'result.csv',
               X=prediction,
               header="truth,prediction",
               delimiter=',')

    util.plot_result(epoch, loss_history, x_test, u_truth, u_pred, dump_dir,
                     error_u)
    for i in range(N_elem):
        tmp_data = np.zeros((N_pred, 1))
        tmp_data[:, 0] = np.linspace(domain[i], domain[i + 1], N_pred)
        data_in.append(tmp_data)

    Soln = the_DNN.predict(data_in)

    A_in_data = np.concatenate(data_in, 0)  # shape: (N_elem*N_pred, 1)
    pred_in_data = np.reshape(A_in_data, (N_elem * N_pred, ))
    A_soln = np.concatenate(Soln, 0)  # shape: (N_elem*N_pred, 1)
    Soln_flat = np.reshape(A_soln, (N_elem * N_pred, ))

    Exact_soln = the_anal_soln(A_in_data)
    Exact_soln_flat = np.reshape(Exact_soln, (N_elem * N_pred, ))

    error_u = np.sqrt(np.square(Soln_flat - Exact_soln_flat).mean())

    loss_history = np.log10(history.history['loss'])
    epoch = range(len(loss_history))

    with open(dump_dir + "paras.txt", 'w') as writer:
        writer.writelines("N={}\ndepth={}\nwidth={}\nc={}\n".format(
            N_elem, depth, width, Ck_cont))
        writer.writelines("max_epoch={}\nnum_tested_per_element={}\n".format(
            max_epoch, N_pred))
        writer.writelines("%.2f\t" % point for point in domain)

    output_data(solution_file, pred_in_data, Soln_flat, Exact_soln_flat)
    util.plot_result(epoch, loss_history, pred_in_data, Exact_soln_flat,
                     Soln_flat, dump_dir, error_u)
Esempio n. 7
0
def analize(XX, yy):
    y_pred = regr.predict(XX)
    plot_result(yy, y_pred)
    full_report(yy, y_pred)
    print('Training time: %.4f' % training_time)

    x_testing = np.linspace(lower_bound, upper_bound, num_testing).reshape(
        (-1, 1))
    prediction, truth = model.test(x_testing)

    error_u = np.sqrt(np.square(prediction - truth).mean())
    print('Error u: %e' % error_u)

    with open(dump_dir + "info.txt", 'w') as log:
        writing_items = [
            ">>> Function <<<",
            "Interval = [%.2f, %.2f]" % (lower_bound, upper_bound),
            "Q = %d" % quad_order,
            "(a, w) = (%.2f, %.2f)" % (a, w), "\n>>> Network <<<",
            "Layers = %s" % str(layers),
            "Max_epochs = %d" % max_epoch,
            "Num_testing = %d" % num_testing,
            "Init_lr = %.2e" % init_lr,
            "Least_squared = %s" % least_squared,
            "Lambda = %.2f" % lam,
            "Loss_threshold = %.1e" % min_loss,
            "Adjusting_period = %d" % adjusting_epoch, "\n>>> Result <<<",
            "Training time: %.4fs" % training_time,
            "L2_error: %e" % error_u
        ]
        log.writelines("\n".join(writing_items))

    util.plot_result(epoch_list, loss_history, x_testing, truth, prediction,
                     dump_dir, error_u)
Esempio n. 9
0
        G_optimizer.step()

        train_hist['G_losses'].append(G_train_loss.data)
        G_losses.append(G_train_loss.data)

        num_iter += 1

    epoch_end_time = time.time()
    per_epoch_ptime = epoch_end_time - epoch_start_time

    print('[%d/%d] - ptime: %.2f, loss_d: %.3f, loss_g: %.3f' %
          ((epoch + 1), opt.train_epoch, per_epoch_ptime,
           torch.mean(torch.FloatTensor(D_losses)),
           torch.mean(torch.FloatTensor(G_losses))))
    fixed_p = root + 'Fixed_results/' + model + str(epoch + 1) + '.png'
    train_hist['per_epoch_ptimes'].append(per_epoch_ptime)

end_time = time.time()
total_ptime = end_time - start_time
train_hist['total_ptime'].append(total_ptime)
print('total training time: ', total_ptime)

print("Avg one epoch ptime: %.2f, total %d epochs ptime: %.2f" %
      (torch.mean(torch.FloatTensor(
          train_hist['per_epoch_ptimes'])), opt.train_epoch, total_ptime))
print("Training finish!... save training results")
torch.save(G.state_dict(), root + model + 'generator_param.pkl')
torch.save(D.state_dict(), root + model + 'discriminator_param.pkl')

util.plot_result(train_hist)