gpt4 book ai didi

python - Tensorflow:将标量张量值作为 int 传递给 set_shape()

转载 作者:行者123 更新时间:2023-12-05 07:44:15 25 4
gpt4 key购买 nike

我正在尝试将 3D 图像及其标签从 numpy 数组加载到 TensorFlow 记录,然后在训练我的网络时从队列中读取它们。转换的代码是基于TensorFlow的Inception model的转换.

每个图像都有不同的高度、宽度和深度值,因此在 reshape 数组时我需要知道这些值。但是,当我尝试使用 set_shape 时出现错误,因为在某处使用了 int(),并且它不接受 Tensor 值。

reader = tf.TFRecordReader()
_, value = reader.read(filename_queue)


# Features in Example proto
feature_map = {
'height': tf.VarLenFeature(dtype=tf.int64),
'width': tf.VarLenFeature(dtype=tf.int64),
'depth': tf.VarLenFeature(dtype=tf.int64),
'label': tf.VarLenFeature(dtype=tf.int64),
'image_raw': tf.VarLenFeature(dtype=tf.string)
}

features = tf.parse_single_example(value, feature_map)
result.label = tf.cast(features['label'].values[0], dtype=tf.int32)
result.height = tf.cast(features['height'].values[0], dtype=tf.int32)
result.width = tf.cast(features['width'].values[0], dtype=tf.int32)
result.depth = tf.cast(features['depth'].values[0], dtype=tf.int32)

image = tf.decode_raw(features['image_raw'].values[0], tf.int16)

image = tf.reshape(image, [result.depth, result.height, result.width])
image = tf.cast(tf.transpose(image, [1, 2, 0]), tf.float32)
result.image = tf.expand_dims(image, 3)

result.image.set_shape([result.height, result.width, result.depth, 1])
result.label = tf.expand_dims(result.label, 0)
result.label.set_shape([1])

错误轨迹:

Traceback (most recent call last):
File "dsb17_multi_gpu_train.py", line 227, in <module>
tf.app.run()
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "dsb17_multi_gpu_train.py", line 223, in main
train()
File "dsb17_multi_gpu_train.py", line 129, in train
loss = tower_loss(scope)
File "dsb17_multi_gpu_train.py", line 34, in tower_loss
images, labels = dsb17.inputs(False)
File "/home/ubuntu/dsb17/model/dsb17.py", line 104, in inputs
batch_size=FLAGS.batch_size)
File "/home/ubuntu/dsb17/model/dsb17_input.py", line 161, in inputs
read_input = read_data(filename_queue)
File "/home/ubuntu/dsb17/model/dsb17_input.py", line 62, in read_data
result.image.set_shape([result.height, result.width, result.depth, 1])
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 425, in set_shape
self._shape = self._shape.merge_with(shape)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 573, in merge_with
other = as_shape(other)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 821, in as_shape
return TensorShape(shape)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 457, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 457, in <listcomp>
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 378, in as_dimension
return Dimension(value)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 33, in __init__
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

我最初认为这是因为 Tensor 在 session 中被评估之前没有值,但是 loss 是在 sess.run() 中被评估的,这就是需要调用 tower_loss() 的原因。我的训练代码在结构上与 cifar10_multi_gpu_train.py 相同,整体文件结构也非常相似。

接下来的问题是:它实际上是在 session 中被评估,还是图形尚未构建?我是否需要以某种方式从零维张量中提取值?更一般地说,我对张量和 session 的误解是什么导致我的代码无法按预期工作?

最佳答案

根据 TensorFlow 的 tf.cast docs , tf.cast 返回一个张量。

您的错误表明当使用 set_shape() 时,您不能将 Tensor 作为参数,而是一个 int。

您可以尝试强制 Tensorflow 评估转换。这个简单的例子对我有用:

a = tf.constant(2.0)
b = tf.constant([1.0,2.0])
b.set_shape(a.eval())

如果不调用 eval(),我会得到与您相同的错误。

关于python - Tensorflow:将标量张量值作为 int 传递给 set_shape(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com