【问题标题】:"Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key" when running sess.run()“如果启用了张量相等,则张量是不可散列的。相反,在运行 sess.run() 时使用 tensor.experimental_ref() 作为键”
【发布时间】:2020-04-27 09:38:18
【问题描述】:

TypeError:如果启用了张量相等,则张量是不可散列的。相反,使用 tensor.experimental_ref() 作为键。

... 当我运行下面的代码时正在显示。

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import load_model
print(tf.__version__)

seed_num=1
data_path = 'Caltech-256/'
batch_size = 80  # the number of images to load per iteration
target_size=(64,64) # pixel size of each image
num_pixels_and_channels = (64,64,3) # pixels and channels 
input_shape = (1,64,64,3) 
layer = 1
feature = 0

val_data_gen_aug_rotate = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, 
                                                               validation_split=0.1)


val_img = val_data_gen_aug_rotate.flow_from_directory(data_path, 
                                           subset='validation',
                                           color_mode='rgb',
                                           target_size=target_size,
                                           batch_size=batch_size,
                                           class_mode='categorical',
                                           seed=seed_num)

sample_imgs_val, sample_labels_val = next(val_img)

model = load_model("Models/ex_13_epoch_4_3563_336.h5")


sess = tf.compat.v1.Session()
input_layer = model.layers[0].input
output_layer = model.layers[layer].output

outputs = sess.run(output_layer, feed_dict = {input_layer : sample_imgs_val})

问题在于代码outputs = sess.run(output_layer, feed_dict = {input_layer : sample_imgs_val})。是什么导致了错误,如何解决?

我正在通过 Jupyter Notebook 在 CPU 上使用 tensorflow 版本 2.1.0。

【问题讨论】:

  • 您正在尝试在 TensorFlow 2.x 中使用来自 TensorFlow 1.x 的会话,该会话用于图形模式,默认情况下在 Eager 模式下工作。该错误专门抱怨尝试将张量用作 dict 键,但通常您不能在急切模式下使用会话。看看changes introduced by TensorFlow 2.x 和tips to migrate your code from TensorFlow 1.x。
  • 谢谢。这就解释了出了什么问题。我还不清楚我需要做些什么来解决它,所以我们将不胜感激。

标签: python tensorflow keras


【解决方案1】:

错误是由于版本引起的。

您正在尝试使用 TensorFlow 1.x,它在图形模式下工作,而 TensorFlow 2.x 在急切模式下工作。

【讨论】:

  • 谢谢。这就解释了出了什么问题。我还不清楚我需要做些什么来修复它,所以我们将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2023-03-12
  • 2023-03-15
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多