avatarElaine Hilides

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2746

Abstract

n library. This library contains a lot of DNN models. Since the task is a 10 class classification task but the model from the library is based on the ImageNet dataset which is a 1000 class classification task I will be changing the very last fully connected layer to replace the 1000 output nodes to 10.</p><div id="8d8c"><pre>model = torchvision.models.resnet18().to(<span class="hljs-string">'cuda'</span>) model.fc = torch.nn.Linear(in_features=<span class="hljs-number">512</span>, out_features=<span class="hljs-number">10</span>, <span class="hljs-comment"># same number of output units as our number of classes</span> bias=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>)</pre></div><p id="a557">Here, I changed the number of output nodes to 10 based on the layer name. Most of the tutorials or blogs doesn’t say how you get the name of the layer. For different models it is often hard to find. Also what is the number of in_features is another concern. You can use summary from torchinfo to get these information.</p><div id="c45c"><pre>summary(model=model, input_size=(<span class="hljs-number">32</span>, <span class="hljs-number">3</span>, <span class="hljs-number">32</span>, <span class="hljs-number">32</span>), col_names=[<span class="hljs-string">"input_size"</span>, <span class="hljs-string">"output_size"</span>, <span class="hljs-string">"num_params"</span>, <span class="hljs-string">"trainable"</span>], col_width=<span class="hljs-number">20</span>, row_settings=[<span class="hljs-string">"var_names"</span>] )</pre></div><p id="47b0">You need to pass the model and the input size of the dataset that is going to be passed to the model in the summary. This will help generate a random dataset and show you the name of the layers, number of parameters and whether the layers are trainable or not. <i>The image below is for a VGG model not the ResNet-18 model. It was huge for ResNet-18 when printed out like this, so for reference only I am showing you an example of the VGG model.</i></p><figure id="f175"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5lr_YZ5eSAGWcbe0R6lIrg.png"><figcaption>Visualizing the model using summary</figcaption></figure><p id="0d6f">Now that we have our model we just train the model using the CIFAR-10 data. Detailed implementation can be found <a href="https://github.com/aminul-huq/medium">here</a>. After training for 10 epochs we will find out that the testing accuracy is 45.80%. We can visualize how the model learned in each epoch using the loss vs epoch and accuracy vs epoch curve shown below.</p><figure id="4f44"><img src="https://cdn-images-1.readmediu

Options

m.com/v2/resize:fit:800/1*A4j7TEBm4nCS468NxnHTMg.png"><figcaption>Without pretraining.</figcaption></figure><p id="1995">Let’s now experiment with pre-training and freezing some layers. Now I will be changing the model initialization slightly. I will be using the pre-training weights of the ImageNet dataset which is available in pytorch and in order to use it we just need to set ‘pretrained=True’. However, there are other ways to do the same thing.</p><div id="3adc"><pre>model = torchvision.models.resnet18(pretrained=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>) model.fc = torch.nn.Linear(in_features=<span class="hljs-number">512</span>, out_features=<span class="hljs-number">10</span>, bias=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>)</pre></div><div id="3b60"><pre><span class="hljs-keyword">for</span> name, param <span class="hljs-keyword">in</span> model.named_parameters(): <span class="hljs-keyword">if</span> name[<span class="hljs-number">5</span>] < <span class="hljs-string">'2'</span>: param.requires_grad = <span class="hljs-literal">False</span></pre></div><p id="4d2a">In the code snippet above, based on the the name of each of the layers I am actually freezing several initial layers by setting the ‘requires_grad=False’. What it does is that, this makes sure these layers are not trained and preserve the assigned weights. If you pass the model to summary after these lines of code you will find that now the trainable column has several False values which was not the case previously.</p><p id="d3f5">Now if we retrain the model from the start using the same hyper-parameters we will see that after 10 epochs we are getting 62.01% of accuracy on the testing data which is more than 15% than before. We can visualize the loss and accuracy curve of this model below.</p><figure id="882c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aArjzOgvEP6Su6WNEy8Pxw.png"><figcaption>With pre-training and freezing layers</figcaption></figure><p id="d7c1">Based on this graph and the one before we can see the mode with pre-trained weights are doing much better in terms of training and validation data. After 10 epoch the loss value is much lower and the accuracy is much higher.</p><p id="ee64">I hope this blog helped you out in some extend to understand the concept of pre-training and finetuning. Detailed implementation can be found <a href="https://github.com/aminul-huq/medium">here</a>.</p><blockquote id="648f"><p>If you have any difficulty understanding anything or want to reach out to me for any question shoot me an email at [email protected].</p></blockquote></article></body>

Why You Don’t Have to Hide Your Vulnerability

However scared you are

photo by Karsten Winegeart on Unsplash

What is it about feeling vulnerable?

I’ve had many times when I’ve felt vulnerable and exposed. One of the times was sitting on the toilet in a Russian station because the toilet door was from chest height to calf.

You feel very vulnerable trying to have a pee when you can see everyone going past. Logically you know they can’t see you, but you still feel very exposed.

But mostly, I felt vulnerable over the years because I was afraid of the emotional risk of being open. I was scared of being rejected.

Why do people fear being vulnerable?

Vulnerability, like every other emotion, is neutral.

You don’t have to embrace your vulnerability, nor do you have to fear it. It’s just another emotion created by thought.

When you try to avoid vulnerability, you shut down the capacity for feeling emotion. You become a void. A vacuum. And, as we all know, the saying that ‘nature abhors a vacuum’, you’ll do anything you can to fill the void, including drugs, food, alcohol or whatever you think will make you feel better.

Feeling vulnerable isn’t nice, and so some people do their best to avoid the feeling by, as I’ve said, closing themselves off from emotion or trying to numb the feeling. But numbing vulnerability doesn’t just deaden the pain; it can deaden all feelings, including love and joy.

You can’t selectively numb the feelings you want to, just like you can’t selectively lose weight from just one area of your body, “I just want to lose the arm fat,” you might say, “not the boobs,” but you can’t, and when you numb the pain, you numb the possibility of any joy.

Attempting to numb your feelings increases a sense of isolation, and psychological isolation can be terrifying. It can lead to a feeling of hopelessness and desperation which can, naturally, leave you feeling even more vulnerable.

But what is vulnerability?

What makes people feel vulnerable? The feeling stems from fear; it’s a fear of being hurt and being attacked or harmed somehow.

If you’re in a potentially violent or unstable environment and you feel as if you’re vulnerable to being attacked or harmed, I encourage you to get out as quickly as you can.

But, if the feeling of vulnerability comes from the fear of being emotionally hurt or potentially verbally attacked, it’s only the thought of what might happen if you open yourself up to life that’s hurting you right now.

Feeling vulnerable

Some people use humour to deflect feeling vulnerable, especially if they’ve been bullied. The comedian and actor, Lenny Henry, said that he used to get bullied at school, and comedy became his weapon to deal with it.

Humour is a wonderful asset, but sometimes people use humour as a shield to hide behind because they’re frightened of showing others what they consider to be their authentic self, to show their vulnerability.

But, if you see that when you’re being humorous and showing up as a more confident person, at that moment, you aren’t feeling vulnerable. You’ve let go of the vulnerable thoughts to have a fresh thought about being funny.

You could feel vulnerable in a relationship if you feel that the other person might leave you or stop loving you, but when you see that your own thoughts generate fear, you can let go of the feeling and be present to the relationship.

If you feel that your partner wants to leave, you’ll be ultra alert to any signals, and this will feed your vulnerability. Remember, your happiness or security can’t come from someone else but only from your thoughts about the other person and what you imagine they’re thinking and feeling.

You might feel vulnerable working in a company that is making cuts, but, again, the feeling is coming from what you imagine this means for you. You’re probably thinking about the consequences of not having a job or imagining that you won’t ever be able to get another job, so the feeling isn’t coming from your position but your head.

And, if you have a head full of thoughts about being made redundant, you don’t have your mind on the job you’re doing, which will make you feel less confident about holding your position.

There are occasions when recognising vulnerability is necessary; if you have a compromised immune system, for example, and fear that you’re likely to pick up an infection, you don’t want to put yourself in a situation where you be vulnerable to germs.

But again, it’s important to be vigilant, but fearful thoughts on the subject are not compulsory. I had a client in her mid-thirties who felt highly vulnerable to serious illness. She hadn’t had more than a dose of flu in her life, but she lived on high alert, constantly scanning her body for signs of disease and panicking if anyone she knew was diagnosed with cancer or anything that she considered life-threatening.

When she saw that she was a healthy young woman and the vulnerability was created via thought, she was able to relax; as she relaxed, she let go of the stress that she was experiencing and consequently felt much better, and she could see that she was no more vulnerable to disease than anyone else.

Feeling vulnerable isn’t saying anything about who you are; it doesn’t mean you’re a scaredy-cat or a quivering jelly; it’s just how you feel right now.

And vulnerability is part of your emotional package; it isn’t good or bad until you think it is.

“Vulnerability is not knowing victory or defeat, it’s understanding the necessity of both; it’s engaging. It’s being all in.” Brene Brown

Life
Vulnerability
Self Development
Psychology
Mental Health
Recommended from ReadMedium