Search code examples
tensorflowmnist

Add a row to mnist images dataset


I have to add a row of ones to mnist images dataset, which is batched to 32 samples. Here the code:

(mnist_images, mnist_labels), _ = tf.keras.datasets.mnist.load_data()
dataset = tf.data.Dataset.from_tensor_slices(
(tf.cast(mnist_images[...,tf.newaxis]/255, tf.float32),
 tf.cast(mnist_labels,tf.int64)))
 dataset = dataset.shuffle(1000).batch(32)

for images,labels in dataset.take(1):
   print("Logits: ", mnist_model(images[0:1]).numpy())

b =tf.reshape(images, [784,32], tf.float32)
c = tf.concat(b,tf.ones([1,32], tf.float32),0)

I get the following error, but both are dtype float 32,

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor: 
shape=(1, 32), dtype=float32, numpy= array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,1., 1., 1., 1.,
    1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]],
  dtype=float32)>

Is there another way to add a row into images tensor?


Solution

  • It seems like you've just forgotten to use brackets - [ ] .

    use:

    c = tf.concat([b,tf.ones([1,32], tf.float32)],0)
    

    instead of :

    c = tf.concat(b,tf.ones([1,32], tf.float32),0)