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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

tensorflow随机性设置

發(fā)布時間:2025/3/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow随机性设置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

案例一:?

import tensorflow as tf tf.set_random_seed(42) sess = tf.InteractiveSession() a = tf.constant([1, 2, 3, 4, 5]) tf.initialize_all_variables().run() a_shuf = tf.random_shuffle(a) print(a.eval()) print(a_shuf.eval()) sess.close()

上述代碼重復運行會產生不一樣的結果

?

import tensorflow as tf tf.set_random_seed(42) sess = tf.InteractiveSession() a = tf.constant([1, 2, 3, 4, 5]) tf.initialize_all_variables().run() a_shuf = tf.random_shuffle(a,seed=42) print(a.eval()) print(a_shuf.eval()) sess.close()

這段代碼產生相同的結果

?

案例二:
?

# using global seed tf.set_random_seed(1) fc1_W = tf.Variable(tf.truncated_normal(shape=(2, 2), mean=0, stddev=1)) fc2_W = tf.Variable(tf.truncated_normal(shape=(2, 2), mean=0, stddev=1))# initialize session with tf.Session() as sess:sess.run(tf.global_variables_initializer())print('\n variables with global seed ')print('round 2.0')print(fc1_W.eval(sess))print(fc2_W.eval(sess))# new session with tf.Session() as sess:sess.run(tf.global_variables_initializer())print('round 2.1')print(fc1_W.eval(sess))print(fc2_W.eval(sess))

第一次運行:
?variables with global seed?
round 2.0
[[-0.89710885 ?0.39287093]
?[ 0.4009913 ?-1.9170585 ]]
[[ 1.2090957 ?-0.13654923]
?[ 1.6384401 ?-0.18959242]]
round 2.1
[[-0.89710885 ?0.39287093]
?[ 0.4009913 ?-1.9170585 ]]
[[ 1.2090957 ?-0.13654923]
?[ 1.6384401 ?-0.18959242]]
第一次與第二次不斷的創(chuàng)建圖,其結果是相同的

第二次運行:
variables with global seed?
round 2.0
[[ 1.1742289 ? 0.03763932]
?[ 0.7202809 ?-0.52002007]]
[[ 1.5818138 ? 0.81615436]
?[ 1.2647419 ?-0.6432518 ]]
round 2.1
[[ 1.1742289 ? 0.03763932]
?[ 0.7202809 ?-0.52002007]]
[[ 1.5818138 ? 0.81615436]
?[ 1.2647419 ?-0.6432518 ]]

第一次運行與第二次運行結果不相同;

結論:只靠tf.set_random_seed是沒有辦法做到消除隨機性 ,該函數(shù)可以在一次運行中,創(chuàng)建的多個圖的結果是一樣的; 因為從本質上來講,operation-level的初始值是一樣的(因為初始化一次被多個圖使用),而graph-level的隨機種子相同,所以對于不同的圖而言,不存在隨機性;

隨機性由兩個隨機種子確認(Operations that rely on a random seed actually derive it from two seeds: the graph-level and operation-level seeds來自 于tensorflow官網(wǎng))

?

總結

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

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