【发布时间】:2017-09-15 21:37:36
【问题描述】:
我保存了一个基于循环神经网络的训练模型。当我运行以下函数“lstm_vector_predict()”时,即使加载相同的模型,它每次都会返回不同的值。张量流在预测值时是否使用一些随机数生成?
import get_list_of_values_to_input
import tensorflow as tf
import tensorflow.contrib.learn as tflearn
import tensorflow.contrib.layers as tflayers
from tensorflow.contrib.learn.python.learn import learn_runner
import tensorflow.contrib.metrics as metrics
import tensorflow.contrib.rnn as rnn
import numpy as np
from backend.common.numpy_array_to_numpy_array_of_arrays import get_numpy_arrays_from_numpy_matrix
def lstm_vector_predict(model_name='sample_model_vector.meta', number_of_tickers=2, batch_size=20,number_of_points=100, start_time=1489462200):
tf.reset_default_graph()
inputs = number_of_tickers
hidden = 100
output = number_of_tickers
current_time = start_time
X = tf.placeholder(tf.float32, [None, batch_size, inputs])
# This is low level tensor flow stuff used for preparing output of data generation
basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden, activation=tf.nn.relu)
rnn_output, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
stacked_rnn_output = tf.reshape(rnn_output, [-1, hidden])
stacked_outputs = tf.layers.dense(stacked_rnn_output, output)
outputs = tf.reshape(stacked_outputs, [-1, batch_size, output])
# We get the saver ready
saver = tf.train.import_meta_graph(model_name)
init = tf.global_variables_initializer()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
return_values = []
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, tf.train.latest_checkpoint('./'))
print("Model restored.")
# Check the values of the variables
sess.run(init)
for i in range(number_of_points):
last_values = get_list_of_values_to_input()
print("Generating point", i)
#x_generators = last_values[-batch_size:]
x_generators = last_values[-batch_size:].reshape(-1, batch_size, number_of_tickers)
y_forecast = sess.run(outputs, feed_dict={X: x_generators})
return_values.append(y_forecast[-1][-1])
current_time += 300
return return_values
【问题讨论】:
-
假设您的检查点文件没有改变并且
get_list_of_values_to_input没有改变,另一种可能性是您正在加载的模型 (sample_model_vector.meta) 包含一些随机操作。 IE, tf.Variable() 默认使用随机初始化器 -
加载变量后不要运行初始化操作。这将覆盖它们恢复的值。
-
感谢您的提问。我已经为此苦苦挣扎了好几天,同时试图将遗留的 tf 代码移植到 pytorch。
标签: python tensorflow recurrent-neural-network