05.16 「入門」TensorFlow中的基本操作,與Numpy類型的無縫銜接

「入門」TensorFlow中的基本操作,與Numpy類型的無縫銜接

這篇文章總結,深度學習的框架:TensorFlow 的基本接口和操作類型。

01

Operations分類預覽

「入門」TensorFlow中的基本操作,與Numpy類型的無縫銜接

02

Tensor

1 0-d tensor, or "scalar"

t_0 = 19

tf.zeros_like(t_0) # ==> 0

tf.ones_like(t_0) # ==> 1

2 1-d tensor, or "vector"

t_1 = ['apple', 'peach', 'banana'] #==>['' '' '']

3 2x2 tensor, or "matrix"

t_2 = [[True, False, False], [False, False, True], [False, True, False]]

tf.zeros_like(t_2) # ==> 2x2 tensor, all elements are False

tf.ones_like(t_2) # ==> 2x2 tensor, all elements are True

03

TensorFlow和Numpy

TensorFlow 和 Numpy能做到無縫銜接,例如:

tf.int32 == np.int32 # True

但是,將來tensorflow和numpy可能兼容性沒有現在這麼好。

可以傳遞Numpy對象給TensorFlow ops

tf.ones([2, 2], np.float32) # ⇒ [[1.0 1.0], [1.0 1.0]]

不要傳遞python的原生對象給Tensorflow,因為Tensorflow必須推斷python的類型。

04

Constant

tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

b = tf.constant([[0, 1], [2, 3]], name="b")

tf.ones(shape, dtype=tf.float32, name=None)

tf.linspace(10.0, 13.0, 4) #==> [10.0 11.0 12.0 13.0]

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype= tf.float32, seed=None, name=None)

tf.set_random_seed(seed)

a = tf.constant([3, 6])

b = tf.constant([2, 2])

tf.add(a, b) # >> [5 8]

05

What's wrong with constant?

Constants 存儲在 graph definition 中,當 constants很大時,加載圖就會變得非常昂貴。

什麼場合下用constant? 僅僅用primitive types為constant,用variables or readers 來緩存佔用內存更大的數據。


分享到:


相關文章: