如何列印tensorflow 的dataset

2022-09-11 10:54:13 字數 1900 閱讀 1766

有時,為了除錯資料,需要將資料列印打出來,可以用interator來遍歷資料

首先定義兩個遍歷函式,

def

print_dataset(self, data_set):

iterator =data_set.make_one_shot_iterator()

next_element =iterator.get_next()

num_batch =0

with tf.train.monitoredtrainingsession() as sess:

while

notsess.should_stop():

value =sess.run(next_element)

num_batch += 1

print("

num batch:

", num_batch)

print("

batch value:

", value)

def

print_dataset2(self, data_set):

iterator =data_set.make_initializable_iterator()

next_element =iterator.get_next()

num_batch =0

with tf.train.monitoredtrainingsession() as sess:

sess.run(iterator.initializer)

while

true:

try:

value =sess.run(next_element)

print("

num batch:

", num_batch)

print("

batch value:

", value)

#assert j == value

#j += 1

num_batch += 1

except

tf.errors.outofrangeerror:

break

第乙個函式不支援lookup等操作,會報錯

valueerror: failed to create a one-shot iterator for a dataset. `dataset.make_one_shot_iterator()` does not support datasets that capture stateful objects, such as a `variable` or `lookuptable`. in these cases, use `dataset.make_initializable_iterator()`. (original error: cannot capture a stateful node (name:hash_table, type:hashtablev2) by value.)

在這種情況下,使用第二個函式。

dataset =tf.data.textlinedataset(file_list)

print("

*****==len(file_list)*****==

",len(file_list))

print

(dataset)

self.print_dataset(dataset)

self.text_set = self.text_set.map(lambda

src, tgt:

(self.case_table.lookup(src), self.case_table.lookup(tgt))

).prefetch(buffer_size)

self.print_dataset2(self.text_set)

tensorflow之如何列印tensor張量的值

在學習tensorflow的過程中,我們需要知道某個tensor的值是什麼,這個很重要,尤其是在debug的時候。直接print只能列印輸出shape的資訊,而要列印輸出tensor的值,需要借助class tf.session,class tf.interactivesession。因為我們在建立...

Tensorflow列印變數的值

用tf.print 函式 tf.print input data,message none,first n none,summarize none,name none 列印張量列表 輸入 input 通過此op的乙個tensor.即輸入tf.print 中的資料 data 當此op被計算之後列印輸出...

tensorflow 如何使用矩陣

refence tensorflow machine learning cookbook working with matrices packt.tensorflow.machine.learning.cookbook.2017 筆記 coding utf 8 import tensorflow a...