Search code examples
tensorflowneural-networkconv-neural-network

Why do I get a type error when trying to run convolutional neural network on pycharm, with python


I've been trying to build a neural network model that will be able to classify images. But there is this one consistent error that keeps on popping up. Can anyone please help me with this? Here are the code and error below: Second Tracebackthird Traceback

x=tf.placeholder(tf.float32,shape=[None,img_size])
y=tf.placeholder(tf.float32,shape=[None,no_classes])
#keep_probable=tf.argmax(y,dimension=1)
keep_probable=tf.placeholder(tf.float32)

def conv2d(x,w,b,strides=1):
    x=tf.nn.conv2d(x,w, strides=[1,strides,strides,1],padding='SAME')
    x=tf.nn.bias_add(x,b)
    return tf.nn.relu(x)

def maxpool2d(x,k=2):
    return tf.nn.max_pool(x,ksize=[1,k,k,1],strides=[1,k,k,1],padding='SAME')

def conv_net(x,weights,biases,drop_out):
    x=tf.reshape(x,shape=[-1,50,50,1])

    conv1=conv2d(x,weights['wc1'],biases['bc1'])
    conv1=maxpool2d(conv1,k=2)

    conv2=conv2d(conv1,weights['wc2'],biases['bc2'])
    conv2=maxpool2d(conv2,k=2)

    fcl=tf.reshape(conv2,[-1,weights['wd1'].get_shape().as_list()[0]])
    fcl=tf.add(tf.matmul(fcl,weights['wd1'])[][2],biases['bd1'])
    fcl=tf.nn.relu(fcl)

    # application of dropout
    fcl.tf.nn.dropout(fcl,dropout)
    # output of the class prediction
    out=tf.add(tf.matmul(fcl,weights['out']),biases['out'])

    return out

weights = {

'wc1': tf.Variable((tf.random_normal)([5,5,1,32])),
'wc2': tf.Variable((tf.random_normal)([5,5,32,64])),
'wd1': tf.Variable((tf.random_normal)([7*7*64,1024])),
'out': tf.Variable((tf.random_normal)([1024,no_classes]))

}

biases = {

'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([no_classes]))

 }

# construction of a model
pred = conv_net(x,weights,biases,keep_probable)

# definition of the loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels =y))
optimiser = tf.train.AdadeltaOptimizer(learning_rate=LR).minimize(cost)

# Evaluating the model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

Following is the error I get:

TypeError: Expected bool for argument 'transpose_a' not <tf.Variable 'Variable_6:0' shape=(1024,) dtype=float32_ref>.


Solution

  • Problem is in your line

    fcl = tf.add(tf.matmul(fcl, weights['wd1'], biases['bd1']))
    

    You are missing a bracket. Do this:

    fcl = tf.add(tf.matmul(fcl, weights['wd1']), biases['bd1'])