我正在尝试使用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的图像数据,这些数据将在每次迭代中随机混洗?
发布于 2018-02-22 16:30:08
来自tf.train.batch的文档
参数张量可以是张量的列表或字典。函数返回的值将与张量具有相同的类型。
您需要将数据转换为5000个张量的列表,每个张量的形状为(96,96,3)。
发布于 2018-02-22 18:00:38
您可以在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)希望我能帮上忙。
发布于 2018-07-26 10:47:57
请检查tf.train.batch的使用情况
label_batch = tf.train.batch([label], capacity=20, batch_size=10, num_threads=2)label的括号消失会导致这样的错误。
https://stackoverflow.com/questions/48922447
复制相似问题