示例#1
0
saver = tf.train.Saver()
# Train the network
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    #saver.restore(sess, 'models/%s/%s.ckpt' % (eid, 533))
    record_loss(sess, train_dataset.X)
    k = 1
    new_iters = int(n_iters / k)
    for it in range(new_iters):
        # Shuffle data once for each epoch
        if it % int(new_iters / n_epochs) == 0:
            train_dataset.shuffle()
        # Train
        for i in range(k):
            next_x, _ = train_dataset.next_batch()
            draw_prior_samples = draw_multivariate_gaussian(
                latent_dim, batch_size)
            sess.run(train_discriminator,
                     feed_dict={
                         x: next_x,
                         prior_samples: draw_prior_samples
                     })
        sess.run(train_generator, feed_dict={x: next_x})
        sess.run(train_reconstruct, feed_dict={x: next_x})
        # Show loss
        if it % show_steps == 0:
            print('Iterations %5d: ' % (it + 1), end='')
            record_loss(sess, train_dataset.X)
    extract_encoded_data(sess)
    extract_image(sess)
示例#2
0
if __name__ == '__main__':
    num_cur_best = 0
    saver = tf.train.Saver()
    # Launch the graph
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        # Restore model
        #saver.restore(sess, 'models/try_2.ckpt')
        #print("Model restored.")
        
        #print('Before train:\t', end="")
        record_error(sess)
        for it in range(n_iters):
            # Train next batch
            next_x, next_y = train_dataset.next_batch()
            sess.run(train_step, feed_dict={x: next_x, y: next_y})
            # Record loss
            if it % show_steps == 0:
                print('Iterations %4d:\t' %(it+1) , end="")
                loss_this_iter = record_error(sess)
                # Save the model
                if log['best_loss'] - loss_this_iter > 0.0000000001:
                    num_cur_best += 1
                    print('Find and save %d current best loss model. %.10f' %(num_cur_best, loss_this_iter))
                    log['best_loss'] = loss_this_iter
                    if not os.path.exists(save_directory):
                        os.makedirs(save_directory)
                    save_path = saver.save(sess, '%s/model.ckpt' % (save_directory))

            # Shuffle data once for each epoch
def calculate_error_with_real_price(sess, predict_op, X, Y, real_last_one,
                                    real_last_two, statistcs):
    # Predict prices
    dataset = Dataset(X, Y, batch_size)
    n_batch = int(X.shape[0] / batch_size)
    for i in range(n_batch):
        next_x, next_y = dataset.next_batch()
        if i == 0:
            predict_log_return = sess.run(predict,
                                          feed_dict={
                                              x: next_x,
                                              y: next_y
                                          }).tolist()
        else:
            predict_log_return = np.append(predict_log_return,
                                           sess.run(predict,
                                                    feed_dict={
                                                        x: next_x,
                                                        y: next_y
                                                    }).tolist(),
                                           axis=0)

    predict_last_one = np.exp(predict_log_return + np.log(real_last_two))
    # Export prices to statistcs_file
    statistcs['real_price'] = real_last_one.tolist()
    statistcs['predict_price'] = predict_last_one.tolist()

    # Show last 5 statistics
    print('last price is: ', real_last_two[-5:])
    print('predict price is: ', predict_last_one[-5:])
    print('real price is: ', real_last_one[-5:])

    # Calculate MSE
    MSE = np.mean((real_last_one - predict_last_one)**2)
    statistcs['MSE'] = MSE
    print('MSE = %.10f' % MSE)

    # Caluculate Variance
    mean_real = np.mean(real_last_one)
    var_real = np.var(real_last_one)
    print('Real mean=%.4f, var=%.5f' % (mean_real, var_real))
    mean_predict = np.mean(predict_last_one)
    var_predict = np.var(predict_last_one)
    print('Predict mean=%.4f, var=%.5f' % (mean_predict, var_predict))

    length = real_last_one.shape[0]
    real_last_one = np.reshape(real_last_one, length)
    real_last_two = np.reshape(real_last_two, length)
    predict_last_one = np.reshape(predict_last_one, length)

    # Calculate sign accuracy
    real_diff = real_last_one - real_last_two
    predict_diff = predict_last_one - real_last_two
    real_diff_sign = np.sign(real_diff)
    predict_diff_sign = np.sign(predict_diff)
    print('real_diff: ', real_diff[-20:])
    print('predict_diff: ', predict_diff[-20:])
    num_correct_sign = np.count_nonzero(
        np.equal(real_diff_sign, predict_diff_sign))
    sign_accuracy = num_correct_sign / real_diff_sign.shape[0]
    statistcs['sign_accuracy'] = sign_accuracy
    print('Sign Accuracy = %.10f' % sign_accuracy)