0

i am trying to run a KI by using tensorflow. My Input is a Frame with a size of 10x10 pixel. So my s = tf.placeholder("float", [None, **10, 10**, 4]). But i get the following error and do not understand the issue. Any help?

Error "Negative dimension size caused by subtracting 4 from 1 for 'Conv2D_49' (op: 'Conv2D') with input shapes: [?,1,1,32], [4,4,32,64]."

#create tensorflow graph
def createGraph():

    #first convolutional layer. bias vector
    #creates an empty tensor with all elements set to zero with a shape
    W_conv1 = tf.Variable(tf.zeros([8, 8, 4, 32]))
    b_conv1 = tf.Variable(tf.zeros([32]))

    W_conv2 = tf.Variable(tf.zeros([4, 4, 32, 64]))
    b_conv2 = tf.Variable(tf.zeros([64]))

    W_conv3 = tf.Variable(tf.zeros([3, 3, 64, 64]))
    b_conv3 = tf.Variable(tf.zeros([64]))

    W_fc4 = tf.Variable(tf.zeros([3136, 784]))
    b_fc4 = tf.Variable(tf.zeros([784]))

    W_fc5 = tf.Variable(tf.zeros([784, ACTIONS]))
    b_fc5 = tf.Variable(tf.zeros([ACTIONS]))

    #input for pixel data
    s = tf.placeholder("float", [None, 10, 10, 4])


    #Computes rectified linear unit activation fucntion on  a 2-D convolution given 4-D input and filter tensors. and 
    conv1 = tf.nn.relu(tf.nn.conv2d(s, W_conv1, strides = [1, 4, 4, 1], padding = "VALID") + b_conv1)

    conv2 = tf.nn.relu(tf.nn.conv2d(conv1, W_conv2, strides = [1, 2, 2, 1], padding = "VALID") + b_conv2)

    conv3 = tf.nn.relu(tf.nn.conv2d(conv2, W_conv3, strides = [1, 1, 1, 1], padding = "VALID") + b_conv3)

    conv3_flat = tf.reshape(conv3, [-1, 3136])

    fc4 = tf.nn.relu(tf.matmul(conv3_flat, W_fc4) + b_fc4)

    fc5 = tf.matmul(fc4, W_fc5) + b_fc5

    return s, fc5
qwertz
  • 619
  • 1
  • 7
  • 15
  • You are trying to convolve a 1 pixel image (with 32 channels) with 64 kernels of size 4x4. The number of channels match, but the image size do not. You are using `VALID` padding, but your image is too small to produce a single valid result. – jdehesa May 14 '19 at 12:29
  • a 1 Pixel Image? isnt it a 10x10 Pixel Image? So what would be a valid kernels number and size? – qwertz May 14 '19 at 12:40
  • 1
    The input is a 10x10 image, but after the first convolutional layer (8x8 kernels and 4x4 strides) you are left with just one pixel, which is not enough for the second layer. It is up to you what sizes to use, but they must produce valid results. See for example [Calculate the Output size in Convolution layer](https://stackoverflow.com/q/53580088/1782792) (the formula there applies per dimension, and assumes `VALID` padding mode and no dilation, but that seems to be your use case). – jdehesa May 14 '19 at 12:49
  • Oh the error ocurres in the second layer. Didnt see that. Thanks! – qwertz May 14 '19 at 12:52
  • Please change ```padding = "VALID"``` to ```padding = "SAME"```, It will resolve negative dimension error in all your convolution layers. – bsquare Apr 23 '20 at 07:39

0 Answers0