Python Forum

Full Version: AttributeError: '' object has no attribute ''
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have no idea how to resolve this issue, please help
#[Convolution] -> [Batch Normalization] -> [ReLU]
def Conv_block2d(in_channels,out_channels,*args,**kwargs):
	return nn.Sequential(nn.Conv2d(in_channels,out_channels,*args,**kwargs,bias=False),nn.BatchNorm2d(out_channels,eps=0.001),nn.ReLU(inplace=True))

class MainBlock(nn.Module):
	"""docstring for MainBlock"nn.Module"""
	def __init__(self):
		super(MainBlock,self).__init__()

		self.pool_2x2 = nn.MaxPool2d(2,2)

		"""
		size = [3,18,36,7,5]
		for in_c,out_c,k in zip(size[:2],size[1:3],size[3:]):
		    print("in channel",in_c,"out channel",out_c,"kernel size",k)
			
		in channel 3 out channel 18 kernel size 7
		(256x256x3) * (7x7x18) = (250x250x18))*(2x2x1) = (125x125x18)
		in channel 18 out channel 36 kernel size 5
		(125x125x18 * (5x5x36) = (125x125x36))*(2x2x1) = (60x60x36)
		"""
		self.block1_size = [3,18,36,7,5]
		block1_out = [self.pool_2x2(Conv_block2d(in_c,out_c,kernel_size=k)) for in_c,out_c,k in zip(self.block1_out[:2],self.block1_out[1:3],self.block1_out[3:])]
		self.block1_output = nn.Sequential(*block1_out)

def forward(self,x):

		#Block 1 output (60x60x36)
		x = self.block1_output(x)	
                return x
I'm getting this error
Traceback (most recent call last):
  File "simpletest.py", line 170, in <module>
    net = MainBlock().cuda()
  File "simpletest.py", line 83, in __init__
    self.block1_out = [self.pool_2x2(Conv_block2d(in_c,out_c,kernel_size=k)) for in_c,out_c,k in zip(self.block1_out[:2],self.block1_out[1:3],self.block1_out[3:])]
  File "/home/ffffff/.virtualenvs/LearnPytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 518, in __getattr__
    type(self).__name__, name))
AttributeError: 'MainBlock' object has no attribute 'block1_out'
Indentation of lines 26 and 30 are wrong.
(Oct-19-2018, 08:44 AM)volcano63 Wrote: [ -> ]Indentation of lines 26 and 30 are wrong.

Even with proper Indentation i am getting the same error
(Oct-19-2018, 08:59 AM)Santhosh198915 Wrote: [ -> ]
(Oct-19-2018, 08:44 AM)volcano63 Wrote: [ -> ]Indentation of lines 26 and 30 are wrong.

Even with proper Indentation i am getting the same error

Line 23 - could you guess what you missed there?
(Oct-19-2018, 08:33 AM)Santhosh198915 Wrote: [ -> ]I have no idea how to resolve this issue, please help
#[Convolution] -> [Batch Normalization] -> [ReLU]
def Conv_block2d(in_channels,out_channels,*args,**kwargs):
	return nn.Sequential(nn.Conv2d(in_channels,out_channels,*args,**kwargs,bias=False),nn.BatchNorm2d(out_channels,eps=0.001),nn.ReLU(inplace=True))

class MainBlock(nn.Module):
	"""docstring for MainBlock"nn.Module"""
	def __init__(self):
		super(MainBlock,self).__init__()

		self.pool_2x2 = nn.MaxPool2d(2,2)

		"""
		size = [3,18,36,7,5]
		for in_c,out_c,k in zip(size[:2],size[1:3],size[3:]):
		    print("in channel",in_c,"out channel",out_c,"kernel size",k)
			
		in channel 3 out channel 18 kernel size 7
		(256x256x3) * (7x7x18) = (250x250x18))*(2x2x1) = (125x125x18)
		in channel 18 out channel 36 kernel size 5
		(125x125x18 * (5x5x36) = (125x125x36))*(2x2x1) = (60x60x36)
		"""
		self.block1_size = [3,18,36,7,5]
		block1_out = [self.pool_2x2(Conv_block2d(in_c,out_c,kernel_size=k)) for in_c,out_c,k in zip(self.block1_out[:2],self.block1_out[1:3],self.block1_out[3:])]
		self.block1_output = nn.Sequential(*block1_out)

def forward(self,x):

		#Block 1 output (60x60x36)
		x = self.block1_output(x)	
                return x
I'm getting this error
Traceback (most recent call last):
  File "simpletest.py", line 170, in <module>
    net = MainBlock().cuda()
  File "simpletest.py", line 83, in __init__
    self.block1_out = [self.pool_2x2(Conv_block2d(in_c,out_c,kernel_size=k)) for in_c,out_c,k in zip(self.block1_out[:2],self.block1_out[1:3],self.block1_out[3:])]
  File "/home/ffffff/.virtualenvs/LearnPytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 518, in __getattr__
    type(self).__name__, name))
AttributeError: 'MainBlock' object has no attribute 'block1_out'

(Oct-19-2018, 09:11 AM)volcano63 Wrote: [ -> ]
(Oct-19-2018, 08:59 AM)Santhosh198915 Wrote: [ -> ]Even with proper Indentation i am getting the same error

Line 23 - could you guess what you missed there?

Built this using this soruce https://towardsdatascience.com/pytorch-h...54597b5f17
(Oct-19-2018, 10:12 AM)Santhosh198915 Wrote: [ -> ]Built this using this soruce https://towardsdatascience.com/pytorch-h...54597b5f17

Python-wise - not a very good coding style, and you copy-pasted the bug in their code. I repeat - line 23. Try to analyze
it should have been block1_size inside zip instead to block1_out