Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment1 k_nearest_neighbor.py memory overflow #3

Open
nhzc123 opened this issue Jan 17, 2017 · 2 comments
Open

assignment1 k_nearest_neighbor.py memory overflow #3

nhzc123 opened this issue Jan 17, 2017 · 2 comments

Comments

@nhzc123
Copy link

nhzc123 commented Jan 17, 2017

in function compute_distances_no_loops(self, X)

T = np.sum(X**2,axis = 1)
F = np.sum(self.X_train**2,axis = 1).T
F = np.tile(F,(500,5000))
FT = X.dot(self.X_train.T)
print T.shape,F.shape,FT.shape,X.shape,self.X_train.shape
dists = T+F-2*FT`

the code

F = np.tile(F,(500,5000))

will make a matrix which contain 500 * 5000 * 5000 elements
may be the code should be modified like

T = np.reshape(np.sum(X**2,axis = 1),(num_test,1))
F = np.sum(self.X_train**2,axis = 1).T
F = np.tile(F,(num_test,1))
FT = X.dot(self.X_train.T)
print T.shape,F.shape,FT.shape,X.shape,self.X_train.shape
dists = T+F-2*FT`
@nhzc123 nhzc123 closed this as completed Jan 17, 2017
@nhzc123 nhzc123 reopened this Jan 17, 2017
@ChienDuong
Copy link

ChienDuong commented May 18, 2017

This is another way, we dont need to use np.tile. Just remove the line, and the code still workable.
#F=np.tile(F,(500,5000))
In the line: dists = T+F-2*FT (when we plus F, it automatically plus F's element through all 500 rows)

@wzirui
Copy link

wzirui commented Sep 1, 2017

The original code is wrong:

    T = np.sum(X**2,axis = 1)
    F = np.sum(self.X_train**2,axis = 1).T
    F = np.tile(F,(500,5000))
    FT = X.dot(self.X_train.T)
    print T.shape,F.shape,FT.shape,X.shape,self.X_train.shape
    dists = T+F-2*FT

But only remove the line F = np.tile(F,(500,5000)) can't fix the problem:

    T = np.sum(X**2,axis = 1)
    F = np.sum(self.X_train**2,axis = 1).T
    FT = X.dot(self.X_train.T)
    print T.shape,F.shape,FT.shape,X.shape,self.X_train.shape
    dists = T+F-2*FT

What we need to do is to transpose T but not F, so that broadcasting can work properly. Also, transposing a vector doesn't change anything. Instead we need to reshape the vector to a matrix:

    T = np.reshape(np.sum(X**2,axis = 1), (num_test,1))
    F = np.sum(self.X_train**2,axis = 1)
    FT = X.dot(self.X_train.T)
    print T.shape,F.shape,FT.shape,X.shape,self.X_train.shape
    dists = T+F-2*FT

Repository owner deleted a comment Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@nhzc123 @wzirui @ChienDuong and others