亂七八糟的tf

2021-09-26 10:38:08 字數 1995 閱讀 2632

my_state = tf.variable(0, name = "counter") #建立乙個op變數my_state,並初始化為0

one = tf.constant(1) #建立乙個op常量賦值為1

new_value = tf.add(my_state, one)

update = tf.assign(my_state, new_value) #通過assign()函式,將new_value的值賦給my_state.

init_op = tf.global_variables_initializer() #tf.global_variables_initializer()

#會返回乙個操作,初始化計算圖中所有variable物件

variable_names = [v.name for v in tf.trainable_variables()]

print(variable_names)

.split('.')[-1]

#[-1]代表把最後一塊切出來

import matplotlib.pyplot as plt  

import tensorflow as tf

import numpy as np

image_raw_data = tf.gfile.gfile('c:/users/dell/desktop/cwtpic/train1.jpg','r').read() #載入原始影象

with tf.session() as sess:

img_data = tf.image.decode_jpeg(image_raw_data) #解碼

plt.imshow(img_data.eval())

plt.show()

resized = tf.image.resize_images(img_data, [64,64],method=0) #第乙個引數為原始影象,第二個引數為影象大小,第三個引數給出了指定的演算法

resized = np.asarray(resized.eval(),dtype='uint8') #變為uint8才能顯示

plt.imshow(resized)

plt.show()

對於使用tf.variable來說,tf.name_scope和tf.variable_scope功能一樣,都是給變數加字首,相當於分類管理,模組化。

對於tf.get_variable來說,tf.name_scope對其無效,也就是說tf認為當你使用tf.get_variable時,你只歸屬於tf.variable_scope來管理共享與否。

來看乙個例子:

with tf.name_scope('name_sp1') as scp1:

with tf.variable_scope('var_scp2') as scp2:

with tf.name_scope('name_scp3') as scp3:

a = tf.variable('a')

b = tf.get_variable('b')

等同於with tf.name_scope('name_sp1') as scp1:

with tf.name_scope('name_sp2') as scp2:

with tf.name_scope('name_scp3') as scp3:

a = tf.variable('a')

with tf.variable_scope('var_scp2') as scp2:

b = tf.get_variable('b')

在乙個sess.run()環境中,乙個大圖裡面的節點只會計算一次,哪怕有c->a->b這種依賴關係,執行sess.run([a,b])這種時候,a也只會被執行一次。其內部的機制是:先檢視圖中的依賴關係,得到本次sess.run()需要計算圖中那些節點,然後進行一次計算,返回計算的a,b的值。

c 亂七八糟

大綱 變數和基本型別 常量 表示式,各種操作符等 控制語句 強制型別轉換 異常處理 函式 標準輸入輸出類 類,封裝性,繼承性,多型性 標準類 泛型程式設計,類模板,函式模板 標準容器類,泛型演算法 類是什麼,類是對具有相同特性以及行為的一類物件的高度歸納,類是一種抽象資料型別 快慢指標 多個指標,前...

亂七八糟的命令

man ls col b ls.txt whatis du cd 前一目錄 cd 直接回家 tree makir p home dir1 dir2 dir3 head 5 etc passwd tail sudo etc init.d apache2 stop 檔案預設不能授予x許可權,預設644 ...

亂七八糟記一下亂七八糟的碎片化知識

from mdn 對於所有情況都是true,除非屬性是乙個自身的不可配置的屬性,在這種情況下,非嚴格模式返回false。在嚴格模式下,如果是屬性是乙個自己不可配置的屬性,會丟擲typeerror。檢查分支,確定分支 提交到緩衝區 git add 將緩衝區內容提交到本地倉庫 git commit m ...