我正在學習 MacBook M1 上 2.8.0 版本的 tensorflow。為了除錯資料集的 map 函式中的代碼,我想在我的函式中列印張量值。
def function(i):
print("in: ", i)
if i < 2:
i = i - 1
return i
dataset = tf.data.Dataset.range(1, 6)
dataset = dataset.map(lambda x: tf.py_function(function, inp=[x], Tout=tf.int64))
for x in dataset:
print("out:", x)
我得到了輸出:
in: tf.Tensor(1, shape=(), dtype=int64)
out: tf.Tensor(0, shape=(), dtype=int64)
in: tf.Tensor(2, shape=(), dtype=int64)
out: tf.Tensor(2, shape=(), dtype=int64)
in: tf.Tensor(3, shape=(), dtype=int64)
out: tf.Tensor(3, shape=(), dtype=int64)
in: tf.Tensor(4, shape=(), dtype=int64)
out: tf.Tensor(4, shape=(), dtype=int64)
in: tf.Tensor(5, shape=(), dtype=int64)
out: tf.Tensor(5, shape=(), dtype=int64)
在我洗掉外面的列印后,我沒有得到任何輸出。里面的印刷品和外面的印刷品有什么區別。我不明白為什么它只有在列印同時出現時才能生效。除此之外,print 和 tf.print 之間有什么區別?
uj5u.com熱心網友回復:
我相信這是因為 tensorflow 資料集具有延遲加載,這意味著在您實際嘗試迭代結果之前不會對它們進行評估。
當您洗掉 for 回圈時,您不再對結果進行迭代,因此它從未被評估過。
見https://stackoverflow.com/a/54679387/494134
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/481084.html
