[英]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]) ----> 1 conv1=utils.conv_layer(x,shape=[5,5,3,32]) 中的 TypeError Traceback (最近一次调用最后一次)
TypeError: conv_layer() got an unexpected keyword argument 'shape' TypeError:conv_layer() 得到了一个意外的关键字参数“shape”
But when I move the class keyword and use the code as simple function call like但是当我移动 class 关键字并将代码用作简单的 function 调用时
conv1=conv_layer(x,shape=[5,5,3,32]) 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.
我的理解是关键字“形状”在这里一团糟。
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.如果 conv_layer 作为 CnnUtils class 的方法,conv_layer 方法的第一个参数 input 指的是 class CnnUtils 的实例。 Therefore, when you call utils.conv_layer(x,shape=[5,5,3,32]), x is assigned as the value of shape.
因此,当你调用 utils.conv_layer(x,shape=[5,5,3,32]) 时,x 被赋值为 shape 的值。 [just print the value of input and shape in conv_layer method].
[只需在 conv_layer 方法中打印输入和形状的值]。 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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.