Classes and object in pytorch

torch.tensor()
torch.utils.data.dataloader()
my question is in the above two examples if we consider torch as a package we cannot have tensor() function inside a package as it should be present inside a .py file . if we consider tensor as a .py file then utils is a class then what data is . hence it contradicts
is torch a package and tensor() is a function present inside a file init so that we write torch.tensor() insted of torch.init.tensor()

Hi,
In a python package you can import objets in __init__. Those objects can be directly called as you aforementioned.

So if “tensor” exist in a file called torch.core.tensor (for example) if you write in torch’s __init__

from torch.core.tensor import tensor

you will be able to call torch.tensor

i still cant get it could you please tell tell behind the scene operation that happens starting from importing torch to what is class and modules in the two examples above

@JuanFMontesinos sorry if i am disturbing you . i would like to ask you one question
1.if i import torch then if there is a file called tensor.py which contains the (assume) class tensor1 then if i need to call the class tensor1 is it enough to call tensor1() or should i call tensor.tensor1

Hi,
Pytorch is a complex library (maybe not the best one to learn basics)
I’m not developer and I don’t know the intrinsecs of pytorch.

In pytorch’s .pyi file you can find torch definitions. One of them is tensor. That is the stub file in which tensor usage is explained.

tensor itself It is defined here:
https://github.com/pytorch/pytorch/blob/7eb9f1788c2e865130db1875d07f5dbbd978e941/torch/init.py#L372

Until then tensor points to the torch/tensor.py file. Afterwards it points to some c++ defined function (maybe, I havent gone deeper).
Don’t get mad with pytorch. It’s complex.

If you want to follow the standard way of importing files look at the definition of Tensor instead of tensor.
It’s a python class you can easily inspect and it’s straight-forward imported.

@JuanFMontesinos
no i am a beginer to python all i am thaught by my instructors is if you want to use a function fn1 present in a file called file.py present in another package pkg1 then use should
import pkg1
pkg1.file.fn1
this is all i know about python i dont know if there is any tutorial explaining how to use classes present in a module (i dont know about importing classes)

what i have come to observe is in pytorch if there is a class cls1 present in file file.py which is present in package pkg1 to use class cls1 we
import pk1
pkg1.cls1
we omit file.py (ie we omit file.py insted of writting pkg1.file.cls1 we write as above)
correct me if i am wrong and please tell what i have written above is true
i wonder why using function requires prefixing file name and using class doesnot

Not true at all, in python you can import all objects inside a file.py
For example

from .pyfile import *

That start means import everything from that file.
you can also import in a parent package (in the __init__) something like:

from pkg1.file import fn1

Then, at usage time, you will be able to do

import pkg1
pkg1.fn1

As pkg1 is aware of what fn1 is since you imported it.

And ofc that will be equivalent to call pkg1.file.fn1 as internally in the init fn1 comes from pig.fime

i would like to know about import only here not from import so to use a class present in another package
import pkg
pkg.cls()
is this true

But you where asking about pytorch… Almost any module will import subpackages or other packages.

BTW that is true if and only if in the __init__.py file of pkg they whether define or import cls.