how to normalize data in tensorflow

Viewed 1032

I try to create a simple neural network by following this example,however I have a problem when I want to normalize the data

import tensorflow as tf
from tensorflow import keras


fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = test_images / 255.0
test_images = test_images / 255.0

this is my code ,in the last 2 lines ,as you can see, I attempt to normalize the data as shown here , however after I execute this line the variable training_images has only 10,000 samples instead of 60,000 (like it was in the beginning) and now when I try to fit the model (model.fit()) I get an exception

Input arrays should have the same number of samples as target arrays. Found 10000 input samples and 60000 target samples.

And when I try to run this example without the normalization I get a 0.2 accuracy at best (while in the site they have much higher accuracy) where did I go wrong here?

1 Answers

the bug is a simple typo -

train_images = test_images / 255.0
test_images = test_images / 255.0

you divide through test_images both times. change the first division to train_images and it should work

Related