Tensorflow tensor 數(shù)學(xué)運(yùn)算和邏輯運(yùn)算方式
一、arthmetic 算術(shù)操作(+,-,*,/,Mod)
(1)tensor-tensor操作(element-wise)
#兩個(gè)tensor 運(yùn)算 #運(yùn)算規(guī)則:element-wise。即c[i,j,..,k]=a[i,j,..,k] op b[i,j,..,k] ts1=tf.constant(1.0,shape=[2,2]) ts2=tf.Variable(tf.random_normal([2,2])) sess.run(tf.global_variables_initializer()) #以ts1和ts2為例: #(1)加法+ ts_add1=tf.add(ts1,ts2,name=None) ts_add2=ts1+ts2 #二者等價(jià) #(2)減法- ts_sub1=tf.subtract(ts1,ts2,name=None) ts_sub2=ts1-ts2 #二者等價(jià) #(3)乘法* ts_mul1=tf.multiply(ts1,ts2,name=None) ts_mul2=ts1*ts2 #(4)除法/ ts_div1=tf.divide(ts1,ts2,name=None) ts_div2=tf.div(ts1,ts2,name=None) #div 支持 broadcasting(即shape可不同) ts_div3=ts1/ts2 #另外還有truediv(x,y) x,y類型必須一致,floor_div等。 #(5)取模Mod(估計(jì)基本用不到)
(2)tensor-scalar操作
#scalar-tensor操作。 #對(duì)tensor中所有element執(zhí)行同樣的操作(+,-,*,/) #加法 ts_add=ts1+2 #減法 ts_sub=ts1-2 #乘法 ts_mul=ts1*2 #除法 ts_div=ts1/2
二、基本數(shù)學(xué)函數(shù)
#以下x,y均代表tensor tf.add_n(inputs, name=None) #inputs:tensor數(shù)組,所有tensor相加 tf.abs(x, name=None) #絕對(duì)值 tf.negative(x, name=None) #取反 tf.sign(x, name=None) #取符號(hào)(y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.) tf.square(x, name=None) #y=x*x tf.round(x, name=None) #Rounds the values of a tensor to the nearest integer, element-wise. tf.sqrt(x, name=None) #sqrt tf.pow(x, y, name=None) #x,y均為tensor,element-wise求pow tf.exp(x, name=None) #y=e^x tf.log(x, name=None) #y=log(x) tf.ceil(x, name=None) #ceil tf.floor(x, name=None) #floor tf.maximum(x, y, name=None) #z=max(x,y) tf.minimum(x, y, name=None) tf.cos(x, name=None) #三角函數(shù),sin,cos,tan,acos,asin,atan tf.sin(x, name=None) tf.tan(x, name=None) tf.acos(x, name=None) tf.asin(x, name=None) tf.atan(x, name=None) #... #等等一些函數(shù)。
三、Matrix矩陣操作
tf.diag(diagonal, name=None) #得到以diagonal為對(duì)角的tensor tf.diag_part(input, name=None) #tf.diag 逆操作,得到input的對(duì)角矩陣 tf.transpose(a, perm=None,name=None) #轉(zhuǎn)置矩陣,y[i,j]=x[j,i] #矩陣乘法 tf.matmul(a, b, transpose_a=False, transpose_b=False, # adjoint_a=False, adjoint_b=False, #共軛 a_is_sparse=False, b_is_sparse=False, #矩陣是否稀疏 name=None)
四、Reduction 歸約操作
#(1)tf.reduce_sum #當(dāng)keep_dims=False。rank of tensor會(huì)降維度。 tf.reduce_sum(input_tensor, axis=None, #要?dú)w約的dimention。值為None或一個(gè)數(shù)字或者數(shù)組。如0,1,[0,3,4] keep_dims=False, #if true, retains reduced dimensions with length 1. name=None, reduction_indices=None) #(2)tf.reduce_min / tf.reduce_max / tf.reduce_mean #參數(shù)與tf.reduce_sum一致。 #tf.reduce_min : 被歸約的數(shù)取最小值; #tf.reduce_max : 被歸約的數(shù)取最大值; #tf.reduce_mean: 被歸約的數(shù)取平均值。 #(3)邏輯操作 # tf.reduce_all:logical and operation # tf.reduce_any: logical or operation #(4)自定義操作函數(shù) tf.einsum(equation, *inputs) #例子: tf.einsum('ij,jk->ik', ts1,ts2) #矩陣乘法 tf.einsum('ij->ji',ts1) #矩陣轉(zhuǎn)置
五、tensor大小 比較
#(1)相等equal (element-wise) tf.equal(x, y, name=None) #Returns the truth value of (x == y) element-wise. #(2)不等not_equal tf.not_equal(x, y, name=None) #(3)其他比較 tf.less(x, y, name=None) tf.less_equal(x, y, name=None) tf.greater(x, y, name=None) tf.greater_equal(x, y, name=None)
六、恒等映射
#恒等映射
tf.identity(input, name=None) #Return a tensor with the same shape and contents as the input tensor or value.
七、類型轉(zhuǎn)化
tf.cast(x, dtype, name=None) #Casts a tensor to a new type. #For example: # tensor `a` is [1.8, 2.2], dtype=tf.float #tf.cast(a, tf.int32) ==> [1, 2] dtype=tf.int32
八、例子
(1)RELU實(shí)現(xiàn)
import tensorflow as tf def relu(x): #要構(gòu)造一個(gè)和x shape一樣的Tensor。源碼中應(yīng)該不會(huì)用效率這么低的寫法。 y=tf.constant(0.0,shape=x.get_shape()) return tf.where(tf.greater(x,y),x,y) sess=tf.Session() x=tf.Variable(tf.random_normal(shape=[10],stddev=10)) sess.run(tf.global_variables_initializer()) x_relu=relu(x) data_x,data_x_relu=sess.run((x,x_relu)) for i in range(0,len(data_x)): print("%.5f --relu--> %.5f" %(data_x[i],data_x_relu[i]))
補(bǔ)充知識(shí):tensorflow 復(fù)合邏輯‘且'和‘或'的實(shí)現(xiàn)
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
import tensorflow as tf n1 = tf.constant(2) n2 = tf.constant(3) n3 = tf.constant(4) n4 = tf.constant(5) def true_fn1(): return tf.constant(11) def false_fn1(): return tf.constant(22) def true_fn(): return tf.cond(n3<n4,true_fn1,false_fn1) def false_fn(): return tf.constant(33) r = tf.cond(n1<n2,true_fn,false_fn) sess = tf.Session() print(sess.run(r))
print結(jié)果11
相當(dāng)于實(shí)現(xiàn)了if n1<n2 and n3<n4:
后來(lái)發(fā)現(xiàn),用 & 和 | 就行了
import tensorflow as tf n1 = tf.constant(True,tf.bool) n2 = tf.constant(False,tf.bool) r1 = n1 | n2 r2 = n1 & n2 sess = tf.Session() print(sess.run(r1)) print(sess.run(r2))
import tensorflow as tf n1 = tf.constant(1)>tf.constant(0) n2 = tf.constant(1)<tf.constant(0) r1 = n1 | n2 r2 = n1 & n2 sess = tf.Session() print(sess.run(r1)) print(sess.run(r2))
以上這篇Tensorflow tensor 數(shù)學(xué)運(yùn)算和邏輯運(yùn)算方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python list元素為tuple時(shí)的排序方法
下面小編就為大家分享一篇python list元素為tuple時(shí)的排序方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加)
今天小編就為大家分享一篇Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法
這篇文章主要介紹了python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法,涉及Python中os模塊makedirs方法的使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí))
裝飾器(decorator)是一種高級(jí)Python語(yǔ)法。裝飾器可以對(duì)一個(gè)函數(shù)、方法或者類進(jìn)行加工。本文給大家介紹Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí)),對(duì)python裝飾器相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01python3通過(guò)subprocess模塊調(diào)用腳本并和腳本交互的操作
這篇文章主要介紹了python3通過(guò)subprocess模塊調(diào)用腳本并和腳本交互的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12centos系統(tǒng)升級(jí)python 2.7.3
CentOS上安裝的python版本是2.6,不能滿足我運(yùn)行軟件的要求,所以對(duì)python進(jìn)行升級(jí)。Python的最新版本已經(jīng)是3.3,但是Python3的兼容性可能還有一定的問(wèn)題,所以還是升級(jí)到2.7較為保險(xiǎn)。2014-07-07淺談python內(nèi)置變量-reversed(seq)
下面小編就為大家?guī)?lái)一篇淺談python內(nèi)置變量-reversed(seq)。小編覺(jué)得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06