错误:无法使用给定的会话来评估张量:张量的图与会话的图不同

时间:2019-10-22 15:30:37

标签: tensorflow

我正在使用之前训练有素的模型来生成预测,如其中一个教程中所述。但是当我尝试运行该方法时,出现以下错误:

ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.

整个反馈如下:

WARNING:tensorflow:From C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\ops\init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
2019-10-22 10:42:43.943842: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:/Users/vicke/PycharmProjects/RNN-LSTM/RNN.py", line 46, in <module>
    history = model.fit(train_set, epochs=10)
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 780, in fit
    steps_name='steps_per_epoch')
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 141, in model_iteration
    inputs, steps_per_epoch, epochs=epochs, steps_name=steps_name)
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 1393, in infer_steps_for_dataset
    size = K.get_value(cardinality.cardinality(dataset))
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\keras\backend.py", line 2989, in get_value
    return x.eval(session=get_session((x,)))
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 731, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:\Users\vicke\PycharmProjects\RNN-LSTM\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 5576, in _eval_using_default_session
    raise ValueError("Cannot use the given session to evaluate tensor: "
ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.

我的代码在这里

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data=pd.read_csv("c://pp/11.csv")
series=np.array([data.Return]).T

time=np.arange(276, dtype="float32")
split_time = 200
time_train = time[:split_time]
x_train = series[:split_time]
time_valid = time[split_time:]
x_valid = series[split_time:]

window_size = 10
batch_size = 10
shuffle_buffer_size = 20

def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
  dataset = tf.data.Dataset.from_tensor_slices(series)
  dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True)
  dataset = dataset.flat_map(lambda window: window.batch(window_size + 1))
  dataset = dataset.shuffle(shuffle_buffer).map(lambda window: (window[:-1], window[-1]))
  dataset = dataset.batch(batch_size).prefetch(1)
  return dataset

train_set = windowed_dataset(x_train, window_size, batch_size=128, shuffle_buffer=shuffle_buffer_size)

tf.keras.backend.clear_session()
tf.compat.v1.random.set_random_seed(51)
np.random.seed(51)

model = tf.keras.models.Sequential([
    tf.keras.layers.SimpleRNN(16,return_sequences=True,
                              input_shape=[None,1]),
    tf.keras.layers.SimpleRNN(16,return_sequences=True),
    tf.keras.layers.Dense(1),
])

optimizer = tf.keras.optimizers.SGD(lr=1e-8, momentum=0.9)
model.compile(loss=tf.keras.losses.Huber(),
              optimizer=optimizer,
              metrics=["mae"])

history = model.fit(train_set, epochs=10)

出什么问题了?

0 个答案:

没有答案