Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Reshape not working

I am using keras to train a CNN and the base error is dimensionality mismatch.

Reason, after debugging is:

print("Before")
print(TX.shape)
print(TeX.shape)

X_train = TX.reshape(1000, 1, img_rows, img_cols)
X_test = TeX.reshape(430, 1, img_rows, img_cols)
print("After")
print(TX.shape)
print(TeX.shape)

Generating the output:

Using Theano backend.
Using gpu device 0: GeForce GTX 750 Ti (CNMeM is disabled, CuDNN not available)
Before
(1000, 27, 36)
(430, 27, 36)
After
(1000, 27, 36)
(430, 27, 36)

If needed, my model's summary is:

____________________________________________________________________________________________________

Layer (type) Output Shape Param # Connected to

convolution2d_1 (Convolution2D) (None, 32, 25, 34) 320 convolution2d_input_1[0][0]


activation_1 (Activation) (None, 32, 25, 34) 0 convolution2d_1[0][0]


convolution2d_2 (Convolution2D) (None, 32, 23, 32) 9248 activation_1[0][0]


activation_2 (Activation) (None, 32, 23, 32) 0 convolution2d_2[0][0]


convolution2d_3 (Convolution2D) (None, 32, 21, 30) 9248 activation_2[0][0]


activation_3 (Activation) (None, 32, 21, 30) 0 convolution2d_3[0][0]


maxpooling2d_1 (MaxPooling2D) (None, 32, 10, 15) 0 activation_3[0][0]


dropout_1 (Dropout) (None, 32, 10, 15) 0 maxpooling2d_1[0][0]


flatten_1 (Flatten) (None, 4800) 0 dropout_1[0][0]


dense_1 (Dense) (None, 128) 614528 flatten_1[0][0]


activation_4 (Activation) (None, 128) 0 dense_1[0][0]


dropout_2 (Dropout) (None, 128) 0 activation_4[0][0]


dense_2 (Dense) (None, 26) 3354 dropout_2[0][0]


activation_5 (Activation) (None, 26) 0 dense_2[0][0]

Total params: 636698


like image 515
Adorn Avatar asked Sep 03 '26 12:09

Adorn


1 Answers

You are assigning the reshaped arrays to a new variable, but then you are still printing the shape of the old variable:

X_train = TX.reshape(..)

You must use:

print(X_train.shape)
like image 144
Cyb3rFly3r Avatar answered Sep 06 '26 02:09

Cyb3rFly3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!