C2W4 Multi-classifier: reshape issue


Need help in reshaping. New to python numpy

I have this shape : Training images has shape: (27455, 784) and dtype: float64
Need to reshape to : Training images has shape: (27455, 28, 28) and dtype: float64

If i do like this , I am getting error

np.reshape(images, (28,28))

ValueError: cannot reshape array of size 21524720 into shape (28,28)

Hi Kamavaram!
Not sure which coursera course you are referring to, it was not tagged in your post.

I think you are almost there, I tried the following test case:

import numpy as np
tst = np.random.rand(10,9)
tst2 = np.reshape(tst,(10,3,3))

Note that you could also use the shape of the original input in order to make your code more generic like this:

tst2 = np.reshape(tst,(tst.shape[0],int(np.sqrt(tst.shape[1])),int(np.sqrt(tst.shape[1]))))

This will break the flattened vector into 3x3 rows/columns.
The part you missed is stating the first dimension, the number of inputs.

Hope that helps!

Hi Gautam,

 Thanks. i am referring to "DeepLearning.AI TensorFlow Developer Professional Certificate" course.

this assignment is in C2W4. multi-classifier , reading pixels from csv file.

Let me check your solution

Hi Gautam,

looks like it worked. However, while training the model I am getting below error but other steps in the code are showing as per expected output. Can you help here.

1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
—> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False

ValueError: in user code:

File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 890, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 948, in compute_loss
    return self.compiled_loss(
File "/usr/local/lib/python3.8/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 139, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 243, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 1787, in categorical_crossentropy
    return backend.categorical_crossentropy(
File "/usr/local/lib/python3.8/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy

ValueError: Shapes (None, 1) and (None, 26) are incompatible

Your welcome Kamavaram! Let me know how it goes. I am a mentor for GANS, so I don’t have access to the assignment to try it myself.

Looks as though you have another dimension mismatch issue.

ValueError: Shapes (None, 1) and (None, 26) are incompatible

somewhere you are providing the wrong shape vector ( not sure which one is expected (None, 1) or (None,26). You need to find out where this mismatched vector shape occurs and make the one you are providing look the same shape as the one that is expected. Since I do not have access to the assignment, you may need a mentor who has access to the TensorFlow Developer Professional curriculum.

ok Gautam. hope someone will help.

i have created new ticket for the same.

hi @gautamaltman fixed the problem.

I was using categorical crossentropy but using sparse categorical crossentropy did the trick.

1 Like

great news, and thanks for sharing!

Awesome! How does your loss and accuracy look like? My loss is nan and accuracy is below 0.5

This is what I got

Epoch 15/15
858/858 [==============================] - 14s 16ms/step - loss: 0.6291 - accuracy: 0.7888 - val_loss: 0.1513 - val_accuracy: 0.9568

1 Like

Ohh nice! I wonder what I did wrong then, my loss is nan for all the 15 epochs.

Hope you used following and it helps :
[removed - moderator]