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

Chap4 (CNN_working_details.ipynb) -- Sumprod (Device: CPU) X & cnn_w (Device: GPU ("cuda" or "mps")) #74

Open
Camaltra opened this issue Aug 31, 2023 · 0 comments

Comments

@Camaltra
Copy link

In the code

sumprod = torch.zeros((h_im - h_conv + 1, w_im - w_conv + 1))

for i in range(h_im - h_conv + 1):
    for j in range(w_im - w_conv + 1):
        img_subset = X_train[0, 0, i:(i+3), j:(j+3)]
        model_filter = cnn_w.reshape(3,3)
        val = torch.sum(img_subset*model_filter) + cnn_b
        sumprod[i,j] = val

Sumprod is on the CPU device, while X_train and model_filter that come from the cnn_w form the model are both init on the GPU

So when come the assignment, there will have an issues, and the sumprod will look like this

tensor([[val, 0],
              [0, 0]])

To fix it, just specify the device to sumprod

sumprod = torch.zeros((h_im - h_conv + 1, w_im - w_conv + 1)).to(device)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant