3

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.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

5 Answers5

2
  1. Make an empty file named __init__.py inside the src folder.
  2. Import encoder as:from src import encoder
0x5050
  • 1,221
  • 1
  • 17
  • 32
2

Look at the source of encode.py (here). At the very beginning, there is this:

# Usage:
#  PYTHONPATH=src ./encode.py <file|directory|glob> /path/to/output.npz
#  PYTHONPATH=src ./train --dataset /path/to/output.npz

Please try again, setting PYTHONPATH as they suggest you do.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
1

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.

haruo
  • 11
  • 1
0

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 :)

Steven Barnard
  • 514
  • 7
  • 12
0

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
user1692094
  • 302
  • 2
  • 5
  • 12