python: keras shape mismatch error

  • Last Update :
  • Techknowledgy :

The np_utils.to_categorical function solved this for me;

from keras.utils
import np_utils, generic_utils

y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]

Suggestion : 2

My first problem is that the StatisticsGen takes ~5700MiB of my 6077MiB GPU when I run it through InteractiveContext and doesn’t unload until I restart the notebook. I’m using CUDA V10.1.243 and the problem is independent of the python or tfx version.,The second problem is for an image classifier with TFX. When I run the Evaluator I get a shape mismatch from the metrics, whether I use CategoricalAccuracy, SparseCategoricalAccuracy, or even when I do not use any metric in the eval_config. My guess is that the evaluator is calling keras binary accuracy backend by default but I don’t know why.,For the second question, when you compile your model metric is set to ‘accuracy’, so in eval config it should match it, here is an example,Hi tensorflowers, so after a few trial and errors I found that the GPU memory issue with the execution of the StatisticsGen might actually come from compatibility with 20.04. In docker I do not have these issues if I start my dockerfile with FROM tensorflow/tensorflow:latest-gpu, which I think is running 18.04. Using airflow on my local machine didn’t solve the problem and even made it worse (reboot of the machine every time). Anyway I plan on using Docker from now on but I’d be interested in whether someone has seen a change in GPU usage with tfx on 20.04.

import tensorflow as tf
import numpy as np
import tensorflow_transform as tft

def decode_and_resize(image_raw):
   decoded_img =, channels = 3)
return tf.image.resize(decoded_img, (300, 300))

def process_image(raw_image):
   raw_image = tf.reshape(raw_image, [-1])
img = tf.map_fn(decode_and_resize, raw_image, dtype = tf.float32)
img = tf.image.convert_image_dtype(img, tf.float32)
return tf.reshape(img, [-1, 300, 300, 3])

def preprocessing_fn(inputs):
   outputs = {}

image_raw = inputs['image']

outputs['image_xf'] = process_image(image_raw)
outputs['label_xf'] = tf.reshape(tf.one_hot(inputs['label'], 2), [-1, 2])

return outputs

Suggestion : 3

C++ std::regex_replace to replace different match with different strings,Is there a way to identify what conversational AI engine (ie - dialogfow, lex, watson, etc.) is being used just be conversing with a chatbot?

Here is the error I recieved :

Train on 60000 samples
Epoch 1/100
32/60000 [..............................] - ETA: 1:18
ValueError Traceback (most recent call last)
<ipython-input-123-4ce1d4d047d4> in <module>()
      ----> 1, y_train, batch_size=32, epochs=100, verbose=1)

      25 frames
      /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/ in sparse_softmax_cross_entropy_with_logits(_sentinel, labels, logits, name)
      3391 "should equal the shape of logits except for the last "
      3392 "dimension (received %s)." % (labels_static_shape,
      -> 3393 logits.get_shape()))
      3394 # Check if no reshapes are required.
      3395 if logits.get_shape().ndims == 2:

      ValueError: Shape mismatch: The shape of labels (received (32,)) should equal the shape of logits except
      for the last dimension (received (5408, 10)).

This is my code:

import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets
import fashion_mnist

   (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

x_train = x_train / 255.0
x_test = x_test / 255.0
x_train = np.reshape(x_train, (-1, 28, 28, 1))

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 64, kernel_size = 3, strides = 2, input_shape = [28, 28, 1], activation = 'relu'))
model.add(tf.keras.layers.Dense(units = 10, activation = 'softmax'))

model.compile(optimizer = 'adam',
   loss = 'sparse_categorical_crossentropy',
   metrics = ['sparse_categorical_accuracy']), y_train, batch_size = 32, epochs = 100, verbose = 1)

I only just realized I had forgotten to anycodings_tensorflow add 'flatten' layer to the anycodings_tensorflow model: model.add(tf.keras.layers.Flatten()) anycodings_tensorflow which has resolved the issue.

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 64, kernel_size = 3, strides = 2, input_shape = [28, 28, 1], activation = 'relu'))
model.add(tf.keras.layers.Dense(units = 10, activation = 'softmax'))