Welcome to my solution for this exercise.
So, for here, I had you calculate the output of our network using matrix multiplication.
So remember, we wanted to use matrix multiplication because it’s more
efficient than doing these two separate operations of the multiplication and the sum.
But to do the matrix multiplication,
we actually needed to change the size of our weights tensor.
So, to do that, just do weights.view 5, 1,
and so this will change the shape of our weights tensor to be five rows and one column.
If you remember, our features has the shape of one row and five columns,
so we can do this matrix multiplication.
So, there’s just one operation that does the multiplication and the sum and just one go,
and then we again add our bias term,
pass it through the activation,
and we get our output.
So, as I mentioned before, you could actually stack up
these simple neural networks into a multi-layer neural network,
and this basically gives
your network greater power to capture patterns and correlations in your data.
Now, instead of a simple vector for our weights,
we actually need to use a matrix.
So, in this case, we have our input vector and our input data x_1, x_2, x_3.
You think of this as a vector of just x, which our features.
Then we have weights that connect our input to one hidden unit in this middle layers,
usually called the hidden layer, hidden units,
and we have two units in this hidden layer.
So then, if we have our features,
our inputs as a row vector,
if we multiply it by this first column,
then we’re going to get the output,
we’re going to get this value of h_1.
Then if we take our features and multiply it by the second column,
then we’re going to get the value for h_2.
So again, mathematically looking at this with matrices and vectors and linear algebra,
we see that to get the values for this hidden layer that
we do a matrix multiplication between our feature vector,
this x_1 to x_n,
and our weight matrix.
Then as before with these values,
we’re going to pass them through
some activation function or maybe not an activation function,
maybe we just want the row output of our network.
So here, I’m generating some random data, some features,
and some random weight matrices and bias terms that you’ll
be using to calculate the output of a multi-layer network.
So, what I’ve built is basically we have three input features,
two hidden units and one output unit.
So, you can see that I’ve listed it here.
So, our features we’re going to create three features and this features vector here,
and then we have an input equals three,
so the shape is this, and two hidden units, one output unit.
So, these weight matrices are created using these values. All right.
I’ll leave it up to you to calculate the output for this multi-layer network.
Again, feel free to use the activation function defined
earlier for the output of your network and the hidden layer. Cheers.
이 연습에 대한 내 솔루션에 오신 것을 환영합니다.
그래서 여기에서는 행렬 곱셈을 사용하여 네트워크의 출력을 계산하도록 했습니다.
따라서 행렬 곱셈을 사용하고 싶었습니다.
곱셈과 합계의 이 두 가지 개별 연산을 수행하는 것보다 효율적입니다.
그러나 행렬 곱셈을 수행하려면
실제로 가중치 텐서의 크기를 변경해야 했습니다.
그렇게 하려면 weights.view 5, 1,
이렇게 하면 가중치 텐서의 모양이 5행 1열로 변경됩니다.
우리의 특징은 1행 5열의 형태를 가지고 있다는 것을 기억한다면,
그래서 우리는 이 행렬 곱셈을 할 수 있습니다.
따라서 곱셈과 합계를 수행하는 단 하나의 연산이 있습니다.
그런 다음 바이어스 항을 다시 추가합니다.
활성화를 통해 전달하고,
그리고 우리는 우리의 출력을 얻습니다.
따라서 앞서 언급했듯이 실제로 쌓을 수 있습니다.
이 간단한 신경망을 다층 신경망으로,
그리고 이것은 기본적으로
데이터의 패턴과 상관 관계를 캡처할 수 있는 네트워크의 능력이 향상됩니다.
이제 가중치에 대한 간단한 벡터 대신
실제로 행렬을 사용해야 합니다.
따라서 이 경우에는 입력 벡터와 입력 데이터 x_1, x_2, x_3이 있습니다.
이것을 우리의 특징인 x의 벡터로 생각하면 됩니다.
그런 다음 이 중간 레이어에 있는 하나의 은닉 유닛에 입력을 연결하는 가중치가 있습니다.
일반적으로 은닉층(hidden layer), 은닉 유닛(hidden unit),
이 은닉층에는 두 개의 단위가 있습니다.
그렇다면 우리의 특징이 있다면,
입력을 행 벡터로,
이 첫 번째 열을 곱하면
그러면 우리는 출력을 얻을 것입니다.
우리는 h_1의 이 값을 얻을 것입니다.
그런 다음 특성을 가져와 두 번째 열로 곱하면
그러면 우리는 h_2에 대한 값을 얻을 것입니다.
다시, 이것을 행렬과 벡터, 선형 대수로 수학적으로 살펴보면,
우리는 이 은닉층에 대한 값을 얻기 위해
우리는 특징 벡터 사이에 행렬 곱을 하고,
이 x_1에서 x_n,
그리고 우리의 가중치 행렬.
그런 다음 이 값을 사용하여 이전과 같이
우리는 그들을 통과 할 것입니다
일부 활성화 기능 또는 활성화 기능이 아닐 수도 있습니다.
아마도 우리는 우리 네트워크의 행 출력을 원할 것입니다.
여기에서 임의의 데이터, 일부 기능,
몇 가지 임의 가중치 행렬 및 편향 항을
다층 네트워크의 출력을 계산하는 데 사용합니다.
그래서 제가 구축한 것은 기본적으로 세 가지 입력 기능이 있다는 것입니다.
2개의 은닉 유닛과 1개의 출력 유닛.
그래서 여기에 나열한 것을 볼 수 있습니다.
그래서 우리의 특징은 세 가지 특징을 만들 것입니다. 그리고 이 특징 벡터는 여기에서,
입력이 3과 같습니다.
모양은 이것이고 두 개의 숨겨진 단위, 하나의 출력 단위입니다.
따라서 이러한 가중치 행렬은 이러한 값을 사용하여 생성됩니다. 괜찮아.
이 다층 네트워크의 출력을 계산하는 것은 여러분에게 맡기겠습니다.
다시 말하지만, 정의된 활성화 함수를 자유롭게 사용하십시오.
네트워크 및 은닉층의 출력에 대해 더 일찍. 건배.
Exercise: Calculate the output for this multi-layer network using the weights W1
& W2
, and the biases, B1
& B2
.
# First, import PyTorch import torch def activation(x): """ Sigmoid activation function Arguments --------- x: torch.Tensor """ return 1/(1+torch.exp(-x)) ### Generate some data torch.manual_seed(7) # Set the random seed so things are predictable # Features are 5 random normal variables features = torch.randn((1, 5)) # True weights for our data, random normal variables again weights = torch.randn_like(features) # and a true bias term bias = torch.randn((1, 1)) ### Solution # Now, make our labels from our data and true weights y = activation(torch.sum(features * weights) + bias) y = activation((features * weights).sum() + bias) ## Solution y = activation(torch.mm(features, weights.view(5,1)) + bias) ### Generate some data torch.manual_seed(7) # Set the random seed so things are predictable # Features are 3 random normal variables features = torch.randn((1, 3)) # Define the size of each layer in our network n_input = features.shape[1] # Number of input units, must match number of input features n_hidden = 2 # Number of hidden units n_output = 1 # Number of output units # Weights for inputs to hidden layer W1 = torch.randn(n_input, n_hidden) # Weights for hidden layer to output layer W2 = torch.randn(n_hidden, n_output) # and bias terms for hidden and output layers B1 = torch.randn((1, n_hidden)) B2 = torch.randn((1, n_output)) ## Your solution here