python - 神经网络在 tensorflow 中执行是什么意思

标签 python tensorflow

TensorFlow 图形 API 将图形构建和执行分开。因此,我无法理解神经网络在哪一行执行。

"""
- model_fn: function that performs the forward pass of the model
- init_fn: function that initializes the parameters of the model.
- learning_rate: the learning rate to use for SGD.
"""
tf.reset_default_graph()
is_training = tf.placeholder(tf.bool, name='is_training')

with tf.device(device):
    x = tf.placeholder(tf.float32, [None, 32, 32, 3])
    y = tf.placeholder(tf.int32, [None])
    params = init_fn()           # Initialize the model parameters
    scores = model_fn(x, params) # Forward pass of the model
    loss = training_step(scores, y, params, learning_rate) # SGD

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for t, (x_np, y_np) in enumerate(train_dset):

        feed_dict = {x: x_np, y: y_np}
        loss_np = sess.run(loss, feed_dict=feed_dict)

最佳答案

如 Tensorflow 文档中所述:( https://www.tensorflow.org/api_docs/python/tf/Session#run )

This method runs one "step" of TensorFlow computation, by running the necessary graph fragment to execute every Operation and evaluate every Tensor

在您的示例中,sess.run(tf.global_variables_initializer()) 运行创建所有权重和张量的初始化操作,loss_np = sess.run(loss, feed_dict=feed_dict ) 执行直到 loss 为止的所有操作。

我希望这能回答你的问题

关于python - 神经网络在 tensorflow 中执行是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406576/

相关文章:

python - 在Python中逐列比较两个大文本文件

python - pyparsing 不是嵌套列表......为什么?

python - 使用 for 循环在 1 个图中绘制多个图

python - 在JavaScript中使用RSA加密并在Python3中解密

Tensorflow shuffle_batch 速度

python - 在设计 Python API 时,抛出异常或返回 false/None 等是否更 Pythonic?

tensorflow - 在具有多个 Keras 模型的 TF2 自定义训练循环中应用梯度的正确方法

python - keras model.fit 函数打印的准确率与验证集还是训练集有关?

python - 在 Tensorflow 中添加 GPU Op

python - 计算张量后,如何将其显示为图像?