Comments (2)
The quick answer is so the variables can broadcast correctly so we can actually add them together. If you were to also add a new axis to Y_squared you then have 2 numpy arrays with different shapes that numpy can not add together.
The longer answer is:
X_squared has shape: (num_test, )
Y_squared has shape: (num_train, )
XY has shape: (num_test, num_train)
We add a new axis to X_squared.
X_squared now has shape (num_test, 1)
Numpy will now let you add X_squared and Y_squared because of its broadcasting rules.
You can think of what happens during broadcasting as 'stretching' X_squared along the axis we just added to make it num_train in length. Numpy will then apply the add operation across all the rows (i.e. Y_squared will be added to each row of the 'stretched' X_squared ).
Instead of adding a new axis to X_squared we could have added a new axis to Y_squared and broadcasting would have allowed this. But, the shape coming out would be (num_train, num_test) which would not add to XY which has shape (num_test, num_train). Because of this we know we must add the new axis to X_squared.
Let me know if that explains it to you, for more info look here:
https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
https://medium.com/dataholiks-distillery/l2-distance-matrix-vectorization-trick-26aa3247ac6c
https://stackoverflow.com/questions/31587802/numpy-add-a-vector-to-matrix-column-wise
from cs231n-2017.
really useful,thanks bro!
from cs231n-2017.
Related Issues (9)
- Adding new image to RNN captioning HOT 1
- Why doesn't it work well when I use vgg19 as the model? HOT 1
- Assignment 3 GAN: Logits/Scores HOT 5
- Assignment3:StyleTransfer-PyTorch
- Are these solutions correct? HOT 2
- A question on rnn_layer HOT 4
- a question about loss in TwoLayerNet HOT 1
- A question on two_layer_net HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cs231n-2017.