Search code examples
tensorflowmachine-learningjuliaupdatebatchsize

TensorFlow, Julia // How to use different batch size in my neural network?


Hello

I created a code with TensorFlow and Julia to create a neural network.

I have this problem: My network works with a batch size of 1 and does not work when the batch size is greater...

Here is my code:

You can change the batch_size beetween 1 and 10

ENV["CUDA_VISIBLE_DEVICES"] = "0" # It is to use the gpu
using TensorFlow
using Distributions

sess = Session(Graph())

batch_size = 10  # For this example, 0 < batch_size < 11
num_pixels = 64

###########

# Data base: 10 arrays, the first array is fill with 1, the second with 2 etc...

arrays = zeros(Float32,10,num_pixels,num_pixels)

arrays_2 = zeros(Float32,10)

for i in 1:10
    for j in 1:num_pixels
        for k in 1:num_pixels
            arrays[i,j,k] = i
            end
    end
end


for i in 1:10
    arrays_2[i] = i
end

###########

# inputs

x = placeholder(Float32, shape= [batch_size, 1, 1, 1])

y = placeholder(Float32)

###########

 # Function to create a batch

function create_batch(batch_size)
    x = zeros(Float32, batch_size,num_pixels, num_pixels)
    y = zeros(Float32, batch_size)

    for i in 1:batch_size
        x[i, : ,:] = arrays[i,:,:]

        y[i] = arrays_2[i]
    end
    y, x
end


###########

# Create the different layers ; poids = weight

variable_scope("mymodel" * randstring(), initializer=Normal(0, .001)) do
    global poids_1 = get_variable("p1", [2,2,2,1], Float32)
    global poids_2 = get_variable("p2",[4,4,3,2],Float32)
    global poids_3 = get_variable("p3",[2,2,4,3],Float32)
    global poids_4 = get_variable("p4",[1,4,4,4],Float32)
    global poids_5 = get_variable("p5",[1,4,4,4],Float32)
    global poids_6 = get_variable("p6",[1,4,4,4],Float32)
    global biases_1 = get_variable("b1",[2],Float32)
    global biases_2 = get_variable("b2",[3],Float32)
    global biases_3 = get_variable("b3",[4],Float32)
    global biases_4 = get_variable("b4",[4],Float32)
    global biases_5 = get_variable("b5",[4],Float32)
    global biases_6 = get_variable("b6",[4],Float32)
end

logits_1 = nn.relu(nn.conv2d_transpose(x, poids_1, [batch_size,2,2,2], [1,2,2,1],padding = "SAME") + biases_1)

logits_2 = nn.relu(nn.conv2d_transpose(logits_1,poids_2, [batch_size,4,4,3], [1,2,2,1],padding = "SAME") + biases_2)

logits_3 = nn.relu(nn.conv2d_transpose(logits_2,poids_3, [batch_size,8,8,4], [1,2,2,1],padding = "SAME") + biases_3)

logits_4 = nn.relu(nn.conv2d_transpose(logits_3,poids_4, [batch_size,16,16,4], [1,2,2,1],padding = "SAME") + biases_4)

logits_5 = nn.relu(nn.conv2d_transpose(logits_4,poids_5, [batch_size,32,32,4], [1,2,2,1],padding = "SAME") + biases_5)

 logits_6 = nn.relu(nn.conv2d_transpose(logits_5,poids_6, [batch_size,64,64,4], [1,2,2,1],padding = "SAME") + biases_6)

logits_6 = reduce_sum(logits_6, axis=[4])


if batch_size == 1

logits = reshape(logits_6, [num_pixels,num_pixels])

else

    logits = reshape(logits_6, [batch_size,num_pixels,num_pixels])

end


smax = nn.softmax(logits)



cross_entropy = reduce_mean(-reduce_sum(y.*log(smax))) # loss function

optimizer = train.AdamOptimizer(0.0001)

train_op = train.minimize(optimizer,cross_entropy)

batch = create_batch(batch_size)

run(sess, global_variables_initializer())

x_ = run(sess, train_op, Dict(x => reshape(batch[1], (batch_size,1,1,1)), y => reshape(batch[2], (batch_size,num_pixels,num_pixels))))

If the batch_size is greater than 1, I have the following error where the input shapes are [batch_size,num_pixels,num_pixels], [batch_size*num_pixels, 1]:

    ERROR: LoadError: On worker 2:
Python error: PyObject ValueError(u"Dimensions must be equal, but are 64 and 640 for 'gradients/Softmax_grad/sub' (op: 'Sub') with input shapes: [10,64,64], [640,1].",)

If the batch_size is 1, my logits must be a tensor of 2 dimensions, but if the batch_size is greater than 1, must my logits be a tensor of 3 dimensions (add the batch_size to the dimensions) ?

How can I change the batch_size ?

Thank you


Solution

  • I found a solution.

    When the logits is reshaped, use the following reshape:

    logits = reshape(logits_6, [batch_size,num_pixels*num_pixels])