Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Sunday, 7 May 2023

Can't import the encoder code for fine tuning GPT-2

 I'm trying to reproduce the example from this article: https://medium.com/@ngwaifoong92/beginners-guide-to-retrain-gpt-2-117m-to-generate-custom-text-content-8bb5363d8b7f

The example code is from the following repo: https://github.com/nshepperd/gpt-2

After installing the requirements and downloading the model, the following step is to train the model, for which this code has to be executed:

python encode.py lyric.txt lyric.npz

The issue here is that this requires to import the following modules:

import argparse
import numpy as np

import encoder
from load_dataset import load_dataset

Where encoder and load_dataset are on a child directory:

|--encode.py
 --src
   |--encoder.py
   |--load_dataset.py

This generates the following error:

ModuleNotFoundError: No module named 'encoder'

I tried creating the __init__.py files and importing them as

src.encoder and src.load_dataset but that those not work either.

In the medium post the author proposes to move the file encoder.py to src and execute the code from there, the issue there is that doing it breaks the relative path for the model too and although I handled that the issue with the paths keeps going for other files as well.

  1. Make an empty file named __init__.py inside the src folder.
  2. Import encoder as:from src import encoder
  3. encode.py

   none of that worked for me. however what i did was i installed python 3.6 because tensorflow is compatible with this version, I installed the following requirements :

h5py==3.1.0      
idna==2.10       
protobuf==3.13.0     
pywin32==228        
regex==2020.10.28 
requests==2.24.0     
tensorboard==2.3.0      
tensorflow==2.3.1      
tensorflow-estimator==2.3.0      
tqdm==4.51.0     
urllib3==1.25.11

updated the visual C++ for tensorflow https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads and run the encode.py and it worked.

This is not the 100% correct way to do this but to get around settign a pythonpath, you can copy the files into the src/ directory from the gpt-2/ directory.

So after you are in the gpt-2 directory you can run the following in your cmd line.

cp encode.py src\
cp train.py src\
cp models\ src\

If you do all of this then cd into src/ and run:

python filetoencode.txt filetoencode.npz

you should now have an encoded file and be able to move onto the next steps in the blog post.

Enjoy and have fun with the gpt-2 :)

I know this is a really old topic, but this worked for me: Change encode.py imports to

import src.encoder as encoder
from src.load_dataset import load_dataset

Then you can run

 py encode.py trainingfile.txt training.npz

No comments:

Post a Comment

Connect broadband

AI:

 The question you've posed is complex and speculative, as it involves a mix of robotics, artificial intelligence, cultural consideration...