Tensorflow学习笔记【2】张量操作篇

1 数据类型转换

1.1数据类型

整型数据

代码描述
tf.int88位整数
tf.int1616位整数
tf.int3232位整数
tf.int6464位整数
tf.uint88位无符号整数。
tf.uint1616位无符号整数。

浮点型数据

代码描述
tf.float1616位浮点数
tf.float3232位浮点数
tf.float6464位浮点数
tf.double等同于tf.float64

字符型

代码描述
tf.string字符串

布尔型

代码描述
tf.bool布尔型

复数型

代码描述
tf.complex6464位复数
tf.complex128128位复数

1.2数据类型转换

操作描述
tf.string_to_number (string_tensor, out_type=None, name=None)字符串转为数字
tf.to_double(x, name=’ToDouble’)转为64位浮点类型–float64
tf.to_float(x, name=’ToFloat’)转为32位浮点类型–float32
tf.to_int32(x, name=’ToInt32’)转为32位整型–int32
tf.to_int64(x, name=’ToInt64’)转为64位整型–int64
tf.cast(x, dtype, name=None)将x或者x.values转换为dtype

2 张量操作

2.1 形状操作

操作描述
tf.shape(input, name=None)返回数据的shape.ps: ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] ,shape(t) ==> [2, 2, 3]
tf.size(input, name=None)返回数据的元素数量 . ps: ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]] ,size(t) ==> 12
tf.rank(input, name=None)返回tensor的rank注意:此rank不同于矩阵的rank,tensor的rank表示一个tensor需要的索引数目来唯一表示任何一个元素也就是通常所说的 “order”, “degree”或”ndims”.ps: ’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
tf.reshape(tensor, shape, name=None)改变tensor的形状,tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9],tensor ‘t’ has shape [9] reshape(t, [3, 3]) ==> [[1, 2, 3],[4, 5, 6],[7, 8, 9]],如果shape有元素[-1],表示在该维度打平至一维,-1 将自动推导得为9:reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]]
tf.expand_dim()增加数据的维度,# ‘t’ is a tensor of shape shape(expand_dims(t, 0)) ==> [1, 2],shape(expand_dims(t, 1)) ==> [2,1], shape(expand_dims(t, -1)) ==> [2, 1]
tf.squeeze该函数返回一个张量,这个张量是将原始input中所有维度为1的那些维都删掉的结果

2.2切片和合并

操作描述
tf.slice(input_, begin, size, name=None)x=[[1,2,3],[4,5,6]] ,slice(x,begin,size) begin = [0,1] ,size = [2,1],输出[[2],[5]],对tensor进行分片。
tf.split(split_dim, num_split, value, name=’split’)沿着某一维度将tensor分离为num_split tensors‘value’ is a tensor with shape [5, 30],split0, split1, split2 = tf.split(1, 3, value),tf.shape(split0) ==> [5, 10]
tf.concat(concat_dim, values, name=’concat’)沿着某一维度连结tensor t1 = [[1, 2, 3], [4, 5, 6]],t2 = [[7, 8, 9], [10, 11, 12]],tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]],如果想沿着tensor一新轴连结打包,那么可以:tf.concat(axis, [tf.expand_dims(t, axis) for t in tensors]),等同于tf.pack(tensors, axis=axis)。
tf.transpose(a, perm=None, name=’transpose’)调换tensor的维度顺序按照列表perm的维度排列调换tensor顺序,‘x’ is [[1 2 3],[4 5 6]],tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
tf.one_hot(indices, depth, on_value=None, off_value=None,axis=None, dtype=None, name=None)indices = [0, 2, -1, 1],depth = 3#Then output is [4 x 3]: output =[ [1.0 0.0 0.0], [0.0 0.0 1.0], [0.0 0.0 0.0] ,[0.0 1.0 0.0] ]

2.3 归约计算

操作描述
tf.reduce_sum(input_tensor, reduction_indices=None,keep_dims=False, name=None)计算总和
tf.reduce_min(input_tensor,reduction_indices=None,keep_dims=False, name=None)求最小值
tf.reduce_max(input_tensor,reduction_indices=None,keep_dims=False, name=None)求最大值
tf.reduce_mean(input_tensor,reduction_indices=None,keep_dims=False, name=None)求平均值

2.4索引求取

操作描述
tf.argmin(input, dimension, name=None)返回input最小值的索引index
tf.argmax(input, dimension, name=None)返回input最大值的索引index
tf.listdiff(x, y, name=None)返回x,y中不同值的索引
tf.where(input, name=None)具体下列例子,该式子需要重点掌握
tf.unique(x, name=None)# tensor ‘x’ is [1, 1, 2, 4, 4, 4, 7, 8, 8] ,y idx = unique(x)y ==> [1, 2, 4, 7, 8] ,idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]

tf.where

理解:就是要根据条件找到你要的东西。

condition:条件,是一个boolean

x:数据

y:同x维度的数据。

返回,返回符合条件的数据。当条件为真,取x对应的数据;当条件为假,取y对应的数据

import tensorflow as tf
x = [[1,2,3],[4,5,6]]
y = [[7,8,9],[10,11,12]]
condition3 = [[True,False,False],
             [False,True,True]]
condition4 = [[True,False,False],
             [True,True,False]]
with tf.Session() as sess:
    print(sess.run(tf.where(condition3,x,y)))
    print(sess.run(tf.where(condition4,x,y)))
#结果一
[[1,8,9],[10,5,6]]

#结果二
[[1,8,9],[4,5,12]]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值