Search code examples
pythontensorflowdataset

How to read data in tf.Tensor


I have dataset for video captioning project. The dataset pipeline for training is Built as:

dataset = tf.data.Dataset.from_tensor_slices((videos , tf.ragged.constant(captions)))

I want to read all batch_data that goes to training step which is look like:

class VideoCaptioningModel(keras.Model):
.
.
.
    def train_step(self, batch_data):
        batch_img, batch_seq = batch_data
        batch_loss = 0
        batch_acc = 0
        
        print('batch_data=', batch_data)

.
.

The output is:

batch_data= (<tf.Tensor 'IteratorGetNext:0' shape=(None, 28, 1536) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None, 8) dtype=int64>)

I tried to use print('batch_data=', batch_data.numpy()) but I got:

AttributeError: 'tuple' object has no attribute 'numpy'

Solution

  • Your dataset consists of videos and captions and each entry in your dataset is a tuple. See:

    for x in dataset:
      tf.print(x[0]) # videos
      tf.print(x[1]) # captions
    

    Now, note that you can call .numpy() on a tf.Tensor in Eager Execution mode, but tuples do not have this property. So try:

    tf.print('batch_data=', batch_data[0].numpy())
    tf.print('batch_data=', batch_data[1].numpy())