你好,我是TensorBoard和tf.metrics.accuracy()的新手
(我是中国人,所以我的英语可能不太好,我会尝试描述我的问题)
为方便起见,我只写关键代码 现在我遇到了一个问题,即每个纪元都将火车和val精度保存到TensorBoard,而且我的数据量很大,所以我使用了批处理数据。
我完成的是:
1)获取数据集
现在,我使用
train_iterator = train_dataset.make_initializable_iterator()
val_iterator = val_dataset.make_initializable_iterator()
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(handle, train_iterator.output_types,
train_iterator.output_shapes)
image_batch, label_batch = iterator.get_next()
获取数据集,我可以使用tp.Session()在两个数据集之间切换
sess.run([train_iterator.initializer, accuracy_vars_initializer])
和
sess.run([val_iterator.initializer, accuracy_vars_initializer])
2)计算准确性
with tf.name_scope("meters"):
accuracy, accuracy_op = tf.metrics.accuracy(labels=label_batch,
predictions=tf.argmax(tf.nn.softmax(logits_batch), -1),
name="accuracy")
accuracy_value_ = tf.placeholder(tf.float32, shape=())
accuracy_summary = tf.summary.scalar('accuracy', accuracy_value_)
accuracy_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="meters/accuracy")
accuracy_vars_initializer = tf.variables_initializer(var_list=accuracy_vars)
我使用
sess.run(accuracy_vars_initializer)
在整个火车/ val数据集的火车/ val过程之前,以在tf.metrics.accuracy()内设置内部计数值 那我想用
for epoch_i in range(FLAGS.epoch):
sess.run([train_iterator.initializer, accuracy_vars_initializer])
train_loss_avg, train_acc_avg = [], []
while True:
try:
_, loss_value, step, acc_value, acc_op_value, summary = sess.run(
[train_op, loss, global_step, accuracy, accuracy_op, merged],
feed_dict={handle: train_iterator_handle,
accuracy_value_: np.average(train_acc_avg),
loss_value_: np.average(train_loss_avg)})
train_acc_avg.append(acc_value)
train_loss_avg.append(loss_value)
except tf.errors.OutOfRangeError:
train_writer.add_summary(summary, global_step=step)
saver.save(sess, os.path.join(FLAGS.model_dir, "fcn8.ckpt"), global_step)
print("train dataset finished")
break
sess.run([val_iterator.initializer, accuracy_vars_initializer])
val_loss_avg = []
while True:
try:
loss_value, acc_value, acc_op_value, summary = sess.run(
[loss, accuracy, accuracy_op, merged], feed_dict={handle: val_iterator_handle,
accuracy_value_: acc_op_value,
loss_value_: np.average(val_loss_avg)})
print("Epoch[%d],val batch loss = %g,acc = %g." % (epoch_i, loss_value, acc_value))
val_loss_avg.append(loss_value)
except tf.errors.OutOfRangeError:
val_writer.add_summary(summary, global_step=step)
print("val dataset finished")
break
train_writer.close()
val_writer.close()
实现我的目标。
我之前使用的精度计算方法很简单
feed_dict={handle: train_iterator_handle,
accuracy_value_: accuracy_op,
loss_value_: np.average(train_loss_avg)})
但是新旧方法都将在TensorBoard中产生水平精度线。而且我多次改进了代码,但问题仍然存在
有人可以帮我找到原因吗?有没有更好的,标准化的方法来组织代码?因为现在太复杂了。
非常感谢您的帮助。