# Why doesnt Pytorch provide properties for Class names?

**URL:** <https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220>\
**Category:** Uncategorized\
**Created:** [June 6, 2019, 8:05am UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220 "2019-06-06T08:05:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shisho\_Sama](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/32/6926_2.png) [@Shisho\_Sama](https://discuss.pytorch.org/u/Shisho_Sama)\
**Post date:** [June 6, 2019, 8:05am UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/1 "2019-06-06T08:05:17Z")

</div>

Hi everyone.  
I come from a C++/C# background and this has made me thinking for sometime now! Here is my question :  
Since `Pytorch` has a dynamic nature, there will be many situations in which you’d be dealing with different classes on the fly and checking for which classes you are dealing with.

Currently one way to access the class names is through ` __class__ ` and ` __class__. __name__ ` (class attributes) .

My question is, knowing that names with \_\_, mean they are supposed to be **private** and not be accessed by the developer, Why doesn’t `Pytorch` provide a simple property to access them?

Simple things such as :

```auto
for m in model.modules():
    if (m. __class__. __name__ == torch.nn.Linear. __class__. __name__ ):
         ...

```

become very long and ugly needlessly! let alone more complex ones.

I could ask this about the Python langauge, but thought maybe, due to compatibility reasons, they can not do that there, but nothing stops a library to provide its own set of utility methods, properties.  
So What is stopping you from providing such things that would make life easier and codes much more readable.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 6, 2019, 1:14pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/2 "2019-06-06T13:14:03Z")

</div>

Hi,

I think the point is that comparing names should not be used?  
For what you want, `isinstance(m, torch.nn.Linear)` is the pythonic way to do it I think.

---

<div class="post-metadata">

**Author:** ![Shisho\_Sama](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/32/6926_2.png) [@Shisho\_Sama](https://discuss.pytorch.org/u/Shisho_Sama)\
**Post date:** [June 6, 2019, 2:08pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/3 "2019-06-06T14:08:44Z")

</div>

Hi, Thank you very much.  
The example was given in the Udacity Pytorch course and I just gave that as an example.  
But how do you get the type of any class?  
It seems type() only works on tensors, and I couldnt findy any ways to query and get a class type!

---

<div class="post-metadata">

**Author:** ![Shisho\_Sama](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/32/6926_2.png) [@Shisho\_Sama](https://discuss.pytorch.org/u/Shisho_Sama)\
**Post date:** [August 9, 2019, 7:02pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/4 "2019-08-09T19:02:23Z")

</div>

I was actually trying to finetune a `resnet18`. I wanted to set the gradients of all layers to zero except the last linear layer! the `isinstance` simply doesn’t work!  
I wrote :

```auto
resnet18 = models.resnet18(pretrained=True)
resnet18.fc = nn.Linear(512, 10) 
  
for module in resnet18.modules():
    if not isinstance(module, nn.Linear):
        for param in module.parameters():
            param.requires_grad = False

```

What seems to work is to compare the names which is :

```auto
for module in resnet18.modules():
    if module._get_name() != 'Linear':
        print('layer: ',module._get_name())
        for param in module.parameters():
            param.requires_grad_(False)
    elif module._get_name() == 'Linear':
        for param in module.parameters():
            param.requires_grad_(True)

```

something like `nn.Linear. __class__. __name__ ` doesnt have the proper name! it contains `type` as the name which is weird!  
Thats why I’m asking.

**on a side note :**  
Something weird happens, if I omit the second elseif block, the resnet18.fc parameters gradient will be False! while they are initially True, and the first if clause clearly checks for all layers except ‘Linear’.  
I’d like to know why this is happening as well, and if this is a bug!?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 11, 2019, 6:58pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/5 "2019-08-11T18:58:48Z")

</div>

The `isinstance` comparison is working:

```python
resnet18 = models.resnet18(pretrained=True)
resnet18.fc = nn.Linear(512, 10) 

for module in resnet18.modules():
    if isinstance(module, nn.Linear):
        print(module)

> Linear(in_features=512, out_features=10, bias=True)

```

as it returns the only `nn.Linear` layer in the model.

However, note that `modules` will be called recursively on your main model.  
E.g. the first result will be the parent `ResNet` class.  
If you call `module.parameters()` you will get all parameters of the model, including the parameters of the linear layer.

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [August 11, 2019, 11:14pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/6 "2019-08-11T23:14:22Z")

</div>

also ` __name__ ` and ` __class__ ` are special attributes defined by Python (not specific to PyTorch) and they aren’t really private and you can expect they to always be there: [https://docs.python.org/3/reference/datamodel.html](https://docs.python.org/3/reference/datamodel.html)

---

<div class="post-metadata">

**Author:** ![Shisho\_Sama](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/32/6926_2.png) [@Shisho\_Sama](https://discuss.pytorch.org/u/Shisho_Sama)\
**Post date:** [August 12, 2019, 7:01am UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/7 "2019-08-12T07:01:36Z")

</div>

> [@Shisho\_Sama](#):
>
> `nn.Linear. __class__. __name__ `

Thanks, but do you know why `nn.Linear. __class__. __name__ ` doesnt have a proper name in it (it returns `type`? should it not return ‘Linear’?

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [August 12, 2019, 5:05pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/8 "2019-08-12T17:05:55Z")

</div>

Because `obj. __class__ ` accesses the class (type) of the object. Classes are also objects in Python. Most classes are just instances of the `type` metaclass. Hence, if you do `A_Class. __class__ ` or `type(A_Class)` you (often) get the `type` metaclass.

On the other hand, an instance of `nn.Linear`, e.g., `nn.Linear(3, 4)` has class `nn.Linear` so you get `nn.Linear(3, 4). __class__ == nn.Linear` and `type(nn.Linear(3, 4)) == nn.Linear`.

You probably now realize that if you want to get the name of `nn.Linear`, you should instead use `nn.Linear. __name__ ` rather than `nn.Linear. __class__. __name__ ` which returns the name of the metaclass `type`.

---

<div class="post-metadata">

**Author:** ![Shisho\_Sama](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/32/6926_2.png) [@Shisho\_Sama](https://discuss.pytorch.org/u/Shisho_Sama)\
**Post date:** [August 12, 2019, 6:57pm UTC](https://discuss.pytorch.org/t/why-doesnt-pytorch-provide-properties-for-class-names/47220/9 "2019-08-12T18:57:33Z")

</div>

Aha ! that was great! Thanks a gazillion times sir, your explanation was great 🙂
