[英]Print tensor value tensorflow 2.4
I tied to print one value while i train my Transformer.我在训练我的 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.但是tf.print
不打印任何东西。 However my first print('_')
works What did I do wrong?但是我的第一个print('_')
有效我做错了什么? 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
更新:你也可以向我解释tar_inp
和tar_real
的结构,而不是修复tf. print
tf. print
. tf. print
。
Try changing it to print()
with tensor.numpy()
-尝试使用tensor.numpy()
将其更改为print()
) -
@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. 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.所以你可以索引,然后将这些值转换为 numpy 数组。
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]
for (batch, (inp, tar)) in enumerate(dataset):
tar_inp = tar[:, :-1]
tar_real = tar[:, 1:]
print(tar_inp, tar_real, sep='\n\n')
break
[[ 2 90 21617 ... 0 0 0]
[ 2 282 1103 ... 0 0 0]
[ 2 1120 20666 ... 0 0 0]
...
[ 2 637 19 ... 0 0 0]
[ 2 200 6206 ... 0 0 0]
[ 2 647 19 ... 0 0 0]], shape=(64, 74), dtype=int32)
tf.Tensor(
[[ 90 21617 130 ... 0 0 0]
[ 282 1103 1514 ... 0 0 0]
[ 1120 20666 4508 ... 0 0 0]
...
[ 637 19 1789 ... 0 0 0]
[ 200 6206 9124 ... 0 0 0]
[ 647 19 1487 ... 0 0 0]], shape=(64, 74), dtype=int32)
Use:利用:
model.compile(... run_eagerly=True)
Then everything will work fine.然后一切都会正常工作。 You may have to remove @tf.function
(let me know if it's the case).您可能必须删除@tf.function
(如果是这种情况,请告诉我)。
The reason is that Tensorflow is made to be used with edge devices and/or anything outside of Python.原因是 Tensorflow 用于边缘设备和/或 Python 之外的任何设备。 When writing Tensorflow code, you're creating a graph that will be executed independent of Python.在编写 Tensorflow 代码时,您正在创建一个独立于 Python 执行的图形。 If you want it to be executed as Python functions, for instance if you want to print the values within Python, you will need to execute eagerly.如果您希望它作为 Python 函数执行,例如,如果您想打印 Python 中的值,则需要急切地执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.