AttributeError: 'str' object has no attribute '__stored_args__'

Hello Guys,

In the book Chapter 19 Fastai Learner from Scratch, I got an error in the below section of the code in Colab. Is the error due to a version of fastai or fastcore?

learn = Learner(simple_cnn(), dls, cross_entropy, lr=0.1, cbs=cbs)


AttributeError Traceback (most recent call last)
in <cell line: 1>()
----> 1 learn = Learner(simple_cnn(), dls, cross_entropy, lr=0.1, cbs=cbs)

1 frames
in init(self, model, dls, loss_func, lr, cbs, opt_func)
1 class Learner:
2 def init(self, model, dls, loss_func, lr, cbs, opt_func=SGD):
----> 3 store_attr(self, ‘model, dls, loss_func, lr, cbs, opt_func’)
4 for cb in cbs: cb.learner = self
5

/usr/local/lib/python3.10/dist-packages/fastcore/basics.py in store_attr(names, self, but, cast, store_args, **attrs)
398 else: self = fr.f_locals[args[0]]
399 if store_args is None: store_args = not hasattr(self,‘slots’)
→ 400 if store_args and not hasattr(self, ‘stored_args’): self.stored_args = {}
401 anno = annotations(self) if cast else {}
402 if names and isinstance(names,str): names = re.split(‘, *’, names)

AttributeError: ‘str’ object has no attribute 'stored_args

After going through fastcore documentation i used the below syntax and was able to fix the error

class T:
def init(self, a,b,c): store_attr(‘a,b,c’, self)

but got another error in the next step
learn. fit(1)
AttributeError Traceback (most recent call last)

[] in <cell line: 1>()
----> 1 learn. fit(1)

3 frames
in fit(self, n_epochs)
23
24 def fit(self, n_epochs):
—> 25 self(‘before_fit’)
26 self.opt = self.opt_func(self.model.parameters(), self.lr)
27 self.n_epochs = n_epochs

in call(self, name)
34
35 def call(self,name):
—> 36 for cb in self.cbs: getattr(cb,name,noop)()

in before_fit(self)
4 self.learner.batch = tfm_x(xb),yb
5
----> 6 def before_fit(self): self.model.cuda()

/usr/local/lib/python3.10/dist-packages/fastcore/basics.py in getattr(self, k)
495 attr = getattr(self,self._default,None)
496 if attr is not None: return getattr(attr,k)
→ 497 raise AttributeError(k)
498 def dir(self): return custom_dir(self,self._dir())
499 # def getstate(self): return self.dict

AttributeError: model

Hi Ksanand
My guess is that on page 537 the SGB class uses store_attr(self,‘params,lr,wd’) but I think they changed the code to remove the requirement for the self. Try it as store_attr(‘params,lr,wd’)
Regards Conwyn

1 Like

Hi Conwyn,

I was able to fix the no attribute ‘stored_args’ error (checked fastcore docs) but the new error occurs when I try, learn. Fit(1), possibly some issue with the learner class which I am unable to resolve on my own,

1>() ----> 1 learn. Fit(1)

in fit(self, n_epochs)
23
24 def fit(self, n_epochs):
—> 25 self(‘before_fit’) 26 self.opt = self.opt_func(self.model.parameters(), self.lr) 27 self.n_epochs = n_epochs

in call(self, name)
34
35 def call(self, name):
—> 36 for cb in self.cbs: getattr(cb,name,noop)()

in before_fit(self) 4 self.learner.batch = tfm_x(xb),yb
5
----> 6 def before_fit(self): self.learn.model.cuda()

AttributeError: ‘NoneType’ object has no attribute ‘model’

self.learner.batch v self.learn.model

Should it be self.learner.model ?

Not Sure, let me check.