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/