日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tensorflow随笔-collection收集器

發布時間:2025/3/12 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow随笔-collection收集器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

collection收集器的作用在于,可以在計算圖運行過程中保存需要的值。

get_collection得到collection中關于key的內容,返回值是一個列表。

add_to_collection將值增加到collection中,并與name關聯

add_to_collections將一個值放入多個collection中

tf.get_collection(
key,
scope=None
)
tf.add_to_collection(
name,
value
)

#!/usr/bin/env python2 # -*- coding: utf-8 -*-import tensorflow as tf a = tf.constant([[1., 2.], [3., 4.]]) b = a+1 c= b+2tf.add_to_collection("my_constant",a) tf.add_to_collection("my_constant",b) tf.add_to_collection("my_constant",c)const_vals=tf.get_collection("my_constant") res=tf.add_n([a,b,c]) sess=tf.Session() with sess:print sess.run(res)print sess.run(const_vals)

[[ 7. 10.]
[13. 16.]]
[array([[1., 2.],
[3., 4.]], dtype=float32), array([[2., 3.],
[4., 5.]], dtype=float32), array([[4., 5.],
[6., 7.]], dtype=float32)]
記住,上次跑程序已經保存的值不會被自動刪除,值會累計保存,直到python進程退出。否則,再跑一次上面的程序,則

[[ 7. 10.]
[13. 16.]]
[array([[1., 2.],
[3., 4.]], dtype=float32), array([[2., 3.],
[4., 5.]], dtype=float32), array([[4., 5.],
[6., 7.]], dtype=float32), array([[1., 2.],
[3., 4.]], dtype=float32), array([[2., 3.],
[4., 5.]], dtype=float32), array([[4., 5.],
[6., 7.]], dtype=float32)]

#!/usr/bin/env python2 # -*- coding: utf-8 -*-import tensorflow as tf a = tf.constant([[1., 2.], [3., 4.]])tf.add_to_collections(["my_con1","my_con2"],a)const_1=tf.get_collection("my_con1") const_2=tf.get_collection("my_con2")sess=tf.Session() with sess:print sess.run(const_1)print sess.run(const_2)

[array([[1., 2.],
[3., 4.]], dtype=float32)]
[array([[1., 2.],
[3., 4.]], dtype=float32)]

總結

以上是生活随笔為你收集整理的tensorflow随笔-collection收集器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。