Keras model.fit() taking ages and not showing progress bar

Viewed 476

I'm working on a handwritten digits problem. Hopefully my code is self-explainable enough.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import csv
import numpy as np



with open('train.csv') as f:
    reader = csv.reader(f)
    contents = list(reader)



labels = np.array(contents[0][1:])
labels = np.reshape(labels, (28,28,1))


numbers = []
shape = np.shape(contents)
num_inputs = shape[1]
num_samples = shape[0]
for i in np.arange(1,num_samples):
    pixels = contents[i][1:]
    pixels = np.array(pixels, dtype = int)
    pixels = np.reshape(pixels, (28,28,1))
    #pixels = to_categorical(pixels)
    pixels = pixels / 255
    numbers.append(pixels)

num_hidden_layer = round(2/3 * num_inputs + 10)



model = tf.keras.models.Sequential([
    tf.keras.layers.MaxPooling2D(pool_size=(4,4), input_shape=(28,28,1)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(num_hidden_layer, activation = 'relu'),
    tf.keras.layers.Dense(10, activation = 'softmax')
])


model.summary()



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


model.fit(numbers, labels, epochs=3, verbose = 1)

The shape of numbers is (42000, 28, 28, 1), and the shape of labels is (42000, 28, 28, 1). The flattened input dimensions from my model.summary are only 49, with 31,990 total and trainable params. Despite that, my model.fit() just runs forever, showing absolutely nothing. I've even tried maxpooling further, and nothing. Why isn't it doing anything? I have verbose = 1 and I've yet to see a progress bar. How can I get this to run?

UPDATE:

I just let it run, and it eventually stopped with: ValueError: Layer sequential_1 expects 1 input(s), but it received 42000 input tensors.

2 Answers

I'm pretty sure it's not working because your label values aren't compatible with your model definition. What's in your labels? 28x28 for the labels of the model seems wrong. You're using sparse_categorical_crossentropy which expects a single integer for one example. That single integer would point at the correct lable in your softmax output.

Labels should have only one dimension, right? Must be a array of integers.

Related