是一个;“正常”;python对象自动成为张量?e、 g.“;a「;或1
我很难理解为什么这个函数会起作用:是一个;“正常”;python对象自动成为张量?e、 g.“;a「;或1,python,python-3.x,tensorflow,tensorflow2.0,tensorflow-datasets,Python,Python 3.x,Tensorflow,Tensorflow2.0,Tensorflow Datasets,我很难理解为什么这个函数会起作用: def tf_load_data(training, batch_size, img_path_list, label_list): # Arguments: # path_to_image: a Tensor of type string # returns 3-D float Tensor of shape [new_height, new_width, channels] def get_img(path_to_img)
def tf_load_data(training, batch_size, img_path_list, label_list):
# Arguments:
# path_to_image: a Tensor of type string
# returns 3-D float Tensor of shape [new_height, new_width, channels]
def get_img(path_to_img):
# load the raw data, encoded as string.
raw_img = tf.io.read_file(path_to_img)
# Creates a 3D uint8 tensor.
img = ts.io.decode_png(raw_img, channels=3) # pictures are not saved as Grayscale
# Changes the values in the tensor to be floats in [0,1). -- Normalization
img = ts.image.convert_image_dtype(img, tf.float32)
# Resize all pictures to the same format.
return ts.image.resize(img, [constant.IMG_WIDTH, constant.IMG_HEIGHT])
# Arguments:
# label_string: as byte:32 which represents a string
# path_to_image: as byte:32 which represents a string
# returns a pair of two Tensors
def get_pair(path_to_img, label_string):
return get_img(path_to_img), lable_string
# Arguments: -- function is use together with tf.data.Dataset.map or tf.data.Dataset.apply
# img: is a Tensor of type String
# label: is a Tensor of type String
# return: the type is the same as input
def pre_process(img, label):
# Do all the pre-processing:
return img, label
dataset = tf.data.Dataset.from_tensor_slices((img_path_list, label_list))
dataset_tensor = dataset.map(map_func=get_pair, num_parallel_calls=None)
img_path_list和label_list是字符串类型的列表
我不明白的是很明显
dataset=tf.data.dataset.from_tensor_切片((img_路径列表,标签列表))
是包含元组()的张量。因此,当我运行.map(map\u func=get\u pair,num\u parallel\u calls=None)
时,在get\u对(path\u to\u img,label\u string):
函数中,两个字符串作为元组传递。然后将这些字符串中的一个传递到get\u img(path\u to\u img)
函数中,最后传递到tf.io.read\u文件(path\u to\u img)
。
问题是,io.read\u file()
需要输入:“字符串类型的张量”(参见文档:)。
但是绳子!=到字符串类型的张量:
isinstance(tf.constant["hello"], tf.Tensor) == True
isinstance(“hello”,tf.Tensor)==False
以及:isinstance([“hello”,tf.Tensor)==False
谢谢你的帮助 您显式地使用了
tf
(tensorflow)调用,为什么会对得到张量感到奇怪?在您的isinstance
测试中,您没有使用任何tf
调用,为什么您想知道这些不是tf
张量?不管怎样,tf
是一个外部包,Python本身完全不知道它是什么。没有内置类型是张量。然而,tf
可能会选择将任意对象提升为张量,如果它知道如何提升的话。好的,我明白了,也许它只是为我转换了它。