我想通过实验在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'有什么不同? –