2

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'
A_B_Y
  • 332
  • 2
  • 8

1 Answers1

1

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())
AloneTogether
  • 25,814
  • 5
  • 20
  • 39
  • @ AloneTogether, thanks, but I got this error now: AttributeError: 'Tensor' object has no attribute 'numpy' – A_B_Y Oct 31 '22 at 09:49
  • Yes, because you are in Graph mode and not in Eager Execution mode.. can you show where exactly the error is coming from in your code? I am guessing you are trying to use `tensor.numpy()` inside a `tf.function` and that does not work. See https://stackoverflow.com/questions/34097281/convert-a-tensor-to-numpy-array-in-tensorflow/70782632#70782632 – AloneTogether Oct 31 '22 at 10:00