首页 > 解决方案 > 是我的神经网络在每次迭代中设置随机权重吗?

问题描述

我已经定义了以下创建我的 NN 的函数:

def nn_layer(input_tensor, output_dim, layer_name, act=tf.nn.relu):
    # Adding a name scope ensures logical grouping of the layers in the graph.
    with tf.name_scope(layer_name):
        # This Variable will hold the state of the weights for the layer
        with tf.name_scope('weights'):
            weights = weight_variable([input_tensor.get_shape().as_list()[1], output_dim])
            variable_summaries(weights)
        with tf.name_scope('biases'):
            biases = bias_variable([output_dim])
            variable_summaries(biases)
        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            tf.summary.histogram('pre_activations', preactivate)
        activations = act(preactivate, name='activation')
        tf.summary.histogram('activations', activations)

        return activations


def neural_net(features):
    ''' Model function for the NN '''
    input_layer = tf.cast(features, tf.float32)

    hidden_layer = nn_layer(input_layer, 2, 'hidden_layer', act=tf.nn.relu)

    out_layer = nn_layer(hidden_layer, 2, 'out_layer', act=tf.nn.relu)

    return out_layer

然后我可以计算 logits 值:

logits = neural_net(x)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))
train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)
sess = tf.Session()

for epoch in range(epochs):
    loss,_ = sess.run([loss, train_step], feed_dict={x: x})

我想知道这是否会在我每次调用 run 方法时创建一个新的 NN。如果这是真的,我如何创建网络并使其在整个培训过程中保持不变?

请记住,我试图尽可能地简化代码

标签: pythontensorflowneural-network

解决方案


推荐阅读