0
我想得到可重現的結果,用於我的tensorflow運行。我試圖做到這一點的方法是建立numpy的和tensorflow種子:TensorFlow如何使結果重現`tf.nn.sampled_softmax_loss`
import numpy as np
rnd_seed = 1
np.random.seed(rnd_seed)
import tensorflow as tf
tf.set_random_seed(rnd_seed)
除了確保神經網絡的權重,我跟tf.truncated_normal
初始化也使用該種: tf.truncated_normal(..., seed=rnd_seed)
由於原因超出了這個問題的範圍,我使用了採樣的softmax損失函數tf.nn.sampled_softmax_loss
,不幸的是,我無法用隨機種子來控制此函數的隨機性。
通過查看此函數的TensorFlow文檔(https://www.tensorflow.org/api_docs/python/tf/nn/sampled_softmax_loss),我可以看到參數sampled_values
應該是影響隨機化的唯一參數,但我無法理解如何實際使用種子。
將帖子 這是(部分)我的腳本
import numpy as np
# set a seed so that the results are consistent
rnd_seed = 1
np.random.seed(rnd_seed)
import tensorflow as tf
tf.set_random_seed(rnd_seed)
embeddings_ini = np.random.uniform(low=-1, high=1, size=(self.vocabulary_size, self.embedding_size))
with graph.as_default(), tf.device('/cpu:0'):
train_dataset = tf.placeholder(tf.int32, shape=[None, None])
train_labels = tf.placeholder(tf.int32, shape=[None, 1])
valid_dataset = tf.constant(self.valid_examples, dtype=tf.int32)
# Variables.
initial_embeddings = tf.placeholder(tf.float32, shape=(self.vocabulary_size, self.embedding_size))
embeddings = tf.Variable(initial_embeddings)
softmax_weights = tf.Variable(
tf.truncated_normal([self.vocabulary_size, self.embedding_size],
stddev=1.0/math.sqrt(self.embedding_size), seed=rnd_seed))
softmax_biases = tf.Variable(tf.zeros([self.vocabulary_size]))
# Model.
# Look up embeddings for inputs.
if self.model == "skipgrams":
# Skipgram model
embed = tf.nn.embedding_lookup(embeddings, train_dataset)
elif self.model == "cbow":
# CBOW Model
embeds = tf.nn.embedding_lookup(embeddings, train_dataset)
embed = tf.reduce_mean(embeds, 1, keep_dims=False)
# Compute the softmax loss, using a sample of the negative labels each time.
loss = tf.reduce_mean(tf.nn.sampled_softmax_loss(weights=softmax_weights,
biases=softmax_biases,
inputs=embed,
labels=train_labels,
num_sampled=self.num_sampled,
num_classes=self.vocabulary_size))
你能提供完整的劇本,所以我們可以看到在不受控制的隨機性可能的來源可能是什麼? – Anis
'sampled_values'參數的思想是你傳遞'* _candidate_sampler'函數之一的輸出(你可以在這裏查找它們)(https://www.tensorflow.org/api_docs/python/tf/ nn),儘管它們沒有分成共同的部分或任何東西)。但是如果你使用['tf.set_random_seed'](https://www.tensorflow.org/api_docs/python/tf/set_random_seed),即使你沒有通過,它也應該是可重現的。你能確認你[設置圖中的種子](https://stackoverflow.com/questions/36288235/how-to-get-stable-results-with-tensorflow-setting-random-seed)? – jdehesa
我同意。 '_compute_sampled_logits'不會將任何種子傳遞給候選樣本,所以這一切都歸結爲您的圖的種子。 – Anis