Tensor数据相关的运算及函数讲解

来源:互联网 发布:零基础如何学好c语言 编辑:程序博客网 时间:2024/05/18 00:15

                      Tensor数据相关的运算及函数讲解

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里有tensornumpy不提供创建张量函数和求导,也不提供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.numpy

A * 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  = 12


4.tf.rank(a) 返回tensorrank 

#’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]

# shape of tensor ‘t’ is [2, 2, 3]

rank(t) ==> 3


5.某一维求和

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 tensors

tf.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 Tensor


numpyData = 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()



参考文献

http://blog.csdn.net/lenbow/article/details/52152766


2 0