Tensorflow: building graph with batch sizes varying in dimension 1?

匆匆过客 提交于 2019-12-12 23:42:32

问题


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).

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))

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.

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])

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 :)

## EDIT ##

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...

## UPDATE ##

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.


回答1:


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

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.




回答2:


You can specify variable shape placeholders and use them to store variable length batches. If you look at the basic MNIST tutorial for TensorFlow, they use the following line for the input data placeholder x = tf.placeholder(tf.float32, [None, 784]) In the shape list, 784 is the size of an individual MNIST image (flattened into 1D), but None is the batch size. Here is a link to the tutorial https://www.tensorflow.org/get_started/mnist/beginners so you can look at the rest of the code



来源:https://stackoverflow.com/questions/43684048/tensorflow-building-graph-with-batch-sizes-varying-in-dimension-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!