Tensorflow:构建批量大小在1维上变化的图? [英] Tensorflow: building graph with batch sizes varying in dimension 1?

查看:148
本文介绍了Tensorflow:构建批量大小在1维上变化的图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Tensorflow中构建CNN模型,其中批次中的所有输入均具有相同的形状,但是批次之间的输入的维数为1(即,最小批量大小相同,但最小批量形状则不同)。

I'm trying to build a CNN model in Tensorflow where all the inputs within a batch are equal shape, but between batches the inputs vary in dimension 1 (i.e. minibatch sizes are the same but minibatch shapes are not).

为了更清楚一点,我有各种值N的数据(Nx23x1),我先按升序对其进行排序。在每批(50个样本)中,我对每个样本进行零填充,以使每个N_i等于其小批量内的最大N。现在我为批量输入定义了Tensorflow占位符:

To make this clearer, I have data (Nx23x1) of various values N that I sort in ascending order first. In each batch (50 samples) I zero-pad every sample so that each N_i equals the max N within its minibatch. Now I have defined Tensorflow placeholder for the batch input:

input = tf.placeholder(tf.float32, shape=(batch_size, None, IMAGE_WIDTH, NUM_CHANNELS))

我在输入占位符中使用无,因为在批次之间此值变化,即使在批次中没有变化。在运行我的训练代码时,我使用feed_dict传递了教程中定义的输入值(numpy矩阵)。

I use 'None' in the input placeholder because between batches this value varies, even though within a batch it doesn't. In running my training code, I use a feed_dict to pass in values for input (numpy matrix) as defined in the tutorials.

我的CNN代码接受了此输入;但是,这就是我遇到的问题。尝试在完全连接的层之前将输入变平时,出现ValueError。它尝试使数组变平,但是其中一个维度仍然是无。所以然后我尝试了:

My CNN code takes in this input; however this is where I run into issues. I get a ValueError when trying to flatten the input just before my fully connected layers. It tries to flatten the array but one of the dimensions is still 'None'. So then I tried:

length = tf.shape(input)[1]
reshaped = tf.reshape(input, [batch_size, length, IMAGE_WIDTH, NUM_CHANNELS])

但该值仍然是'None'并且最初尝试构建图形时遇到问题。我的FC层(在展平中)在构建权重和偏差张量时显式采用了 input_to_layer.get_shape()[1],但无法处理无输入。

But still the value is 'None' and I am getting issues when trying to build the graph initially. My FC layer (and in flattening) explicitly takes in 'input_to_layer.get_shape()[1]' in building the weight and bias tensors, but it cannot handle the None input.

我对如何继续感到迷茫!非常感谢您的帮助,谢谢:)

I am quite lost as to how to proceed! Help would be much appreciated, thanks :)

##编辑##

Danevskyi在下面指出,这可能是不可能的。如果我想代替整个连接层,而是要在整个标题上合并(即对于从前一个转换层输出的1024个大小为(D,)的平面过滤器),我想通过均值池创建一个1024像素的矢量,该怎么办?超过每个过滤器的长度D)?一维全局平均池化有可能吗?再次在批次之间,D的值将有所不同...

Danevskyi points out below that this may not be possible. What if instead of the fully connected layer, I wanted to mean pool over the entire caption (i.e. for the 1024 flat filters of size (D,) outputted from the prior conv layer, I want to create a 1024-dim vector by mean pooling over the length D of each filter)? Is this possible with 1D Global Avg Pooling? Again between batches the value of D would vary...

##更新##

tflearn(tflearn.layers.conv.global_avg_pool)的全局均值池化方法不需要指定的窗口大小,它使用完整的输入维,因此即使与TensorFlow中未知的无维也兼容。 / p>

The global mean pooling method from tflearn (tflearn.layers.conv.global_avg_pool) doesn't need a specified window size, it uses the full input dimension, so it's compatible even with unknown 'None' dimensions in TensorFlow.

推荐答案

无法执行此操作,因为您想为每个连接使用不同形状的矩阵(用于全连接层)不同的批次。

There is no way to do this, as you want to use a differently shaped matrix (for fully-connected layer) for every distinct batch.

一种可能的解决方案是使用全局平均池(沿所有空间维度)获得形状为的张量(batch_size,1、1,NUM_CHANNELS ),而不考虑第二维。

One possible solution is to use global average pooling (along all spatial dimensions) to get a tensor of shape (batch_size, 1, 1, NUM_CHANNELS) regardless of the second dimension.

这篇关于Tensorflow:构建批量大小在1维上变化的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆