1. 程式人生 > >tensorflow 中檢視張量值和張量大小

tensorflow 中檢視張量值和張量大小

通過
with tf.Session() as sess:
    print (sess.run(y))
即可打印出變數的值。下面給出例子
import numpy as np
import tensorflow as tf
x = tf.constant([1,2,3,4,5,6])
y=tf.reshape(x,(2,3))
with tf.Session() as sess:
    print (sess.run(y))
2.在有的程式中需要使用如下
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)

print(sess.run(y))

來檢視張量,例子:

import numpy as np
import tensorflow as tf
x = tf.constant([1,2,3,4,5,6])
y=tf.reshape(x,[1,1,1,2,3])
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(y))
3.檢視張量的大小。通過 變數名.shape即可顯示,例子如下:
import numpy as np
import 
tensorflow as tf x = tf.constant([1,2,3,4,5,6]) y=tf.reshape(x,[1,1,1,2,3]) print (y.shape)
以上是在python3以上版本執行