首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用随机洗牌的批次作为来自图像数据张量的CNN的输入

使用随机洗牌的批次作为来自图像数据张量的CNN的输入
EN

Stack Overflow用户
提问于 2018-02-22 16:12:02
回答 3查看 640关注 0票数 1

我正在尝试使用STL-10数据集训练网络。

我已经从STL-10二进制文件中提取了数据,并将它们转换为numpy数组。然后,我使用tf.convert_to_tensor函数将它们转换为张量

现在我有一个形状张量(5000,96,96,3)

我想从这个张量中获得一个大小为32的批量,其中包含5000张图像的数据,这些批量将在每次迭代中随机混洗。

使用tf.train.batch会产生错误

代码语言:javascript
运行
复制
`TypeError: `Tensor` objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.`

如何获得一批大小为32的图像数据,这些数据将在每次迭代中随机混洗?

EN

回答 3

Stack Overflow用户

发布于 2018-02-22 16:30:08

来自tf.train.batch的文档

参数张量可以是张量的列表或字典。函数返回的值将与张量具有相同的类型。

您需要将数据转换为5000个张量的列表,每个张量的形状为(96,96,3)。

票数 1
EN

Stack Overflow用户

发布于 2018-02-22 18:00:38

您可以在tensorflow函数中直接使用numpy数组,因为tensorflow知道如何转换它们。

代码语言:javascript
运行
复制
# 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

然后你可以在训练中使用它:

代码语言:javascript
运行
复制
#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)

希望我能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2018-07-26 10:47:57

请检查tf.train.batch的使用情况

代码语言:javascript
运行
复制
label_batch = tf.train.batch([label], capacity=20, batch_size=10, num_threads=2)

label的括号消失会导致这样的错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48922447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档