Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何手动初始化权重值?_Python 2.7_Neural Network_Tensorflow - Fatal编程技术网

Python 2.7 如何手动初始化权重值?

Python 2.7 如何手动初始化权重值?,python-2.7,neural-network,tensorflow,Python 2.7,Neural Network,Tensorflow,我想尝试一下Karpath在课堂讲稿中推荐的权重初始化 推荐的启发式方法是初始化每个神经元的权重向量 as:w=np.random.randn(n)/sqrt(n),其中n是其 投入 资料来源: 我是python初学者,不知道如何实现:/ weights = tf.Variable(??) 请帮助?..对于单个值,请使用: weights = tf.Variable(10) 对于具有随机值的向量: shape = [784, 625] weights = tf.Variable(tf.ran

我想尝试一下Karpath在课堂讲稿中推荐的权重初始化

推荐的启发式方法是初始化每个神经元的权重向量 as:w=np.random.randn(n)/sqrt(n),其中n是其 投入

资料来源:

我是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来计算变量


另外,请查看其他随机张量:

我按以下方式执行:

    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)
                )
            )
之后


您将有[n_输入,第一个FC层大小,第二个FC层大小…输出层大小]

非常感谢您的回复。我不明白
np.random.randn(n)在哪里
在您显示的代码行中。我想我不想使用带有标准偏差参数的
tf.random\u normal
,而是使用
np.random.randn(n)
手动设置权重矩阵的每个权重。这可以实现吗?我会使用tf.random.X。您可以替换np.random.randn(n)使用tf.random,做同样的事情。请检查。@kalani我只是好奇,你认为
randn
有什么不同于
random\u normal
?@AlexI只有在
np.random.randn(n)
中,
n
出现在tf.random\u normal(shape,stddev=0.01),
n
没有出现。另外,我必须确定一个值作为标准偏差。我在某个地方错了吗?(同样,我是非常初学者,所以欢迎任何帮助我理解的解释)@Kalanit:
np.random.randn(shape)*0.01
tf.random\u normal(shape,stddev=0.01)相同
.randn生成stddev=1的数字,但您可以通过乘法得到所需的任何stdev。除此之外,它们完全相同。
    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)