Tensor数据相关的运算、函数讲解及与numpy区别(转载)

							            <div class="markdown_views">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<hr>

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.numpy

A * B 表示按元素计算
dot(A,B)表示矩阵乘法
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

数据类型转换

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
  • 2
  • 3
  • 4
  • 5

形状操作

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) 返回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) ==> 3

5.某一维求和
Tensor:tf.reduce_sum(b,reduction_indices=1)
numpy:np.sum(b,axis=1)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

数组方面的 切片和合并


 1.合并、连接数组

Tensor
tf.concat(0,[a,b])第一个参数表述位数
若a (11281283)  b( 11281283)
tf.concat(0,[a,b])  ( 21281283)

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-10)
# ‘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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

矩阵相关操作


1.tf.matrix_inverse  方阵的逆矩阵  
2.tf.matrix_determinant  方阵的行列式
3.tf.transpose转置  
4.tf.diag  给定对角线上的值,返回对角tensor
 
 
  • 1
  • 2
  • 3
  • 4

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()
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

参考文献


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值