Trying to import movie_recommender example function onto index 'nn' is not defined

I’ve created and posted the Fastai movie recommender using nbdev HERE where the recommender worked nicely.

I’m trying to add the get_movie_recs function from the 00_movie_recommender.ipynb to the index.ipynb file so it shows up on the README.

Despite using:

from movie_recommender.movie_recommender import *
from fastai.tabular.all import *
from fastai.collab import *

Running get_movie_recs function on index.ipynb throws:

"NameError: name 'nn' is not defined"

I’ve tried adding torch to the ‘requirements’ in settings.ini to no avail. How can I get index.ipynb to recognize the ‘nn’ from nn.CosineSimilarity?

Any help would be greatly appreciated.

Very strange, how about

import torch.nn as nn

Thanks for the idea, but it didn’t work. I added :

import torch.nn as nn

to both .ipynb files and it still will not run on the index. I can copy and paste the whole function into the index notebook, but that seems to defeat the purpose right?

Yeah whole function isn’t ideal.

Is pytorch installed?

If not could pip install it

!pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

so fastai apparently already needs torch to be loaded which is why nn.Cosin… works in the movie_recommender.ipynb file. I tried your pip install but this(below) happpend and it still throws the error: 'nn' is not defined

Could you try importing torch & torch.nn as below?

import torch
import torch.nn as nn

@daveramseymusic Hey, you are not exporting your imports. In particular, when you check movie-recommender/movie-recommender.py you can see that it doesnt import anything, so it can not know of nn. Add #| export to the top of

# For modeling
from fastai.tabular.all import *
from fastai.collab import *
#for publishing the model
import gradio as gr

and it should solve your problem :slight_smile:

Edit: Great project btw, it picked my movie for tonight :laughing:

2 Likes

Aha! That worked nicely.

Glad to hear about the movie too. I’m about to load a larger 20mil reviews version in an hour.

Is it possible to import the learn object with the get_movie_recs() function?

In order to get the function to run simply on in the gradio block I must add learn=learn in the function parameters.

Now when I run: from movie_recommender.movie_recommender import *

This is the error:

NameError: name 'learn' is not defined

I’ve even saved the object into the index.ipynb notebook before the import to no avail using :

learn = load_learner(path/'movie_predictor_large20mil_639.pkl')

Is there a better way to approach this? Ideally, I wouldn’t have redundant code in the index.ipynb just to show the function in the README.

Update: I fixed the error: “name ‘learn’ is not defined” by adding #| export to the learn=load_learner(path/'movie_predictor_large20mil_639.pkl') cell.

In case this is useful for anyone else:

I was having difficulty running nbdev_prepare because the README was not in the nbs directory.

I fixed it using this code where I loaded the learn and titles:

#| export
#load the model and movie titles with indexes (from the previous data loaders)
path = Path('.')
data_folder = '../models_and_dls'

#check if modedels folder is up one level if not 'for readme' then check in current directory
if not os.path.isdir(data_folder):
    data_folder = './models_and_dls'
#load learner and titles csv    
learn = load_learner(path/data_folder/'movie_predictor_large20mil_xtrau_624.pkl')
titles = pd.read_csv(path/data_folder/'movie_20mil_xtrau_dls.csv')

1 Like

I wrote here about how to work with repo-relative paths, you might find it helpful:

2 Likes