【问题标题】:batch structure in tensorflow张量流中的批处理结构
【发布时间】:2020-07-21 20:41:16
【问题描述】:

我正在学习 tensorflow 和 MNIST 数据集的神经网络教程。我遇到了以下代码:

for _ in range(1000):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x: batch[0], y: batch[1]})

我在可视化批处理结构时遇到了一些问题。特别是批次的索引。 batch[0] 是否意味着批次中的所有 50 个图像,batch[1] 是否意味着图像的所有 50 个标签?如果有人可以直观地显示批次的结构,那就太好了。我搜索但找不到一个很好的教程。

【问题讨论】:

  • 是的,你的假设是正确的。您可能想要链接到您从中获得该 sn-p 的教程。断章取义,有点难以解释。

标签: tensorflow deep-learning neural-network mnist


【解决方案1】:

这是我批量显示图像的基本代码

def display_one_flower(image, title, subplot, red=False, titlesize=16):
    plt.subplot(*subplot)
    plt.axis('off')
    plt.imshow(image)
    if len(title) > 0:
        plt.title(title, fontsize=int(titlesize) if not red else int(titlesize/1.2), 
color='red' if red else 'black', fontdict={'verticalalignment':'center'}, 
pad=int(titlesize/1.5))
    return (subplot[0], subplot[1], subplot[2]+1)

def display_batch_of_images(images,labels, predictions=None):
    """This will work with:
    display_batch_of_images(images)
    display_batch_of_images(images, predictions)
    display_batch_of_images((images, labels))
    display_batch_of_images((images, labels), predictions)
   """

    # auto-squaring: this will drop data that does not fit into square or square- 
    #rectangle
    rows = int(math.sqrt(len(images)))
    cols = len(images)//rows

    # size and spacing
    FIGSIZE = 13.0
    SPACING = 0.1
    subplot=(rows,cols,1)
    if rows < cols:
        plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
    else:
        plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))

    # display
    tempo=""
    for i, (image, label) in enumerate(zip(images[:rows*cols], labels[:rows*cols])):
        title = '' if label is None else tempo 
        correct = True
        if predictions is not None:
        title, correct = title_from_label_and_target(predictions[i], label)
        dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3 # magic formula tested to work from 1x1 to 10x10 images
        subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)

    #layout
    plt.tight_layout()
    if label is None and predictions is None:
        plt.subplots_adjust(wspace=0, hspace=0)
    else:
        plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
    plt.show()

现在我们批量显示图片

图像包含所有 50 个图像,标签包含所有 50 个标签

  display_batch_of_images(images,labels)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2019-01-14
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    相关资源
    最近更新 更多