ValueError in Tensorflow while using the subclassing API

Viewed 40

I'm new into tensorflow and I am trying to learn how to use the subclassing API on a batch of data coming from fashion MNIST. But I keep getting the same shape error over and over again and I can't find where it's coming from. Here is my code :


import tensorflow as tf
from tensorflow import keras

import os
import sys
assert sys.version_info >= (3, 5)
import sklearn
assert sklearn.__version__ >= "0.20"

import numpy as np
import matplotlib

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.
#Note that I am not using this part of the code for now because I
#need to debug it first:

#class Block(keras.layers.Layer):
#    def __init__(self, n_layers, n_neurons, **kwargs):
#        super().__init__(**kwargs)
#        self.hidden = [keras.layers.Dense(n_neurons, #activation="relu")
#                       for _ in range(n_layers)]

#    def call(self, inputs):
#        Z = inputs
#        for layer in self.hidden:
#            Z = layer(Z)
#        return inputs + Z
class Modeling(keras.Model):
  def __init__(self, **kwargs):
      super().__init__(**kwargs)
      self.Flatten = keras.layers.Flatten(input_shape=[28,28])
      self.dense1 = keras.layers.Dense(30, activation="relu")
      self.out = keras.layers.Dense(10, activation="softmax")

  def call(self, inputs):
    Z = self.dense1(self.Flatten(inputs))
#    for _ in range (1 + 3):
#      Z = self.block1(Z)
#    Z = self.block2(Z)
    return self.out(Z)
#This one shows that there is multiple output_shapes : 
model = Modeling()
model.build([28,28])
model.summary()
model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

The message I keep getting is, in short :

ValueError
Traceback 1 model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy']) 2 ----> 3 model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

    ValueError: Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 28, but received input with shape (None, 784)


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 28, 28), dtype=float32)

Any help and/or explanations on these input shapes would be greatly appreciated. Thank you all!

1 Answers

The error message itself tells you that your input is not in the shape as your defined model expects it. So the dense layer is expecting input data with a certain number of features but got data that differs.

I am not sure if it is important for you to use the structure you used here:

class Modeling(keras.Model):
  def __init__(self, **kwargs):
      super().__init__(**kwargs)
      self.Flatten = keras.layers.Flatten(input_shape=[28,28])
      self.dense1 = keras.layers.Dense(30, activation="relu")
      self.out = keras.layers.Dense(10, activation="softmax")

  def call(self, inputs):
    Z = self.dense1(self.Flatten(inputs))
#    for _ in range (1 + 3):
#      Z = self.block1(Z)
#    Z = self.block2(Z)
    return self.out(Z)

When I replace this with the following:

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

It works.

Full working code:

import tensorflow as tf
from tensorflow import keras

import os
import sys


import numpy as np
import matplotlib

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

Gives me loss: 0.8309 - acc: 0.7282 - val_loss: 0.5908 - val_acc: 0.7994

Related