Edison
Edison

Reputation: 4291

Got unexpected keyword argument shape

In my following Code

class cnnUtils:

    def get_weight(shape):
        init=tf.truncated_normal(shape,stddev=0.1)
        return tf.Variable(init)
    def get_bias(shape):
        init=tf.constant(0.1,shape=shape)
        return tf.Variable(init)
    def conv2d(x,w):
        return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
    def maxpool_2d(x):
        return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
    def conv_layer(input,shape):
        b=get_bias([shape[3]])
        w=get_weight(shape)
        return tf.nn.relu(conv2d(input,w)+b)
    def full_layer(input,size):
        in_size=int(input.get_shape()[1])
        w=get_weight([in_size,size])
        b=get_bias([size])
        return tf.matmul(input,w)+b
utils=CnnUtils()
x=tf.placeholder(tf.float32,shape=[None,32,32,3])
y=tf.placeholder(tf.float32,shape=[None,10])
conv1=utils.conv_layer(x,shape=[5,5,3,32])

I am getting following error


TypeError Traceback (most recent call last) in ----> 1 conv1=utils.conv_layer(x,shape=[5,5,3,32])

TypeError: conv_layer() got an unexpected keyword argument 'shape'

But when I move the class keyword and use the code as simple function call like

conv1=conv_layer(x,shape=[5,5,3,32])

Erors got finished. Can somebody explain me what is happening here? My understanding is that the keyword "shape" is in a mess here.

Upvotes: 0

Views: 2781

Answers (1)

Kaushik Roy
Kaushik Roy

Reputation: 1685

In case of conv_layer as a method of CnnUtils class, 1st argument of conv_layer method, input, refers to the instance of class CnnUtils. Therefore, when you call utils.conv_layer(x,shape=[5,5,3,32]), x is assigned as the value of shape. [just print the value of input and shape in conv_layer method]. So the working implementation is as follows:

import tensorflow as tf


class CnnUtils:

    def get_weight(self, shape):
        init=tf.truncated_normal(shape,stddev=0.1)
        return tf.Variable(init)

    def get_bias(self, shape):
        init=tf.constant(0.1,shape=shape)
        return tf.Variable(init)

    def conv2d(self, x, w):
        return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")

    def maxpool_2d(self, x):
        return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

    def conv_layer(self, input, shape):
        b=self.get_bias([shape[3]])
        w=self.get_weight(shape)
        return tf.nn.relu(self.conv2d(input,w)+b)

    def full_layer(self, input, size):
        in_size=int(input.get_shape()[1])
        w=self.get_weight([in_size,size])
        b=self.get_bias([size])
        return tf.matmul(input,w)+b


utils=CnnUtils()
x=tf.placeholder(tf.float32,shape=[None,32,32,3])
y=tf.placeholder(tf.float32,shape=[None,10])
conv1=utils.conv_layer(x, shape=[5,5,3,32])

Upvotes: 2

Related Questions