14

我使用tf.data.datsetAPI 并使用残差网络。当我为 TensorBoard 运行代码以可视化我的嵌入时,我遇到了这个错误,但是当我使用两层网络时,我没有这个问题。

def load_and_preprocess_from_path_label(path, label):
    return load_and_preprocess_image(path), label

ds = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))
with tf.Session() as sess:
    # TODO (@omoindrot): remove the hard-coded 10000
    # Obtain the test labels
    image_label_ds = ds.map(load_and_preprocess_from_path_label)
    ds = image_label_ds.shuffle(image_count)

RuntimeError                              Traceback (most recent call last)
<ipython-input-41-ead5d6a54baa> in <module>()
     92         # TODO (@omoindrot): remove the hard-coded 10000
     93         # Obtain the test labels
---> 94         image_label_ds = ds.map(load_and_preprocess_from_path_label)
     95         ds = image_label_ds.shuffle(image_count)
     96 
RuntimeError: Attempting to capture an EagerTensor without building a function.
4

2 回答 2

3

该错误可能是由于 Tensorflow 版本

运行以下代码对我有用:

import tensorflow.compat.v1 as tf

正确功能:

tf.disable_v2_behavior()
于 2020-06-12T22:16:02.843 回答
1

运行以下代码对我有用:

from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
from keras.callbacks import EarlyStopping
from keras import backend as K
import tensorflow as tf


tf.compat.v1.enable_eager_execution()

如果您在创建模型后也使用以下代码强制 LSTM 清除模型参数和 Graph,那就太好了。

K.clear_session()
tf.compat.v1.reset_default_graph()
#tf.compat.v1.disable_eager_execution()
于 2020-02-07T15:17:52.323 回答