TensorFLow 基本用法(一)

# -*- coding:utf-8 -*-
#import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")
import tensorflow as tf
sess=tf.Session()
import numpy as np
#tensor生成
tens1=tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]])
print sess.run(tens1)
x=tf.constant(np.random.rand(32).astype(np.float32))
y=tf.constant([1,2,3])
print sess.run(x)
print x.eval()#通上
#np_to_tensor
x_data=[[1.,2.,3.],[4.,5.,6.],[7,8,9]]
x=tf.convert_to_tensor(x_data,dtype=tf.float32)
tf.Operation.type#返回操作类型
tf.Operation.inputs#返回表示操作的输入张量对象列表
#定义张量变量
b=tf.Variable(tf.zeros([1000]))
#需要初始化后才能变量显示
sess.run(tf.initialize_all_variables())
sess.run(b)
 
 

矩阵操作

#矩阵操作
sess=tf.InteractiveSession()
x1=tf.constant([[1,2],[3,4]])
x2=tf.constant([[2,3],[4,5]])
tf.transpose(x1).eval()#矩阵转置
tf.matmul(x1,x2).eval()#矩阵相乘 matrix multiplication
x_data=[[1.,2.,3.],[4.,4.,6.],[7,11,9]]
tf.matrix_determinant(x_data).eval()#行列式计算 matrix determinant
tf.matrix_inverse(x_data).eval()#矩阵求逆 matrix inverse
y=[[1.1],[1.],[1]]
tf.matrix_solve(x_data,y).eval()#求多元线性方程组
 
 

元素操作

#对应元素操作
x1=tf.constant([[1,2],[3,4]])
x2=tf.constant([[2,3],[4,5]])
#两矩阵操作,其中name默认为None
tf.add(x1,x2,name=None).eval()#对应元素相加
tf.subtract(x1,x2,name=None).eval()#对应元素相减
tf.multiply(x1,x2,name=None).eval()#对应元素相乘
tf.divide(x1,x2,name=None).eval()#对应元素相除
tf.div(x1,x2,name=None).eval()#对应元素取商
tf.mod(x1,x2,name=None).eval()#对应元素取余
tf.maximum(x1,x2).eval()#对应元素取最大值
tf.minimum(x1,x2).eval()#对应元素取最小值


tf.accumulate_n([x1,x2]).eval()#列求和
tf.cumsum([x1,x2]).eval()#求累积和
'''
tf.cumsum([a, b, c]) ==> [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0]
'''
#一矩阵操作
x1=tf.constant([[1.2,-2.2],[-3,4]])
tf.abs(x1,name=None).eval()#对应元素取绝对值
tf.negative(x1,name=None).eval()#对应元素取负
tf.sign(x1,name=None).eval()#对应元素取符号 if x>0,y=1;if x==0,y=0;if x<0 ,y=-1;
tf.square(x1,name=None).eval()#对应元素平方
tf.round(x1,name=None).eval()#对应元素取最近整数
x1=tf.constant([[1.,2],[3,4]])
x2=tf.constant([[2,3.],[4,5]])
tf.pow(x1,x2).eval()#对应元素x1的x2幂次方。x1,x2的元素类型必须相同
####矩阵中元素必须为float16, float32, float64, complex64, complex128,不能是int类型
tf.sqrt(x1,name=None).eval()#对应元素取平方根,
tf.exp(x1,name=None).eval()#对应元素取e的次方,float16, float32, float64, complex64, complex128,不能是int
tf.log(x1).eval()#计算e的ln对数float16, float32, float64, complex64, complex128,不能是int
tf.cos(x1).eval()#对应元素取cos
tf.sin(x1).eval()#对应元素取sin
tf.tan(x1).eval()#对应元素取tan
tf.atan(x1).eval()#对应元素取ctan
 
 

约简

#约简:reduction——跨维度张量操作,计算结果比原张量缩小一个维度
#product,minimum,maximum,mean,all,any,accumulate_n
sess=tf.InteractiveSession()
x=tf.constant([[1.,2,3],[3,2,1],[-1,-2,-3]])
#
tf.reduce_prod(x,reduction_indices=1).eval()#
tf.reduce_sum(x,0).eval()#0:列求和,1行求和
tf.reduce_max(x,1).eval()#0:列最大,1行最大
tf.reduce_min(x,0).eval()#0:列最小,1行最小
tf.reduce_mean(x,0).eval()#0:列平均,1行平均,如果是int,向下取整


#逻辑运算
bool_x=[[False,True],[False,True]]
tf.reduce_all(bool_x).eval()#所有元素求与
tf.reduce_all(bool_x,0).eval()#0:列求与,1:行求与
tf.reduce_any(bool_x).eval()#所有元素求或
tf.reduce_any(bool_x,0).eval()#0:列求或,1:行求或
 
 

分割

'''分割'''#重复索引相同,相同索引进行操作
c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
#将对应的行分为[0,2]标签,相同的行标签的元素对应求和,乘,最小值,最大值,平均值,并放入对应的行标签中
tf.segment_sum(c, tf.constant([0, 2, 2])).eval()#和
tf.segment_prod(c, tf.constant([0, 2, 2])).eval()#??
tf.segment_max(c, tf.constant([0, 2, 2])).eval()#最大值
tf.segment_min(c, tf.constant([0, 2, 2])).eval()#最小值
tf.segment_mean(c, tf.constant([0, 2, 2])).eval()#平均值


seg_ids=tf.constant([0,1,1,2,2])
tens1=tf.constant([[2,5,3,-15],[0,3,-2,5],[4,3,5,3],[6,1,4,0],[6,1,4,0]])
tens1.eval()
 
 

显示维度

#显示维度
x=tf.constant([[2,5,3,-15],[0,3,-2,5],[4,3,5,3],[6,1,4,0],[6,1,4,0]])
tf.argmin(x,1).eval()#每行中最小值中最小的索引位置      0:列,1:行
tf.arg_min(x,1).eval()
tf.argmin(x,0).eval()#每列中最小值中最小的索引位置
tf.arg_min(x,0).eval()
tf.argmax(x,1).eval()#每行中最大值中最小的索引位置  
tf.arg_max(x,1).eval()
tf.argmax(x,0).eval()#每列中最大值中最小的索引位置  
tf.arg_max(x,0).eval()


boolx=tf.constant([[True,False],[False,True]])
tf.where(boolx).eval()#显示True的值


listy=tf.constant([4,5,8,9])
listx=tf.constant([9,1,2,2,2,3,2,4,5,6,7,8])
tf.unique(listx)[0].eval()#统计唯一元素(类似set)array([9, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)
tf.unique(listx)[1].eval()#显示序列中相同元素的位置array([0, 1, 2, 2, 2, 3, 2, 4, 5, 6, 7, 8], dtype=int32)
tf.unique_with_counts(listx)[2].eval()#显示唯一值的对应的统计量array([1, 1, 4, 1, 1, 1, 1, 1, 1], dtype=int32)

张量变换

#张量形状变换
x=tf.constant([[2,5,3,-5],[0,3,-2,5],[4,3,5,3],[6,1,4,0]])
tf.shape(x).eval()#show the shape of the tensor,,,array([4, 4], dtype=int32)
tf.size(x).eval()#size of the tensor ,元素的个数
tf.rank(x).eval()#rank of the tensor,张量的阶(几层【】括号)
tf.reshape(x,[2,4,2]).eval()#reshape
'''array([[[ 2,  5],
        [ 3, -5],
        [ 0,  3],
        [-2,  5]],

       [[ 4,  3],
        [ 5,  3],
        [ 6,  1],
        [ 4,  0]]], dtype=int32)'''
tf.squeeze(x).eval()#去处维度为1的维度
tf.expand_dims(x,-1).eval()#增加一个维度,-1:从最内层增加一个维度,0,不变维度,1:从外层增加一个维度

切片

#切片(slicing),连接joining,,添加填充add padding,打包pack,解包unpack,拆片
x_matrix=tf.reshape(tf.constant(range(9)),[3,3]).eval()
t_array=tf.constant([1,2,3,4,9,8,6,5])
t_array2=tf.constant([2,3,4,5,6,7,8,9])


#切片_matrix
tf.slice(x_matrix,[1,0],[2,3]).eval()#第一个是起始点,后面是切片的大小


'''array([[3, 4, 5],
            [6, 7, 8]], dtype=int32)
'''
y=tf.reshape(tf.constant(range(24)),[4,6]).eval()
tf.split(y,num_or_size_splits=2,axis=0)[0].eval()#矩阵切片,按行axis=0切num_or_size_splits=2份,并显示第0份切片
'''array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]], dtype=int32)'''
#序列
tf.split(t_array,num_or_size_splits=4,axis=0)[0].eval()#,,序列切成num_or_size_splits=4片,axis=0可省略
tf.tile([1,2],[2]).eval()#[1,2]重复2次,array([1, 2, 1, 2], dtype=int32)
tf.pad(x_matrix,[[1,2],[2,1]]).eval()#在对应的上1,下2,左2,右1,添充对应的0行列
'''array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 2, 0],
       [0, 0, 3, 4, 5, 0],
       [0, 0, 6, 7, 8, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]], dtype=int32)'''
tf.concat(0,[t_array,t_array2])





猜你喜欢

转载自blog.csdn.net/Yshihui/article/details/80335859