Coder Social home page Coder Social logo

Question! about cs231n-2017 HOT 2 CLOSED

burton2000 avatar burton2000 commented on August 22, 2024
Question!

from cs231n-2017.

Comments (2)

Burton2000 avatar Burton2000 commented on August 22, 2024

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.

cucrui avatar cucrui commented on August 22, 2024

really useful,thanks bro!

from cs231n-2017.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.