dvgodoy / PyTorchStepByStep

Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"
https://pytorchstepbystep.com
MIT License
834 stars 310 forks source link

importing data_generation.square_sequences failing #46

Closed ggankhuy closed 5 months ago

ggankhuy commented 6 months ago

I am getting following when I try import, any idea???

[root@localhost PyTorchStepByStep]# python3 -c "import data_generation.square_sequences"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'data_generation.square_sequences'

I made similar boilerplate code dir1/dir1a.py and it works:

python3 -c "import dir1.dir1a"
[root@localhost PyTorchStepByStep]# cat dir1/dir1a.py

def dir1a():
    print("dir1a:")

[root@localhost PyTorchStepByStep]# git remote -v
origin  https://github.com/dvgodoy/PyTorchStepByStep.git (fetch)
origin  https://github.com/dvgodoy/PyTorchStepByStep.git (push)
[root@localhost PyTorchStepByStep]# pwd
/root/gg/git/codelab/gpu/ml/tf/tf-from-scratch/3/PyTorchStepByStep

/root/gg/git/codelab/gpu/ml/tf/tf-from-scratch/3/PyTorchStepByStep
[root@localhost PyTorchStepByStep]# uname -r
6.1.77kernel_org
[root@localhost PyTorchStepByStep]# cat /etc/os-release | grep -i cent
NAME="CentOS Stream"
ID="centos"
PRETTY_NAME="CentOS Stream 9"
CPE_NAME="cpe:/o:centos:centos:9"
HOME_URL="https://centos.org/"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
[root@localhost PyTorchStepByStep]# pip3 list | grep torch
pytorch-triton-rocm 2.1.0+dafe145982
torch               2.2.0.dev20231209+rocm5.7
torchaudio          2.2.0.dev20231209+rocm5.7
torchbench          0.1                        /root/gg/git/benchmark
torchvision         0.17.0.dev20231209+rocm5.7
dvgodoy commented 6 months ago

Hi @ggankhuy ,

The file you're trying to import is this one: https://github.com/dvgodoy/PyTorchStepByStep/blob/master/data_generation/square_sequences.py

However, it looks like the subfolder data_generation and its file square_sequences.py are missing in the folder where you're running the import command. That's why you're getting this error.

Perhaps it would be best to start fresh in a different folder and clone the full repo:

git clone https://github.com/dvgodoy/PyTorchStepByStep.git

Once the full repo is cloned, all subfolders and their files should be available in your local setup, and you shouldn't get that error anymore.

Hope it helps! Best, Daniel

ggankhuy commented 6 months ago

Files are not missing but in one environment it works but in another environment (container) it does not. What i found out is that in a non-working environment, I added dummy/empty init.py file in data_generation folder and it worked. I recall long ago init.py must be there. I moved on however still not sure why working case did not need init.py.

dalgurnawi commented 6 months ago

Hello everyone,

I have been experiencing the same issue trying to run the file in Google Collab.

Image 1

The import runs through, but get the warning "Import could not be resolved".

Image 2

This leads to the following errors:

Image 3

dvgodoy commented 6 months ago

Hi @dalgurnawi

Thank you for reporting this error in figure 8 of Chapter 8. I've just fixed that and pushed it to the repo. If you start a new Colab session for this notebook, it should work now.

This error was unrelated to the import issue, it happened because of a deprecated attribute in Numpy. Even though Colab shows you that "could not be resolved" message, the generate_sequences() was still successfully imported.

Please let me know if you find any other errors, I really appreciate it :-)

Best, Daniel

dvgodoy commented 6 months ago

Files are not missing but in one environment it works but in another environment (container) it does not. What i found out is that in a non-working environment, I added dummy/empty init.py file in data_generation folder and it worked. I recall long ago init.py must be there. I moved on however still not sure why working case did not need init.py.

Hi @ggankhuy ,

The __init__.py file turns it into a package, so I guess that addressed whatever was the issue in the container. Even in Colab, we get the "could not be resolved" message but the functions are imported nonetheless. The reason why I used these separate files as imports was to keep code that's not related to the main topic (such as data generation and plots) out of the way. I'm glad to hear you managed to make both environments work :-)

Best, Daniel

ggankhuy commented 5 months ago

Not sure if to discuss in separate issue, i started off in ch9 where in p229: Capture-2 It says "in the 1st square, drawing started at top right corner and in the sequence CDAB in clockwise direction. But from 9.3, i am not seeing instead 1st square's title above says "counterclockwise and started off with 4321 if clockwise. Is it typo? I am completely lost here. I could related this to randomness of the data generation while writing the text vs. time illustration was made but seed should make it deterministic is it not??

Is mapping are such that 1234 is ABCD? I could try relating the above paragraph to 2nd square which matches 1) CDAB and 2) clockwise but following statements "source sequence ... C and D (1 and 2), target sequence ... A and B (3 and 4) makes my assumption that 1234 vs. ABCD mapping invalid. But source and target sequence makes no sense whatsoever.

dvgodoy commented 5 months ago

Hi @ggankhuy ,

I am sorry for the confusion, you're absolutely right, the text it should have been updated to the THIRD square for the sentence to make sense. The third square in Figure 9.3 (not the first one, that was a mistake) is the one corresponding to the description: Screenshot from 2024-04-08 10-06-36

This square starts (node 1) on the top-right corner and numbering follows a clockwise order. The numbers denote the sequence in which the nodes are drawn. If we're trying to use two nodes (source sequence) to predict the other two (target sequence), the source is always going to be 1 and 2, and the target is always going to be 3 and 4.

But we may start at any of the four corners, right? In Figure 9.1 I "labeled" the corners using letters (see screenshot for the clockwise one): Screenshot from 2024-04-08 10-06-25 If we start at the top-right corner (that was "labeled" C), we'll follow CDAB. In this particular example, C=1, D=2, A=3, B=4 because we started on C.

As you mentioned, this mistake was probably due to redrawing the figures which, unfortunately, is never guaranteed to be 100% reproducible whenever PyTorch versions change.

I hope this helps! Once again, I apologize for the confusion.

Thank you for pointing this out.

Best, Daniel

ggankhuy commented 5 months ago

ok, seems third one seems to match well. i know i started directly from ch9 so might have skipped over definition of ABCD/1234 meaning, but it seems: ABCD=[lower left, upper left, upper right, lower right] 1234= just tells the sequence where it starts and which direction, it does not really map one-by-one 1234 to ABCD. Thanks