C4W1 Exercise 7 - distribute_value

may i ask how to use np.ones to output other value other than 1?

distributed value = [[1. 1.]
[1. 1.]]

AssertionError Traceback (most recent call last)
5 assert type(a) == np.ndarray, “Output must be a np.ndarray”
6 assert a.shape == (2, 2), f"Wrong shape {a.shape} != (2, 2)"
----> 7 assert np.sum(a) == 2, “Values must sum to 2”
9 a = distribute_value(100, (10, 10))

AssertionError: Values must sum to 2

I guess you used np.ones() correctly, since it is a matter of dimensions. The problem is to calculate “average” which will be distributed to the matrix created by np.ones, or, you just forgot to multiply with it.

hi Nobu, i use a = np.ones((n_H,n_W))*average
in function distribute_value()
and it passed the test,
i have no idea why this didn’t function when i use it in Exercise 8 - pool_backward with
da = distribute_value(dA[i, vert_start: vert_end, horiz_start: horiz_end, c],(h,w))

could you pls tell where i made the mistake ? thanks

distribute_values() function is only used to calculate “dA_prev[…]” if mode is “average”, not “da”, which is a simple slice of dA.

hi Nobu,
i did change to dA_prev but error still the same.

(5, 4, 2, 2)
(5, 5, 3, 2)
mode = max
mean of dA = 0.14571390272918056
dA_prev1[1,1] = [[ 0. 0. ]
[ 5.05844394 -1.68282702]
[ 0. 0. ]]

ValueError Traceback (most recent call last)
12 print('dA_prev1[1,1] = ', dA_prev1[1, 1])
13 print()
—> 14 dA_prev2 = pool_backward(dA, cache, mode = “average”)
15 print(“mode = average”)
16 print('mean of dA = ', np.mean(dA))

in pool_backward(dA, cache, mode)
57 # Get the value da from dA (≈1 line)
—> 58 da = distribute_value(dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c],(h,w))
60 # Define the shape of the filter as fxf (≈1 line)

in distribute_value(dz, shape)
19 # Create a matrix where every entry is the “average” value (≈1 line)
—> 20 a = np.ones((n_H,n_W))*average

ValueError: operands could not be broadcast together with shapes (0,0) (2,2)

may i ask problem still from distribute_value ?

Looks like you passed “max” case. That’s really good.

Again, “da” is just a slice of dA, similar to what you did for “a_prev_slice” for “max” case. You used distributed_value() at a wrong place, which causes passing wrong parameters to distributed_value(). So, at first, please relook into what “da” is.

sorry i didn’t get you,

da = distribute_value(dA_prev[i,vert_start:vert_end,horiz_start:horiz_end,c],(h,w))
is wrong

da = distribute_value(dA[i,vert_start:vert_end,horiz_start:horiz_end,c],(h,w))
also wrong,

may i ask what should i pass to ?

You do not use distributed_value() for calculating da. Again this is just a slice of dA. So, code should be
da = dA[…]
And, please be careful about the shape of dA. dA is not sliced yet. So, you may want to specify values for 4 dimensions. (looks like this portion is OK)

Please do not post your code. :slight_smile: The answer is yes. In the next line, you prepare the shape. Then, distribute_value() is the last thing to do.