tf.while_oop InvalidArgumentError问题:使用输入dim 0的索引超出范围;输入只有0个d



我正在尝试使用Tensorflow-来并行化这段代码

import numpy as np
import tensorflow as tf
import time
#Importing a generic dataset from Keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
#I would like to compare a reference image to a bunch of images
#This is my reference image
x_reference = np.expand_dims(x_train[0],axis = 2)
start = time.time()
for index, image in enumerate(x_train):
# The tf.image.ssim is a similarity metric
tf.image.ssim(np.expand_dims(x_train[index],axis = 2), x_reference, 255)
print("Total Time=", time.time() - start)

我决定使用tf.while_oop进行并行化-

import numpy as np
import tensorflow as tf
#Importing a generic dataset from Keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
#This is my reference image
x_reference = np.expand_dims(x_train[0],axis = 2)
t1 = tf.constant(x_train)
t2 = tf.constant(x_reference)
iters = tf.constant(60000)
def cond(t1, t2, i, iters):
return tf.less(i, iters)
def body(t1, t2, i, iters):
return [tf.image.ssim(np.expand_dims(t1[i],axis = 2), t2, 255), t2, tf.add(i,1), iters]
res = tf.while_loop(cond, body, [t1, t2, 0 , iters], parallel_iterations=60000)

然而,我遇到了一些错误。我是tensorflow的新手,因此我知道这个代码将在一系列点上被破坏。我们非常感谢所有形式的指导(甚至通过评论(!

我已经修复了您的代码。

共有2个问题。最主要的是你从身体机能上回来了。你需要明白,在";循环";,将上一次迭代的输出注入到body函数中。在您的代码中,您将sim值作为元组中的第一列返回。因此,在第二个";"迭代";,你已经没有t1了。您只有一个值。这就是你犯那个错误的原因。您正试图将";sim";值而不是t1。

另一个问题是在调用tf.image.ssim时。它需要一批图像,它基本上需要[1,64,64,1],但你通过了[64,64,1]

def body(t1, t2, i, iters, sim):
a = np.expand_dims(t1[i],axis = 2)
a = tf.expand_dims(a, axis=0)   #make a batch
b = tf.expand_dims(t2, axis=0)  #make a batch
sim = tf.image.ssim(a, b, 255)
return [t1, t2, tf.add(i,1), iters, sim]

这是整个代码:

import numpy as np
import tensorflow as tf

#Importing a generic dataset from Keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
#This is my reference image
x_reference = np.expand_dims(x_train[0],axis = 2)
t1 = tf.constant(x_train)
t2 = tf.constant(x_reference)
iters = tf.constant(60000)
def cond(t1, t2, i, iters, sim):
return tf.less(i, iters)
def body(t1, t2, i, iters, sim):
a = np.expand_dims(t1[i],axis = 2)
a = tf.expand_dims(a, axis=0)   #make is a batch
b = tf.expand_dims(t2, axis=0)  #make is a batch
sim = tf.image.ssim(a, b, 255)
return [t1, t2, tf.add(i,1), iters, sim]
res = tf.while_loop(cond, body, [t1, t2, 0 , iters, 0], parallel_iterations=60000)

相关内容

  • 没有找到相关文章

最新更新