In [35]: a=tf.zeros([2,3],tf.float32)
In [36]: a
Out[36]: <tf.Tensor 'zeros:0' shape=(2, 3) dtype=float32>
In [37]: sess=tf.InteractiveSession()
In [38]: a.eval()
Out[38]:
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)
In [39]: b=tf.ones([2,3],tf.float32)
In [40]: b.eval()
Out[40]:
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)
In [41]: c=tf.slice(a,[0,0],[1,1])
In [42]: c
Out[42]: <tf.Tensor 'Slice:0' shape=(1, 1) dtype=float32>
In [43]: c.eval()
Out[43]: array([[0.]], dtype=float32) # 1x1的tensor=>1x1的array
In [44]: d=a[0][0] # scalar的tensor=>单个数
In [45]: d.eval()
Out[45]: 0.0
In [14]: b=tf.reduce_sum(a)
In [15]: b
Out[15]: <tf.Tensor 'Sum:0' shape=() dtype=float32>
In [16]: b.eval()
Out[16]: 6.0
In [7]: a=tf.ones([2,3,4])
In [8]: a
In [9]: c=tf.slice(a,[0,0,0],[1,1,1])
In [10]: c
Out[10]: <tf.Tensor 'Slice:0' shape=(1, 1, 1) dtype=float32>
In [11]: d=a[0][0][0]
In [15]: d
Out[15]: <tf.Tensor 'strided_slice_2:0' shape=() dtype=float32>
In [12]: e=c-d
In [13]: sess=tf.InteractiveSession()
In [14]: e.eval()
Out[14]: array([[[0.]]], dtype=float32)
In [16]: a.eval()
Out[16]:
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=float32)
In [17]: ac=a-c
In [18]: ac.eval()
Out[18]:
array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]], dtype=float32)
tensor数据类型探索
最新推荐文章于 2022-03-19 17:50:46 发布