gpt4 book ai didi

python - 有没有办法调试 TensorFlow 代码?另外,在解释代码时如何查看变量或对象中的内容?

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:39 24 4
gpt4 key购买 nike

运行 Python 代码时,调试和跟踪变量和对象值非常容易,但在 tensorflow 中,很难看到幕后发生的情况。我知道 tensorflow 在图表中工作,你必须运行 session 。在解释代码时是否有更简单的方法来查看这些值?我附上了下面的屏幕截图,您可以在其中跟踪每个变量值,但是在 tensorflow 中我无法做到这一点。我已经很累了,我在 session 中使用了 tf.print() 和 tf.eval() 。

Pycharm Showing Variables Values这是tensorflow的代码,我想查看Z3predict_op

的值
def model(X_train, Y_train, X_test, Y_test, learning_rate=0.01,


ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep results consistent (tensorflow seed)
seed = 3 # to keep results consistent (numpy seed)
(m, n_H0, n_W0, n_C0) = X_train.shape
n_y = Y_train.shape[1]
costs = [] # To keep track of the cost

# Create Placeholders of the correct shape
X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)

# Initialize parameters
parameters = initialize_parameters()

# Forward propagation: Build the forward propagation in the tensorflow graph
Z3 = forward_propagation(X, parameters)

# Cost function: Add cost function to tensorflow graph
cost = compute_cost(Z3, Y)


# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost.

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# Initialize all the variables globally
init = tf.global_variables_initializer()

# Start the session to compute the tensorflow graph
with tf.Session() as sess:

# Run the initialization
sess.run(init)

# Do the training loop
for epoch in range(num_epochs):

minibatch_cost = 0.
num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
seed = seed + 1
minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

for minibatch in minibatches:
# Select a minibatch
(minibatch_X, minibatch_Y) = minibatch
# IMPORTANT: The line that runs the graph on a minibatch.
# Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).

_, temp_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})


minibatch_cost += temp_cost / num_minibatches

# Print the cost every epoch
if print_cost == True and epoch % 5 == 0:
print("Cost after epoch %i: %f" % (epoch, minibatch_cost))
if print_cost == True and epoch % 1 == 0:
costs.append(minibatch_cost)



# Calculate the correct predictions
predict_op = tf.argmax(Z3, 1)
correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy)
train_accuracy = accuracy.eval({X: X_train, Y: Y_train})
test_accuracy = accuracy.eval({X: X_test, Y: Y_test})
print("Train Accuracy:", train_accuracy)
print("Test Accuracy:", test_accuracy)

return train_accuracy, test_accuracy, parameters

最佳答案

您可以尝试 TensorFlow Eager Execution,它允许您直接运行 TensorFlow 代码,而无需构建图表并在 session 期间运行它。它甚至在描述中说它可以更轻松地进行调试。

https://www.tensorflow.org/guide/eager

关于python - 有没有办法调试 TensorFlow 代码?另外,在解释代码时如何查看变量或对象中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com