**Title:** Numpy 1.23.5 and fastai 2.8 incompatibility issue when running `lr_find()`


Title: Numpy 1.23.5 and fastai 2.8 incompatibility issue when running lr_find()

Hello fastai community,

I’m encountering a compatibility issue between numpy 1.23.5 and fastai 2.8 that prevents me from running the learning rate finder. Here’s my code:

print("\n--- Inspección de los DataLoaders ---")
# Muestra un lote de imágenes del set de entrenamiento con las aumentaciones aplicadas
print("Ejemplos de imágenes de entrenamiento con aumentaciones (dls.train):")
dls.train.show_batch(max_n=8, nrows=2, unique=True)
plt.show() # Asegura que el gráfico se muestre

# Muestra un lote de imágenes del set de validación (generalmente sin aumentaciones de batch)
print("\nEjemplos de imágenes de validación (dls.valid):")
dls.valid.show_batch(max_n=4, nrows=1)
plt.show() # Asegura que el gráfico se muestre

# --- 5. Creación y entrenamiento del modelo ---
# Carga un modelo preentrenado (resnet18) y define la métrica de error
learn = vision_learner(dls, resnet18, metrics=error_rate)

# Encuentra la tasa de aprendizaje óptima para el modelo y el conjunto de datos
print("\n--- Buscando la tasa de aprendizaje óptima ---")
lr_sug = learn.lr_find()
# Se toma la tasa de aprendizaje sugerida por la heurística 'valley' de FastAI
suggested_lr = lr_sug.valley
print(f"Tasa de aprendizaje sugerida: {suggested_lr}")

# Entrena el modelo usando 'fine_tune' con la tasa de aprendizaje óptima y más épocas
print(f"\n--- Iniciando el entrenamiento del modelo por 10 épocas con LR={suggested_lr:.1e} ---")
learn.fine_tune(10, base_lr=suggested_lr)

print("\nEntrenamiento completado.")

Error message:

AttributeError: Exception occured in `LRFinder` when calling event `after_fit`: 
module 'numpy' has no attribute 'dtypes'

Full traceback:

Click to expand
Buscando la tasa de aprendizaje óptima 
-----AttributeError Traceback (most recent call last) Cell In[7], line 18 
16 # Encuentra la tasa de aprendizaje óptima para el modelo y el conjunto de datos 
17 print("\n--- Buscando la tasa de aprendizaje óptima ---") 
---> 18 lr_sug = learn.lr_find() 
19 # Se toma la tasa de aprendizaje sugerida por la heurística 'valley' de FastAI 
20 suggested_lr = lr_sug.valley 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/schedule.py:295](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/schedule.py#line=294), in lr_find(self, start_lr, end_lr, num_it, stop_div, show_plot, suggest_funcs) 
293 n_epoch = num_it//len(self.dls.train) + 1 
294 cb=LRFinder(start_lr=start_lr, end_lr=end_lr, num_it=num_it, stop_div=stop_div) 
--> 295 with self.no_logging(): self.fit(n_epoch, cbs=cb) 
296 if suggest_funcs is not None: 
297 lrs, losses = tensor(self.recorder.lrs[num_it//10:-5]), tensor(self.recorder.losses[num_it//10:-5]) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:272](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=271), in Learner.fit(self, n_epoch, lr, wd, cbs, reset_opt, start_epoch) 
270 self.opt.set_hypers(lr=self.lr if lr is None else lr) 
271 self.n_epoch = n_epoch 
--> 272 self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:209](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=208), in Learner._with_events(self, f, event_type, ex, final) 
207 try: self(f'before_{event_type}'); f() 
208 except ex: self(f'after_cancel_{event_type}') 
--> 209 self(f'after_{event_type}'); final() 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:180](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=179), in Learner.__call__(self, event_name) 
--> 180 def __call__(self, event_name): L(event_name).map(self._call_one) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/foundation.py:163](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/foundation.py#line=162), in L.map(self, f, *args, **kwargs) 
--> 163 def map(self, f, *args, **kwargs): return self._new(map_ex(self, f, *args, gen=False, **kwargs)) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/basics.py:934](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/basics.py#line=933), in map_ex(iterable, f, gen, *args, **kwargs) 
932 res = map(g, iterable) 
933 if gen: return res 
--> 934 return list(res) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/basics.py:919](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastcore/basics.py#line=918), in bind.__call__(self, *args, **kwargs) 
917 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i) 
918 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:] 
--> 919 return self.func(*fargs, **kwargs) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:184](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=183), in Learner._call_one(self, event_name) 
182 def _call_one(self, event_name): 
183 if not hasattr(event, event_name): raise Exception(f'missing {event_name}') 
--> 184 for cb in self.cbs.sorted('order'): cb(event_name) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/core.py:64](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/core.py#line=63), in Callback.__call__(self, event_name) 
62 try: res = getcallable(self, event_name)() 
63 except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise 
---> 64 except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True) 
65 if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit 
66 return res 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/core.py:62](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/core.py#line=61), in Callback.__call__(self, event_name) 
60 res = None 
61 if self.run and _run: 
---> 62 try: res = getcallable(self, event_name)() 
63 except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise 
64 except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True) 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/schedule.py:205](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/callback/schedule.py#line=204), in LRFinder.after_fit(self) 
203 tmp_f = self.path[/self.model_dir/self.tmp_p/](http://localhost:8888/self.model_dir/self.tmp_p/)'_tmp.pth' 
204 if tmp_f.exists(): 
--> 205 self.learn.load(f'{self.tmp_p}[/_tmp](http://localhost:8888/_tmp)', with_opt=True) 
206 self.tmp_d.cleanup() 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:428](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=427), in load(self, file, device, **kwargs) 
426 file = join_path_file(file, self.path[/self.](http://localhost:8888/self.model_dir)[model_dir](http://localhost:8888/self.model_dir), ext='.pth') 
427 distrib_barrier() 
--> 428 load_model(file, self.model, self.opt, device=device, **kwargs) 
429 return self 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py:55](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/fastai/learner.py#line=54), in load_model(file, model, opt, with_opt, device, strict, **torch_load_kwargs) 
53 elif device is None: device = 'cpu' 
54 if ismin_torch("2.5"): 
---> 55 context = torch.serialization.safe_globals([L, np.core.multiarray.scalar, np.dtype, *[getattr(np.dtypes, dt) for dt in np.dtypes.__all__]]) 
56 torch_load_kwargs.setdefault("weights_only", True) 
57 else: context = nullcontext() 
File [/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py:311](http://localhost:8888/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py#line=310), in __getattr__(attr) 
308 from .testing import Tester 
309 return Tester 
--> 311 raise AttributeError("module {!r} has no attribute " 
312 "{!r}".format(__name__, attr)) 
AttributeError: Exception occured in `LRFinder` when calling event `after_fit`: module 'numpy' has no attribute 'dtypes'

Question:

  1. Is there a planned update for pip fastai to address this numpy compatibility issue?
  2. Has anyone faced this problem before and knows how to solve it?
  3. Currently, the only workaround I can think of is using a fixed learning rate through trial and error, which is quite cumbersome.

Environment:

  • fastai: 2.8
  • numpy: 1.23.5
  • Python: 3.10

Thank you for your help!