为什么每次使用张量流模型时都会得到不同的结果?

时间:2017-09-15 21:37:37

标签: python tensorflow recurrent-neural-network

我已经保存了一个基于递归神经网络的训练模型。当我运行以下函数' 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

1 个答案:

答案 0 :(得分:1)

由于LSTM模型的随机性,您将看到不同的结果,并且因为很难为LSTM模型修复随机种子以获得100%可重复的结果。