tensorflow2 0 維度變換

2021-10-25 15:57:50 字數 1431 閱讀 4938

a = tf.random.normal((4,28,28,3))

a_1 = tf.reshape(a,[4,784,3]).shape #1

a_1 = tf.reshape(a,[4,-1,3]).shape #和1等價

a_2 = tf.reshape(a,[4,784*3]).shape #2

a_2 = tf.reshape(a,[4,-1]).shape #和2等價

aprint (a_1)

print (a_2)

"""28*28轉為乙個維度

28*28*3轉為乙個維度

(4, 784, 3)

(4, 2352)

"""

a = tf.random.normal((4,28,28,3))

b =tf.transpose(a).shape #逆序輸出

c =tf.transpose(a,perm=[0,1,3,2]).shape #定義輸出順序

print (b)

print (c)

"""(3, 28, 28, 4)

(4, 28, 3, 28)

"""

a1 = tf.random.normal((4,35,8))

b1 = tf.expand_dims(a1,axis=0).shape

b2 = tf.expand_dims(a1,axis=3).shape

b3 = tf.expand_dims(a1,axis=-1).shape#倒數第乙個位置

b4 = tf.expand_dims(a1,axis=-4).shape#倒數第四個位置

print (b1)

print (b2)

print (b3)

print (b4)

"""(1, 4, 35, 8)

(4, 35, 8, 1)

(4, 35, 8, 1)

(1, 4, 35, 8)

"""

s = tf.squeeze(tf.zeros([1,2,1,1,4])).shape #減少所有維度為1的維度

r1 = tf.zeros([1,2,1,3])

s1 = tf.squeeze(r1,axis=0).shape #減少下標為0的維度

s2 = tf.squeeze(r1,axis=-2).shape #減少下標為-2的維度

print (s)

print (s1)

print (s2)

"""(2, 4)

(2, 1, 3)

(1, 2, 3)

"""

Tensorflow 2 0 梯度計算

tensorflow 提供了gradienttape函式,實現梯度的計算。gradienttape 意思為梯度帶 persistent 預設是false.如果是false,那麼gradient 函式最多只能呼叫一次。反之可以呼叫多次,watch accessed variables 預設值是true...

TensorFlow2 維度變換

目錄tensorflow2教程完整教程目錄 更有python go pytorch tensorflow 爬蟲 人工智慧教學等著你 import tensorflow as tfa tf.random.normal 4,28,28,3 a.shape,a.ndim tensorshape 4,28,...

tensorflow2 0視訊記憶體設定

遇到乙個問題 新買顯示卡視訊記憶體8g但是tensorflow執行的時候介面顯示只有約6.3g的視訊記憶體可用,如下圖 即限制了我的視訊記憶體,具體原因為什麼我也不知道,但原來的視訊記憶體小一些的顯示卡就沒有這個問題。目前的解決辦法是 官方文件解決 然後對應的中文部落格 總結一下,就是下面的兩個辦法...