r/MLQuestions • • 16d ago

Beginner question 👶 trying to make CNN from scratch but i cannot understand this specefic part

from PIL import Image
import matplotlib.pyplot as plt
from datasets import load_dataset
import numpy as np


# load the image
# image = Image.open("cat.jpg")
def preprocess_image(image):
    image = image.convert("RGB")
    image = image.resize((128, 128))
    image = np.array(image).astype(np.float32) / 255.0
    return image


# Load dataset in streaming mode
dataset = load_dataset(
    "microsoft/cats_vs_dogs",
    split="train",
    streaming=True
)


images = []
labels = []


for sample in dataset.take(32):
  images.append(preprocess_image(sample["image"]))
  labels.append(sample["labels"])


X = np.array(images) #shape (1,32)
y = np.array(labels) #shape (1,32)


#fliters
filters = np.random.randn(64, 27) * 0.05
filters_2 = np.random.randn(32, 576) * 0.05
filters_3 = np.random.randn(16, 288) * 0.05


b = 0.0
stride = 1
pad = 0
all_feature_maps = []


for image in X[:1]:
  #1st layer
  feature_maps_1 = []
  feature_maps_2 = []
  feature_maps_3 = []
  feature_maps_3_grids = []


  for w in filters:
    feature_map = np.zeros((126, 126))


    for i in range(126):
        for j in range(126):
            a = image[i:i+3, j:j+3].flatten()
            z = np.dot(w, a) + b
            feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_1.append(feature_map)


  feature_maps_1 = np.array(feature_maps_1)


  #2nd layer
  for w in filters_2:
    feature_map = np.zeros((124, 124))
    for i in range(124):
      for j in range(124):
        a = feature_maps_1[:,i:i+3, j:j+3].flatten()
        z = np.dot(w, a) + b
        feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_2.append(feature_map)


  feature_maps_2 = np.array(feature_maps_2)


  #3rd layer
  for w in filters_3:
    feature_map = np.zeros((122, 122))
    for i in range(122):
      for j in range(122):
        a = feature_maps_2[:,i:i+3, j:j+3].flatten()
        z = np.dot(w, a) + b
        feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_3_grids.append(feature_map)
    feature_map = np.mean(feature_map) #global average pooling
    feature_maps_3.append(feature_map)
  all_feature_maps.append(feature_maps_3)


all_feature_maps = np.array(all_feature_maps)
feature_maps_3_grids = np.array(feature_maps_3_grids)


dense_weights = np.random.randn(16,2)* 0.01
dense_bias = np.zeros(2)


logits = all_feature_maps @ dense_weights + dense_bias
exp_values = np.exp(logits)
p = exp_values / np.sum(exp_values,axis=1,keepdims=True)
loss = -np.log(p[0, y[0]])
print(loss)
print(p)
d_logits = p.copy()
d_logits[0, y[0]] -= 1
print(d_logits)


grad_d_weights = all_feature_maps.T @ d_logits
grad_d_bias = np.sum(d_logits, axis=0)
print(grad_d_weights)
print(grad_d_bias)from PIL import Image
import matplotlib.pyplot as plt
from datasets import load_dataset
import numpy as np


# load the image
# image = Image.open("cat.jpg")
def preprocess_image(image):
    image = image.convert("RGB")
    image = image.resize((128, 128))
    image = np.array(image).astype(np.float32) / 255.0
    return image


# Load dataset in streaming mode
dataset = load_dataset(
    "microsoft/cats_vs_dogs",
    split="train",
    streaming=True
)


images = []
labels = []


for sample in dataset.take(32):
  images.append(preprocess_image(sample["image"]))
  labels.append(sample["labels"])


X = np.array(images) #shape (1,32)
y = np.array(labels) #shape (1,32)


#fliters
filters = np.random.randn(64, 27) * 0.05
filters_2 = np.random.randn(32, 576) * 0.05
filters_3 = np.random.randn(16, 288) * 0.05


b = 0.0
stride = 1
pad = 0
all_feature_maps = []


for image in X[:1]:
  #1st layer
  feature_maps_1 = []
  feature_maps_2 = []
  feature_maps_3 = []
  feature_maps_3_grids = []


  for w in filters:
    feature_map = np.zeros((126, 126))


    for i in range(126):
        for j in range(126):
            a = image[i:i+3, j:j+3].flatten()
            z = np.dot(w, a) + b
            feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_1.append(feature_map)


  feature_maps_1 = np.array(feature_maps_1)


  #2nd layer
  for w in filters_2:
    feature_map = np.zeros((124, 124))
    for i in range(124):
      for j in range(124):
        a = feature_maps_1[:,i:i+3, j:j+3].flatten()
        z = np.dot(w, a) + b
        feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_2.append(feature_map)


  feature_maps_2 = np.array(feature_maps_2)


  #3rd layer
  for w in filters_3:
    feature_map = np.zeros((122, 122))
    for i in range(122):
      for j in range(122):
        a = feature_maps_2[:,i:i+3, j:j+3].flatten()
        z = np.dot(w, a) + b
        feature_map[i, j] = z
    feature_map = np.maximum(feature_map, 0) #ReLU
    feature_maps_3_grids.append(feature_map)
    feature_map = np.mean(feature_map) #global average pooling
    feature_maps_3.append(feature_map)
  all_feature_maps.append(feature_maps_3)


all_feature_maps = np.array(all_feature_maps)
feature_maps_3_grids = np.array(feature_maps_3_grids)


dense_weights = np.random.randn(16,2)* 0.01
dense_bias = np.zeros(2)


logits = all_feature_maps @ dense_weights + dense_bias
exp_values = np.exp(logits)
p = exp_values / np.sum(exp_values,axis=1,keepdims=True)
loss = -np.log(p[0, y[0]])
print(loss)
print(p)
d_logits = p.copy()
d_logits[0, y[0]] -= 1
print(d_logits)


grad_d_weights = all_feature_maps.T @ d_logits
grad_d_bias = np.sum(d_logits, axis=0)
print(grad_d_weights)
print(grad_d_bias)

so i don't understand

- why is d_logits even needed and what is it?

- why does multiplying d_logits the transpose of 16 pooled features gives us the gradient for dense weights

- why is grad of bias just sum of d_logits?

( i tried asking LLMs but they give too complex answers or start giving code which i don't want and cannot understand)

and most importantly what do i do next and how to move further and backprop on CNN?

any additional tips will be appreciated!

12 Upvotes

9 comments sorted by

2

u/T1lted4lif3 16d ago

Your first two questions come from the loss function not the cnn, have you worked through an MLP yet?

I recommend drawing out the partial derivatives by hand on paper first, will answer your question for you.

1

u/boldbug-dev 16d ago

i have worked through an MLP and it was more simpler, you take partial derivative of loss with respect to that neuron i think and use that later to adjust the weights

1

u/T1lted4lif3 16d ago

yes, so you should have had the d_logits as well then, because re-iterating, that derivative has nothing to do with the layers, it's with respect to the loss function.

So it means you need to either go back or you missed something

1

u/boldbug-dev 16d ago

maybe because i dont watch tutorials and just try to raw dog it and learn on the way so nobody told me about it but the MLP was working and i prefer to use simpler names and after some times i also understand d_logits now. logits is the score and d_logits is the blame on the score like if one score goes down and then loss goes down too kind of that like relation

1

u/[deleted] 16d ago

[removed] — view removed comment

1

u/boldbug-dev 16d ago

Thanks for looking, but I checked both points:

  1. labels is appended once per sample, so y ends up as shape (32,), and I only use y[0] (a single int) in the loss. No mismatch there.
  2. Layer 2 takes 64 input channels with a 3×3 window, so each filter needs 64×3×3 = 576 weights. filters_2 is (32, 576), which matches. The same holds for layer 3: 32×3×3 = 288.

If you saw an actual error, can you paste the traceback and the line number? Then I can look at the real cause.

1

u/[deleted] 16d ago

[removed] — view removed comment

1

u/boldbug-dev 16d ago

no it works but i dont know the whys