python - 是我的神经网络在每次迭代中设置随机权重吗?
问题描述
我已经定义了以下创建我的 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。如果这是真的,我如何创建网络并使其在整个培训过程中保持不变?
请记住,我试图尽可能地简化代码
解决方案
推荐阅读
- php - Openfigi php 请求 -> 请求正文必须是 JSON 数组
- c# - Specflow 特征文件不使用步骤定义
- mysql - 如何选择应用 IS NULL 的最佳列?Leetcode 数据库 1350
- javascript - 根据数字创建长度的数组
- r - 计算行中值的频率(计算排名表中的第一、第二和第三名的数量)
- html - 更改图层下方的文本颜色
- azure - 如何获取 Azure 订阅经典管理员的 AD 用户?
- javascript - Prettier VS Code - 如何格式化 JS 对象
- python - Python反应式编程条件执行
- java - springframework 启动测试上下文不存在