您好,登錄后才能下訂單哦!
這篇文章主要介紹TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
張量:TensorFlow的張量是n維數(shù)組,類型為tf.Tensor。
標量:一個數(shù)字 (0階張量)
向量:一維數(shù)組 (1階張量)
矩陣:二維數(shù)組 (2階張量)
#創(chuàng)建常數(shù)張量 a = tf.constant(3.0) print(a)
#創(chuàng)建常數(shù)張量 a = tf.constant(3.0) print(a)
#創(chuàng)建張量數(shù)組 #0: array_0 = tf.zeros(shape=[3,3]) #3*3數(shù)組(0)
#1: array_1 = tf.ones(shape=[3,3]) #3*3數(shù)組(1)
#隨機: array_random = tf.random_normal(shape=[2,3], mean=1.75, stddev=0.12) # 2*3數(shù)組 均值(1.75) 標準差
查看張量值:張量.eval()
#會話(查看張量) with tf.Session() as sess: print(a.eval()) print(array_0.eval()) print(array_1.eval()) print(array_random.eval())
#修改張量類型 array_0 = tf.cast(array_0, tf.int32)
注:屬于動態(tài)改變張量,需要張量元素個數(shù)固定。
#修改張量形狀 array_random = tf.reshape(array_random, shape=[3,2])
修改前:
修改后:
# 張量(創(chuàng)建與修改) import tensorflow as tf # 創(chuàng)建張量 def Create_Tensor(): # 創(chuàng)建常數(shù)張量 a = tf.constant(3.0) print(a) # 創(chuàng)建張量數(shù)組 # 0: array_0 = tf.zeros(shape=[3, 3]) # 3*3數(shù)組(0) # 1: array_1 = tf.ones(shape=[3, 3]) # 3*3數(shù)組(1) # 隨機: array_random = tf.random_normal(shape=[2, 3], mean=1.75, stddev=0.12) # 2*3數(shù)組 均值(1.75) 標準差 # 會話(查看張量) with tf.Session() as sess: print(a.eval()) print(array_0.eval()) print(array_1.eval()) print(array_random.eval()) # 修改張量 def Modify_Tensor(): global array_0, array_random print('修改后的:') # 修改張量類型 array_0 = tf.cast(array_0, tf.int32) # 修改張量形狀 array_random = tf.reshape(array_random, shape=[3, 2]) # 會話(查看張量) with tf.Session() as sess: print(array_0.eval()) print(array_random.eval()) # 創(chuàng)建張量 Create_Tensor() # 修改張量 Modify_Tensor()
# 定義變量 a = tf.Variable(initial_value=2) b = tf.Variable(initial_value=4) c = tf.add(a,b)
TensorFlow的變量必須初始化,否則會報錯。
# 初始化變量 init = tf.global_variables_initializer()
# 開啟會話 with tf.Session() as sess: sess.run(init) print(sess.run(c))
# 變量 import tensorflow as tf # 定義變量 a = tf.Variable(initial_value=2) b = tf.Variable(initial_value=4) c = tf.add(a,b) # 初始化變量 init = tf.global_variables_initializer() # 開啟會話 with tf.Session() as sess: sess.run(init) print(sess.run(c))
以上是“TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。