1. 程式人生 > >Tensorflow:輸出tensor具體值

Tensorflow:輸出tensor具體值

在一些任務中,經常需要看tensor的具體值是多少,舉個例子,你得檢視一部分的feature map,或者是一些資料是否符合訓練的預期,那麼怎麼檢視對應tensor的具體值(⊙o⊙)?

有些小夥伴可能會特別不屑,print一下不就好了?哈哈,要是直接print有效, LZ還花這閒工夫幹啥!

舉個例子,不難.
我們先生成一些隨機數

import numpy as np
np.random.seed(100)
triangles = np.random.rand(1, 5, 3, 3).astype('float32')

使用print列印一下:

print triangles

顯示如下:

[[[[0.54340494 0.2783694  0.4245176 ]
   [0.84477615 0.00471886 0.12156912]
   [0.67074907 0.82585275 0.13670659]]
  [[0.5750933  0.89132196 0.20920213]
   [0.18532822 0.10837689 0.21969749]
   [0.9786238  0.8116832  0.17194101]]
  [[0.81622475 0.27407375 0.4317042 ]
   [0.9400298  0.81764936 0.33611196]
   [0.17541045 0.37283206 0.00568851]]
  [[0.25242636 0.7956625  0.01525497]
   [0.5988434  0.6038045  0.10514768]
   [0.38194343 0.03647606 0.89041156]]
  [[0.98092085 0.05994199 0.89054596]
   [0.5769015  0.7424797  0.63018394]
   [0.5818422  0.02043913 0.21002658]]]]

OK,然後我們將資料轉化成tensor型別,在使用print進行列印,看下會發生什麼?

inp = tf.constant(traingles)
print inp

顯示如下:並沒有輸出具體數值,同樣也就沒有辦法對所獲得的tensor進行分析

Tensor("Const:0", shape=(1, 5, 3, 3), dtype=float32)

解決方法有兩種:

第一種:利用session.run()進行處理

sess = tf.Session()
print(sess.run(inp))

最後的顯示結果如此啊所示:

[[[[0.54340494 0.2783694  0.4245176 ]
   [0.84477615 0.00471886 0.12156912]
   [0.67074907 0.82585275 0.13670659]]
  [[0.5750933  0.89132196 0.20920213]
   [0.18532822 0.10837689 0.21969749]
   [0.9786238  0.8116832  0.17194101]]
  [[0.81622475 0.27407375 0.4317042 ]
   [0.9400298  0.81764936 0.33611196]
   [0.17541045 0.37283206 0.00568851]]
  [[0.25242636 0.7956625  0.01525497]
   [0.5988434  0.6038045  0.10514768]
   [0.38194343 0.03647606 0.89041156]]
  [[0.98092085 0.05994199 0.89054596]
   [0.5769015  0.7424797  0.63018394]
   [0.5818422  0.02043913 0.21002658]]]]

第二種方法:

with tf.Session():
    print(inp.eval())

最後列印結果也是一致的

[[[[0.54340494 0.2783694  0.4245176 ]
   [0.84477615 0.00471886 0.12156912]
   [0.67074907 0.82585275 0.13670659]]
  [[0.5750933  0.89132196 0.20920213]
   [0.18532822 0.10837689 0.21969749]
   [0.9786238  0.8116832  0.17194101]]
  [[0.81622475 0.27407375 0.4317042 ]
   [0.9400298  0.81764936 0.33611196]
   [0.17541045 0.37283206 0.00568851]]
  [[0.25242636 0.7956625  0.01525497]
   [0.5988434  0.6038045  0.10514768]
   [0.38194343 0.03647606 0.89041156]]
  [[0.98092085 0.05994199 0.89054596]
   [0.5769015  0.7424797  0.63018394]
   [0.5818422  0.02043913 0.21002658]]]]

如果不先註冊session,直接執行

print(inp.eval())

那麼就會報錯,如下所示:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 606, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3914, in _eval_using_default_session
    raise ValueError("Cannot evaluate tensor using `eval()`: No default "
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

Y(o)Y,好啦,這個就是tensorflow輸出tensor具體值的一個小技巧,O(∩_∩)O哈哈~,Felaim要繼續加油呢!