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
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 721 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,533 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,668 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 687 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,215 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,302 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,780 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,352 Jul-29-2022, 09:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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