我正在尝试使用STL-10数据集训练网络。
我从STL-10二进制文件中提取了数据并将它们转换为numpy数组。然后我使用tf.convert_to_tensor
函数
现在我有一个形状的张量(5000,96,96,3)
我想从这个张量中得到一批大小为32的数据包含5000个图像的数据,并且批次将在每次迭代中随机洗牌。
使用tf.train.batch
会出错
`TypeError: `Tensor` objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.`
如何获得一批大小为32的图像数据,这些数据将在每次迭代中随机洗牌?
答案 0 :(得分:1)
来自tf.train.batch
的文档:
论证张量可以是张量或张量词典。该 函数返回的值与张量的类型相同。
您需要将数据转换为5000张张量的列表,每张张量均为(96,96,3)。
答案 1 :(得分:1)
您可以直接在tensorflow函数中使用numpy数组,因为tensorflow知道如何转换它们。
# Form shuffled batch of data
def get_batch(inputs, targets, size):
'''
Return a total of `size` random inputs and targets(or labels).
'''
targets_shape = targets.shape
num_data = targets_shape[0]
# this is a list of the right number of indices in the indices range
shuffled_indices = np.random.randint(0,num_data,size)
#this takes the selected random elements
inputs_shuffled = inputs[shuffled_indices,:,:,:]
#depending on the target shape it could be targets[idx,:,:,...]
#this takes the corresponding targets
targets_shuffle = targets[shuffled_indices,:]
#return the shuffled data and targets
return inputs_shuffled, targets_shuffle
然后你可以在训练中使用它:
#This calls the function we created
inputs_batch, targets_batch = get_batch(inputs_all,targets_all,batch_size)
#This tells to tensorflow which input goes to which placeholder
feed_dict={inputs_placeholder: inputs_batch,
targets_placeholder: targets_batch}
#This runs one step of the training
sess.run(train_step,feed_dict = feed_dict)
希望我能帮忙......
答案 2 :(得分:1)
请检查tf.train.batch
的用法:
label_batch = tf.train.batch([label], capacity=20, batch_size=10, num_threads=2)
label
中括号的消失将导致这种错误。