Value Error - Assignment 1 Week 1

for i in range(m): # loop over the batch of training examples
a_prev_pad = A_prev_pad[i] # Select ith training example’s padded activation
for h in range(0, n_H, stride): # loop over vertical axis of the output volume
# Find the vertical start and end of the current “slice” (≈2 lines)
vert_start = h * stride
vert_end = vert_start + f

        for w in range(0, n_W, stride):       # loop over horizontal axis of the output volume
            # Find the horizontal start and end of the current "slice" (≈2 lines)
            horiz_start = w * stride
            horiz_end = horiz_start + f
            
            for c in range(0, n_C, stride):   # loop over channels (= #filters) of the output volume
                                    
                # Use the corners to define the (3D) slice of a_prev_pad (See Hint above the cell). (≈1 line)
                a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
                
                # Convolve the (3D) slice with the correct filter W and bias b, to get back one output neuron. (≈3 line)
                weights = W[:,:,:,c]
                biases = b[0,0,0,c]
                Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

Good day, I have tried everything to get this code right. I have completed everything else in the assignment but am unable to pass due to this error. I am unsure what to change. I get “ValueError: operands could not be broadcast together with shapes (3,3,2) (3,3,4)” as an error

Please assist. Thank you

Your slicing for “biases” is wrong. Why are you using [0,0,0,c] there, when you used [:,:,:,c] for the weights?

I think there also may be a problem with the way you’re specifying the range in the two for-loops.
I haven’t tested your method, but I think you don’t need to include stride in the range(…).

for i in range(m): # loop over the batch of training examples
a_prev_pad = A_prev_pad[i] # Select ith training example’s padded activation
for h in range(0, n_H): # loop over vertical axis of the output volume
# Find the vertical start and end of the current “slice” (≈2 lines)
vert_start = h * stride
vert_end = vert_start + f

        for w in range(0, n_W):       # loop over horizontal axis of the output volume
            # Find the horizontal start and end of the current "slice" (≈2 lines)
            horiz_start = w * stride
            horiz_end = horiz_start + f
            
            for c in range(0, n_C):   # loop over channels (= #filters) of the output volume
                                    
                # Use the corners to define the (3D) slice of a_prev_pad (See Hint above the cell). (≈1 line)
                a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
                
                # Convolve the (3D) slice with the correct filter W and bias b, to get back one output neuron. (≈3 line)
                weights = W[:,:,:,c]
                biases = b[:,:,:,c]
                Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

Sorry I have been tinkering with the code to find fault and didn’t realize I left you with the worst version. The problem seems to be related to this section of code - “a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]”

I left the updated version of code above with the mentioned but after playing with variables, it seems the third dimension in a_prev_pad is problematic

*mentioned corrections

That is incorrect. You need to slice A_prev_pad, using [i,:,:,:].

1 Like