网站推广可采用的方法有哪些,建设银行辽宁省分行网站,佛山出格建站,上海做产地证在哪个网站录入Tensor tensorflow 中使用它来表示数据。可以看做多维数组或者list。 标量是张量#xff0c;向量是张量#xff0c;矩阵是张量#xff0c;矩阵的矩阵是张量。
常用几种定义方法
1. variable变量#xff0c;一般是可以被更更新或更改的数值#xff0c;即在流图运行过程中…
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), dtypefloat32) print(ta.eval())
numpy a np.zeros((2,2)) print(a) Tensor 相关操作 算术操作
1.加法操作Tensor、numpy 两个的效果一致遇到不相同的维度时会自动扩充。但是同一维度上的大小必须一致的除了某一维度是值是1的情况。
Tensor的shape是tensor1和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, nameNone)
tensor a is [1.8, 2.2], dtypetf.float
tf.cast(a, tf.int32) [1, 2] # dtypetf.int32
形状操作
1.shape
numpya.shape()
Tensora.get_shape() tf.shape(a)2.reshape
Tensortf.reshape(a, (1,4))
numpynp.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.某一维求和
Tensortf.reduce_sum(b,reduction_indices1)
numpynp.sum(b,axis1) 数组方面的 切片和合并 1.合并、连接数组Tensor
tf.concat(0,[a,b])第一个参数表述位数
若a 11281283 b( 11281283
tf.concat(0,[a,b]) ( 21281283numpy
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, nameNone)
#’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, axis0, 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], axis1) [[1, 2, 3], [4, 5, 6]]
等价于tf.pack([x, y, z]) np.asarray([x, y, z])5.tf.reverse(tensor, dims, nameNone)
沿着某维度进行序列反转
其中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, permNone, 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),dtypenp.float32)
tf.convert_to_tensor(numpyData)2.Tensor到 numpy array eval()
tf.constant([1,2,3]).eval() 参考文献 Tensor数据相关的运算及函数讲解