我想通過實驗在Karpathy他的講義中建議的權重初始化,如何手動初始化權值?
推薦的啓發式是將每個神經元的權重向量 初始化爲:W = np.random.randn(N)/ SQRT( n),其中n是其 輸入數量
來源:http://cs231n.github.io/neural-networks-2/#init
我在python初學者,我不「知道如何實現這一點:/
weights = tf.Variable(??)
請幫忙? ...
我想通過實驗在Karpathy他的講義中建議的權重初始化,如何手動初始化權值?
推薦的啓發式是將每個神經元的權重向量 初始化爲:W = np.random.randn(N)/ SQRT( n),其中n是其 輸入數量
來源:http://cs231n.github.io/neural-networks-2/#init
我在python初學者,我不「知道如何實現這一點:/
weights = tf.Variable(??)
請幫忙? ...
對於單個值,使用:
weights = tf.Variable(10)
對於隨機值向量:
shape = [784, 625]
weights = tf.Variable(tf.random_normal(shape, stddev=0.01)/tf.sqrt(n))
請注意,你需要sess.run評估的變量。
另外,請查看其它隨機張量:https://www.tensorflow.org/versions/r0.8/api_docs/python/constant_op.html#random-tensors
n = 10
init_x = np.random.randn(n)
x = tf.Variable(init_x)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print(sess.run(x))
我這樣做以下列方式:
self.w_full, self.b_full = [], []
n_fc_layers = len(structure)
structure.insert(0, self.n_inputs)
with vs.variable_scope(self.scope):
for lr_idx in range(n_fc_layers):
n_in, n_out = structure[lr_idx], structure[lr_idx+1]
self.w_full.append(
vs.get_variable(
"FullWeights{}".format(lr_idx),
[n_in, n_out],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(
minval=-tf.sqrt(tf.constant(6.0)/(n_in + n_out)),
maxval=tf.sqrt(tf.constant(6.0)/(n_in + n_out))
)
)
)
self.b_full.append(
vs.get_variable(
"FullBiases{}".format(lr_idx),
[n_out],
dtype=tf.float32,
initializer=tf.constant_initializer(0.0)
)
)
後
structure.insert(0, self.n_inputs)
你有[n_inputs ,第一FC層大小,第二FC層大小...輸出層大小]
非常感謝您的回覆。我不明白你展示的代碼行中的'np.random.randn(n)'在哪裏。我想我不想在標準偏差參數中使用'tf.random_normal',但是用'np.random.randn(n)'手動設置權重矩陣的每個權重。這可以實現嗎? – Kalanit
我會使用tf.random.X。你可以用tf.random替換np.random.randn(n),並且做同樣的事情。請查看https://www.tensorflow.org/versions/r0.8/api_docs/python/constant_op.html#random-tensors。 –
@Kalanit只是好奇,你認爲'randn'與'random_normal'有什麼不同? –