Python Forum
AttributeError: '' object has no attribute ''
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: '' object has no attribute ''
#1
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'
Reply
#2
Indentation of lines 26 and 30 are wrong.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(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
Reply
#4
(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?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
(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
Reply
#6
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
it should have been block1_size inside zip instead to block1_out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 281 Aug-17-2021, 05:24 PM
Last Post: Franky77
  AttributeError: 'DataFrame' object has no attribute 'Articles' Anldra12 5 1,825 Jun-17-2021, 04:54 PM
Last Post: Larz60+
  AttributeError: 'Node' object has no attribute 'insert' Anldra12 4 1,294 May-11-2021, 10:12 AM
Last Post: Anldra12
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 2,341 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  AttributeError: 'tuple' object has no attribute 'format' Anldra12 7 6,231 Apr-13-2021, 07:45 AM
Last Post: Anldra12
Lightbulb 'str' object has no attribute 'to_csv' brunolelli 3 3,526 Mar-25-2021, 06:40 AM
Last Post: ndc85430
  AttributeError: 'Message' object has no attribute 'split' helpme1 2 1,426 Mar-14-2021, 11:25 AM
Last Post: helpme1
  AttributeError class object has no attribute list object scttfnch 5 887 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  AttributeError: 'NoneType' object has no attribute 'next' loves 2 2,783 Dec-15-2020, 11:30 PM
Last Post: bowlofred
  How to Locate an Attribute's Parent Object? calvinsomething 5 933 Nov-13-2020, 01:52 AM
Last Post: calvinsomething

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020