gpt4 book ai didi

python - 张量板上的损失函数

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

我是 tensorflow 新手,目前我正在与张量板斗争。我想在张量板上显示损失函数,但我得到的只是一个空白的张量板。事件文件已创建,我也可以打开张量板。以下是我的代码,非常感谢任何帮助。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


import numpy as np
import tensorflow as tf

I_TRAIN = "D:/./iris_training.csv"
I_TEST = "D:/./iristest.csv"

sess = tf.Session()
train_data = tf.contrib.learn.datasets.base.load_csv_with_header(filename=I_TRAIN,
target_dtype=np.int,features_dtype=np.float32)

test_data = tf.contrib.learn.datasets.base.load_csv_with_header(filename=I_TEST,
target_dtype=np.int,features_dtype=np.float32)

explainer = [tf.contrib.layers.real_valued_column("", dimension=4)]

z = tf.contrib.learn.DNNClassifier(feature_columns=explainer,hidden_units=[5, 10, 5],
n_classes=3,model_dir="/tmp/iris_neural2")

z.fit(x=train_data.data,y=train_data.target,steps=5000)
accuracy = z.evaluate(x=test_data.data,y=test_data.target)["accuracy"]
#tf.summary.scalar('accuracy',accuracy)
loss = z.evaluate(x=test_data.data,y=test_data.target)["loss"]
tf.summary.scalar('loss', loss)
#merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('D:/irs_py',sess.graph)


print("\nThe accuracy of the model is ", accuracy)
print ("\nThe loss of the model is ",loss)

Tensorboard

代码输出:

INFO:tensorflow:Saving dict for global step 85000: accuracy = 0.966667, global_step = 85000, loss = 0.265843

The accuracy of the model is 0.966667

The loss of the model is 0.265843

最佳答案

这里是:

假设损失和准确度是您想要获得的两个图表:

#Model network code

loss = z.evaluate(x=test_data.data,y=test_data.target)["loss"]
accuracy = z.evaluate(x=test_data.data,y=test_data.target)["accuracy"]

# Add summary scalar node
acc_summ = tf.summary.scalar('accuracy',accuracy)
loss_summ = tf.summary.scalar('accuracy',loss)

#Collect all summaries
merged_summary = tf.summary.merge([acc_summ, loss_summ])]

让我们看看运行 session 并将图表写入文件的代码:

summ_writer = tf.summary.FileWriter(dir_path)

# session run (To be put inside batch-loop to log loss for each batch)
summary = session.run([merged_summary], feed_dict='your input')
summ_writer.add_summary(summary, global_step)

# Finally close the writer
summ_writer.close()

关于python - 张量板上的损失函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48556725/

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