Skip to content Skip to sidebar Skip to footer

How To Reshape Batchdataset Class Tensor?

Im unable to reshape tensor loaded from my own custom dataset. As shown below ds_train has batch size of 8 and I want to reshape it such as: len(ds_train),128*128. So that I can fe

Solution 1:

Try changing the shape inside the neural net:

inputs = keras.Input(shape=(128, 128, 1))
flat = keras.layers.Flatten()(inputs)

This would work:

import numpy as np
import tensorflow as tf

x = np.random.rand(10, 128, 128, 1).astype(np.float32)

inputs = tf.keras.Input(shape=(128, 128, 1))
flat = tf.keras.layers.Flatten()(inputs)
encode = tf.keras.layers.Dense(14, activation='relu', name='encode')(flat)
coded =  tf.keras.layers.Dense(3, activation='relu', name='coded')(encode)
decode = tf.keras.layers.Dense(14, activation='relu', name='decode')(coded)
decoded =tf.keras.layers.Dense(128*128, activation='sigmoid', name='decoded')(decode)

model = tf.keras.Model(inputs=inputs, outputs=decoded)

model.build(input_shape=x.shape)  # remove this, it's just for demonstrating

model(x)  # remove this, it's just for demonstrating
<tf.Tensor: shape=(10, 16384), dtype=float32, numpy=
array([[0.50187236, 0.4986383 , 0.50084716, ..., 0.4998364 , 0.50000435,
        0.4999416 ],
       [0.5020216 , 0.4985297 , 0.5009147 , ..., 0.4998234 , 0.5000047 ,
        0.49993694],
       [0.50179213, 0.49869663, 0.50081086, ..., 0.49984342, 0.5000042 ,
        0.4999441 ],
       ...,
       [0.5021732 , 0.49841946, 0.50098324, ..., 0.49981016, 0.50000507,
        0.49993217],
       [0.50205255, 0.49843505, 0.5009038 , ..., 0.49979147, 0.4999932 ,
        0.49991176],
       [0.50192004, 0.49860355, 0.50086874, ..., 0.49983227, 0.5000045 ,
        0.4999401 ]], dtype=float32)>

Note that I removed the rescaling layer, I don't have it in my Tensorflow version. You can put it right back.

Post a Comment for "How To Reshape Batchdataset Class Tensor?"