运行sess.run()时,“如果启用了Tensor相等,则Tensor无法散列。相反,请使用tensor.experimental_ref()作为键”。

时间:2020-04-27 09:38:18

标签: python tensorflow keras

TypeError:如果启用了Tensor相等,则Tensor无法散列。相反,请使用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版。

1 个答案:

答案 0 :(得分:1)

该错误是由于版本引起的。

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