Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print tensor value tensorflow 2.4

I tied to print one value while i train my Transformer.

@tf.function
def train_step(inp, tar):
    tar_inp = tar[:, :-1]
    tar_real = tar[:, 1:]
    global i
    if i == 1:
      print('__________________')
      tf.print('Inp: ', inp, output_stream=sys.stdout)
      tf.print('Tar: ', tar, output_stream=sys.stdout)
      tf.print('Tar_inp: ', tar_inp, output_stream=sys.stdout)
      tf.print('Tar_real: ', tar_real, output_stream=sys.stdout)
      i += 1
    .......

But tf.print doesn't print anything. However my first print('_') works What did I do wrong? Pls, help me to print my tensors.

UPDATE: U also could explain to me the structure of tar_inp and tar_real instead of fixing tf. print.

like image 403
Barry Alen Avatar asked Jun 12 '26 04:06

Barry Alen


1 Answers

Try changing it to print() with tensor.numpy() -

@tf.function
def train_step(inp, tar):
    tar_inp = tar[:, :-1]
    tar_real = tar[:, 1:]
    global i
    if i == 1:
      print('__________________')
      print('Inp: ', inp.numpy())
      print('Tar: ', tar.numpy())
      print('Tar_inp: ', tar_inp.numpy())
      print('Tar_real: ', tar_real.numpy())
      i += 1


W.r.t the tar_inp and tar_real, indexing works the same way on tensors as it does on numpy arrays but it returns a tensor object. So you can index, and then convert pull the values as a numpy array later.

print(tf.convert_to_tensor([1,2,3]).numpy()) #get numpy
print(tf.convert_to_tensor([1,2,3])[1:].numpy()) #get numpy after indexing
print(tf.convert_to_tensor([1,2,3]).numpy()[1:]) #index after getting numpy
[1 2 3]
[2 3]
[2 3]
like image 163
Akshay Sehgal Avatar answered Jun 14 '26 17:06

Akshay Sehgal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!