def prediction(time_step=20): X = tf.placeholder(tf.float32, shape=[None, time_step, input_size]) mean, std, test_x, test_y = get_test_data(time_step) with tf.variable_scope("sec_lstm", reuse=tf.AUTO_REUSE): pred, _ = lstm(X) saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: # 参数恢复 module_file = tf.train.latest_checkpoint('model_save2') saver.restore(sess, module_file) test_predict = [] for step in range(len(test_x) - 1): prob = sess.run(pred, feed_dict={X: [test_x[step]], keep_prob: 1}) predict = prob.reshape((-1)) test_predict.extend(predict) test_y = np.array(test_y) * std[7] + mean[7] test_predict = np.array(test_predict) * std[7] + mean[7] acc = np.average( np.abs(test_predict - test_y[:len(test_predict)]) / test_y[:len(test_predict)]) # 偏差程度 print("The accuracy of this predict:", acc) # 以折线图表示结果 plt.figure() plt.plot( list(range(len(test_predict))), test_predict, color='b', ) plt.plot(list(range(len(test_y))), test_y, color='r') plt.show()
def plot_results(xlog, ulog): t = [0.25 * x for x in range(xlog.shape[1])] plt.figure() plt.subplot(311) plt.plot(t, xlog[3, :], label="altitude", color="b") plt.ylabel('altitude') plt.subplot(312) plt.plot(t, xlog[1, :], label="pitch angle", color="r") plt.ylabel('pitch angle') plt.subplot(313) plt.plot(t, ulog[0, :], label="elevator angle", color="g") plt.ylabel('elevator angle') plt.show()
training_epochs = 1000 # 损失函数: subtract张量的减法 loss = tf.reduce_sum( tf.maximum(0., tf.subtract(tf.abs(tf.subtract(curr_y, y)), epsilon))) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train = optimizer.minimize(loss) sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(training_epochs): sess.run(train, {x: t_x, y: t_y}) if i % 20 == 0: print(i, sess.run([a, b, loss], {x: t_x, y: t_y})) a_val = sess.run(a) b_val = sess.run(b) print("this model is y=", a_val, "* x +", b_val) # sess.close() y_learned = t_x * a_val + b_val plt.plot(t_x, t_y, "k.") plt.plot(t_x, y_learned, "g-") linewidth = sess.run(epsilon) plt.plot(t_x, y_learned + linewidth, "r--") plt.plot(t_x, y_learned - linewidth, "r--") plt.savefig("svm.png") plt.show()