Modules in pytorch

my question is if we consider torch as .py file then contadicts to second statement that if utils is a class then dataloader is function then what is data . If we consider torch as package then functions cannot be inside a package (functions can only be inside a .py file) it contradicts to first statement . i have been reserching about this in many sites and forums but i was not able to find answer . could you please tell me the solution along with what happens when you import torch (what happens when you import torch like where is it being placed). is tensor a function present in init please explain the working when you import pytorch to the modules and files present inside pytorch(along with answer to this question)

You can go to the official github page and can get a clear answer. Actually torch is the main package, then utils is a subpackage under it, data is subpackage under utils and then dataloader is finally the class.

You should note that a package can have subpackages as well as classes , so tensor is a class under torch and utils is a package under the same package

@chetan06 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 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

I think tensor1() is enough. as
When you write

from import DataLoader

then you write the class Dataloader in file within the package

I think I have made myself clear, if not feel free to ask

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 present in another package pkg1 then use should
import pkg1
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 which is present in package pkg1 to use class cls1 we
import pk1
we omit (ie we omit 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

Actually I have used other language and the same is followed there too so I thought that way. But later when I saw the torch github page , that also confirmed that file_name should be omitted. In case the confusion persists you can once try it yourself

1 Like

i have never imported a class before if we import a function it returns a value (like int,bool,string,or float)
but what happens when you call a class does it returns any value ?

You do not use a class…class is a blueprint…you make object out of that class and use that. I recommend you to once go through the object oriented programming concept.

we use torch.tensor() right so tensor() is a class right so what happens when you call tensor() is it like calling a function like fn() . a function returns a value like bool,int,float,string ect but what does class return (a class has subfunction inside and attributes inside without specifically calling a method or attribute how can it return a value) . i am asking this because i couldnot find any tutorial usefull like i have watched oops tutorials of techwithtim,edureka,moshhamidani,coreschafer,sendex,telusko ect but no explains the concept that classes can be called . moreover i cannot afford to study in universities thats why i am asking here

Class is a user defined datatype. Likewise the primitive datatypes like int, float, etc. it is also a datatype but user defined one. So I ask you a question: What does int return?? Think…Absolutely nothing actually we use int to define the objects.
int a; // this is followed in many languages like C++, java, etc.
And with int’s object we can perform a lot many operation.
In the same way, class is a datatype which without its meaning has no significance, it is just the blueprint on which its objects are made and they acquire behaviour. So class returns nothing. They are just to make objects.

For example, we make a class student. Student is a general name. Now its object will have specific names, ids but almost same functions…that is they study, etc.

I think you have read object oriented concepts in python that’s why its difficult for you to get it. I read it in java and C++ so don’t panic just go slow and feel free to ask again.

yes i can get your point but actually if we call fn() it executes that fn and the fn returns the value and the value is the value of that fn likewise what i am asking is if i call a fn what it would return that would be stored as the value of that class (you may tell me the fn inside it but if there are two fns)
no my question was actually there are two function inside tensor class def deepcopy(self, memo): and def reduce_ex(self, proto):
so if i call a class like torch.tensor() what does it returns does it returns first one or second one