Classes and object in pytorch

username: km_proton
cooked: <p>torch.tensor()<br>
torch.utils.data.dataloader()<br>
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<br>
is torch a package and tensor() is a function present inside a file <strong>init</strong> so that we write torch.tensor() insted of torch.<strong>init</strong>.tensor()</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
In a python package you can import objets in <code>__init__</code>. Those objects can be directly called as you aforementioned.</p>
<p>So if “tensor” exist in a file called torch.core.tensor (for example) if you write in torch’s <code>__init__</code></p>
<pre><code class="lang-auto">from torch.core.tensor import tensor
</code></pre>
<p>you will be able to call <code>torch.tensor</code></p>

username: km_proton
cooked: <p>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</p>

username: km_proton
cooked: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> sorry if i am disturbing you .  i would like to ask you one question<br>
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</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
Pytorch is a complex library (maybe not the best one to learn basics)<br>
I’m not developer and I don’t know the intrinsecs of pytorch.</p>
<p>In pytorch’s <code>.pyi</code> file you can find torch definitions. One of them is tensor. That is the stub file in which tensor usage is explained.</p>
<p>tensor itself It is defined here:<br>
<a>https://github.com/pytorch/pytorch/blob/7eb9f1788c2e865130db1875d07f5dbbd978e941/torch/<strong>init</strong>.py#L372</a></p>
<p>Until then tensor points to the torch/tensor.py file. Afterwards it points to some c++ defined function (maybe, I havent gone deeper).<br>
Don’t get mad with pytorch. It’s complex.</p>
<p>If you want to follow the standard way of importing files look at the definition of Tensor instead of tensor.<br>
It’s a python class you can easily inspect and it’s straight-forward imported.</p>

username: km_proton
cooked: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a><br>
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<br>
import pkg1<br>
pkg1.file.fn1<br>
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)</p>
<p>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<br>
import pk1<br>
pkg1.cls1<br>
we omit file.py (ie we omit file.py insted of writting pkg1.file.cls1 we write as above)<br>
correct me if i am wrong and please tell what i have written above is true<br>
i wonder why using function requires prefixing file name and using class doesnot</p>

username: JuanFMontesinos
cooked: <p>Not true at all, in python you can import all objects inside a file.py<br>
For example</p>
<pre><code class="lang-auto">from .pyfile import *
</code></pre>
<p>That start means import everything from that file.<br>
you can also import in a parent package (in the <code>__init__</code>) something like:</p>
<pre><code class="lang-auto">from pkg1.file import fn1
</code></pre>
<p>Then, at usage time, you will be able to do</p>
<pre><code class="lang-auto">import pkg1
pkg1.fn1
</code></pre>
<p>As pkg1 is aware of what fn1 is since you imported it.</p>

username: JuanFMontesinos
cooked: <p>And ofc that will be equivalent to call pkg1.file.fn1 as internally in the init fn1 comes from pig.fime</p>

username: km_proton
cooked: <p>i would like to know about import only here not from import so to use a class present in another package<br>
import pkg<br>
pkg.cls()<br>
is this true</p>

username: JuanFMontesinos
cooked: <p>But you where asking about pytorch… Almost any module will import subpackages or other packages.</p>
<p>BTW  that is true if and only if in the <code>__init__.py</code> file of pkg they whether define or import cls.</p>