我正在尝试使用STL-10数据集训练网络。
我已经从STL-10二进制文件中提取了数据,并将它们转换为Numpy数组。然后,我使用tf.convert_to_tensor
函数将它们转换为张量
现在我有一个形状的张量(5000,96,96,3(
我想从包含5000张图像的数据的张量中获得一批尺寸32,并且这些批次将在每次迭代中随机洗牌。
使用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的图像数据,这些数据将在每次迭代中随机洗牌?
来自 tf.train.batch
的文档:
参数张量可以是张量的列表或词典。这 该函数返回的值将与张量相同。
您需要将数据转换为5000个张量的列表,每个张量(96,96,3(。
您可以直接在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)
希望我能帮助...
请检查 tf.train.batch
的用法:
label_batch = tf.train.batch([label], capacity=20, batch_size=10, num_threads=2)
label
的支架的消失将导致这样的错误。