1 – PyTorch V2 Part 1 V1

Hello everyone and welcome to this lesson on deep learning with PyTorch. So, in this lesson I’m going to be showing you how we can build neural networks with pyTorch and train them. By working through all these notebooks I built, you’ll be writing the actual code yourself for building these networks. By the end of the lesson, you will have built your own state of the art image classifier. But first we’re going to start with basics, so how do you build just a simple neural network in pyTorch? So, as a reminder of how neural networks work, in general we have some input values so here x1, x2, and we multiply them by some weights w and bias. So, this b is this bias we just multiply it by one then you sum all these up and you get some value h. Then we have what’s called an activation function. So, here f of h and passing these input values h through this activation function gets you output y. This is the basis of neural networks. You have these inputs, you multiply it by some waves, take the sum, pass it through some activation function and you get an output. You can stack these up so that the output of these units, of these neurons go to another layer like another set of weights. So, mathematically this what it looks like, y, our output is equal to this linear combination of the weights and the input values w’s and x’s plus your bias value b passes through your activation function f and you get y. You could also write it with this sum. So, sum of wi times xi and plus b, your bias term. That gives you y. So, what’s nice about this is that you can actually think of the x’s, your input features, your values, as a vector, and your weights as another vector. So, your multiplication and sum is the same as a dot or inner product of two vectors. So, if you consider your input as a vector and your weights as a vector, if you take the dot product of these two, then you get your value h and then you pass h through your activation function and that gets you your output y. So, now if we start thinking of our weights and our input values as vectors, so vectors are an instance of a tensor. So, a tensor is just a generalization of vectors and matrices. So, when you have these like regular structured arrangements of values and so a tensor with only one dimension is a vector. So, we just have this single one-dimensional array of values. So, in this case characters T-E-N-S-O-R. A matrix like this is a two-dimensional tensor and so we have values going in two directions from left to right and from top to bottom and so that we have individual rows and columns. So, you can do operations across the columns like along a row or you can do it across the rows like going down a column. You also have three-dimensional tensors so you can think of an image like an RGB color image as a three-dimensional tensor. So, for every pixel, there’s some value for all the red and the green and the blue channels and so for every individual pixel, in a two-dimensional image, you have three values. So, that is a three-dimensional tensor. Like I said before, tensors are a generalization of this so you can actually have four-dimensional, five-dimensional, six-dimensional, and so on like tensors. It’s just the ones that we normally work with are one and two-dimensional, three-dimensional tensors. So, these tensors are the base data structure that you use an pyTorch and other neural network frameworks. So, TensorFlow is named after tensors. So, these are the base data structures that you’ll be using so you pretty much need to understand them really well to be able to use pretty much any framework that you’ll be using for deep learning. So, let’s get started. I’m going to show you how to actually create some tensors and use them to build a simple neural network. So, first we’re going to import pyTorch and so just import torch here. Here I am creating activation function, so this is the Sigmoid activation function. It’s the nice s shape that kind of squeezes the input values between zero and one. It’s really useful for providing a probability. So, probabilities are these values that can only be between zero and one. So, you’re Sigmoid activation if you want the output of your neural network to be a probability, then the sigmoid activation is what you want to use. So, here I’m going to create some fake data, I’m generating some data, I’m generating some weights and biases and with these you’re actually going to do the computations to get the output of a simple neural network. So, here I’m just creating a manual seeds. So, I’m setting the seed for the random number generation that I’ll be using and here I’m creating features. So, features are like the input features of the input data for your network. Here we see torch.randn. So, randn is going to create a tensor of normal variables. So, random normal variables as samples from a normal distribution. You give it a tuple of the size that you want. So, in this case I want the features to be a matrix, a 2-dimensional tensor of one row and five columns. So, you can think of this as a row vector that has five elements. For the weights, we’re going to create another matrix of random normal variables and this time I’m using randn_like. So, what this does is it takes another tensor and it looks at the shape of this tensor and then it creates it, it creates another tensor with the same shape. So, that’s what this this like means. So, I’m going to create a tensor of random normal variables with the same shape as features. So, it gives me my weights. Then I’m going to create a bias term. So, this is again just a random normal variable. Now I’m just creating one value. So, this is one row and one column. Here I’m going to leave this exercise up to you. So, what you’re going to be doing is taking the features, weights, and the bias tensors and you’re going to calculate the output of this simple neural network. So, remember with features and weights you want to take the inner product or you want to multiply the features by the weights and sum them up and then add the bias and then pass it through the activation function and from that you should get the output of your network. So, if you want to see how I did this, checkout my solution notebook or watch the next video which I’ll show you my solution for this exercise.

%d 블로거가 이것을 좋아합니다: