Tensor数据相关的运算、函数讲解及与numpy区别
Tensor
tensorflow 中使用它來表示數據。可以看做多維數組或者list。
標量是張量,向量是張量,矩陣是張量,矩陣的矩陣是張量。
常用幾種定義方法
1. variable變量,一般是可以被更更新或更改的數值,即在流圖運行過程中可以被不斷動態調整的值。我們訓練一個模型的時候,會用到Tensorflow中的變量(Variables),我們需要它來保持和更新參數值,和張量一樣,變量也保存在內存緩沖區當中。
我們要預先對變量初始化,Tensorflow的變量必須先初始化然后才有值!而常值張量是不需要的,變量可以先設置好初始化方式,但是真正初始化是要sess.run(tf.global_variables_initializer())才真的初始化。
2.constant 常量張量
3.placeholder:占位符 動態改變值 feeddict
numpy
b = np.array( [ (1.5,2,3), (4,5,6) ] )
Tensorflow 和numpy區別
相同點: 都提供n位數組
不同點: numpy支持ndarray,而Tensorflow里有tensor;numpy不提供創建張量函數和求導,也不提供GPU支持。
顯示
Tensor
需要加eval函數
ta = tf.zeros((2,2))
print(ta)
Tensor(“zeros_1:0”, shape=(2, 2), dtype=float32)
print(ta.eval())
numpy
a = np.zeros((2,2))
print(a)
Tensor 相關操作
算術操作
1.加法操作Tensor、numpy 兩個的效果一致遇到不相同的維度時,會自動擴充。但是同一維度上的大小必須一致的,除了某一維度是值是1的情況。 Tensor的shape是(tensor,1)和(1,tensor)這是可以相加的,會自動擴充。2.矩陣乘法 Tensor A * B 表示按元素計算 tf.mul(A,B) 表示按元素計算 tf.matmul(A,B) 表示矩陣乘法3.numpyA * B 表示按元素計算 dot(A,B)表示矩陣乘法數據類型轉換
tf.to_double(a) tf.to_float(a) tf.cast(x, dtype, name=None) tensor a is [1.8, 2.2], dtype=tf.float tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int32形狀操作
1.shape numpy:a.shape() Tensor:a.get_shape() tf.shape(a)2.reshape Tensor:tf.reshape(a, (1,4)) numpy:np.reshape(a,(1,4))3.tf.size(a)返回數據的元素數量 tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]) size = 124.tf.rank(a) 返回tensor的rank #’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] # shape of tensor ‘t’ is [2, 2, 3] rank(t) ==> 35.某一維求和 Tensor:tf.reduce_sum(b,reduction_indices=1) numpy:np.sum(b,axis=1)數組方面的 切片和合并
1.合并、連接數組Tensor tf.concat(0,[a,b])第一個參數表述位數 若a (1,128,128,3) b( 1,128,128,3) tf.concat(0,[a,b]) ( 2,128,128,3)numpy vstack 和 hstack stack(a,axis=)2.獲取整行整列數據Tensor temp = tf.constant(0,shape=[5,5]) temp1 = temp[0,:] 獲取某行 temp2 = temp[:,1] 獲取某列 temp[1,1] 獲取某個元素 temp[1:3,1:3] 獲取某個范圍的行列元素 沿著某一維度將tensor分離為num_split tensorstf.split(split_dim, num_split, value, name=’split’) # ‘value’ is a tensor with shape [5, 30] # Split ‘value’ into 3 tensors along dimension 1 split0, split1, split2 = tf.split(1, 3, value) tf.shape(split0) ==> [5, 10]3.對tensor進行切片操作 tf.slice(input_, begin, size, name=None) #’input’ is #[[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]] tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]] tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3], [4, 4, 4]]] tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]], [[5, 5, 5]]]4.打包tf.pack(values, axis=0, name=’pack’) # ‘x’ is [1, 4], ‘y’ is [2, 5], ‘z’ is [3, 6] pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # 沿著第一維pack pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] 等價于tf.pack([x, y, z]) = np.asarray([x, y, z])5.tf.reverse(tensor, dims, name=None) 沿著某維度進行序列反轉 其中dim為列表,元素為bool型,size等于rank(tensor) # tensor ‘t’ is [[[[ 0, 1, 2, 3], #[ 4, 5, 6, 7], #[ 8, 9, 10, 11]], #[[12, 13, 14, 15], #[16, 17, 18, 19], #[20, 21, 22, 23]]]] # tensor ‘t’ shape is [1, 2, 3, 4] # ‘dims’ is [False, False, False, True] reverse(t, dims) ==> [[[[ 3, 2, 1, 0], [ 7, 6, 5, 4], [ 11, 10, 9, 8]], [[15, 14, 13, 12], [19, 18, 17, 16], [23, 22, 21, 20]]]]6.tf.transpose(a, perm=None, name=’transpose’) 調換tensor的維度順序如為定義,則perm為(n-1…0) # ‘x’ is [[1 2 3],[4 5 6]] tf.transpose(x) ==> [[1 4], [2 5],[3 6]] # Equivalently tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
矩陣相關操作
1.tf.matrix_inverse 方陣的逆矩陣 2.tf.matrix_determinant 方陣的行列式 3.tf.transpose轉置 4.tf.diag 給定對角線上的值,返回對角tensor
Tensor 和 numpy array互轉
1.numpy array 到 TensornumpyData = np.zeros((1,10,10,3),dtype=np.float32) tf.convert_to_tensor(numpyData)2.Tensor到 numpy array eval() tf.constant([1,2,3]).eval()
參考文獻
Tensor數據相關的運算及函數講解
總結
以上是生活随笔為你收集整理的Tensor数据相关的运算、函数讲解及与numpy区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使命召唤Online USP45详细图文
- 下一篇: 关于Matlab编程的思考(待续)