1

I have array reshape and sizes issue

I haven't try anything due to the reason I am still new in this and I dont want to mess up things that are unreleated to the issue

import tensorflow as tf
import numpy as np


mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)  # scales data between 0 and 1
x_test = tf.keras.utils.normalize(x_test, axis=1) 

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(32,)))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3)

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss)
print(val_acc)
  File "t1.py", line 17, in <module>
    x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
  File "<__array_function__ internals>", line 6, in reshape
  File "H:\Program Files\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 301, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "H:\Program Files\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 61, in _wrapfunc
    return bound(*args, **kwds)
ValueError: cannot reshape array of size 47040000 into shape (60000,1,28)```
0

1 Answer 1

1

model.add(tf.keras.layers.Flatten(input_shape=(28,28)))

it is an 28x28 image not a 32 vector soo there we know it is not should be a vector of 32 by lefting an argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.