我正在尝试使用tensor-board和下面的代码来记录精度和召回的tensorflow汇总统计信息。Tensorflow概要指标未初始化(用于Tensorboard)
我已经添加了全局和局部变量初始值设定项,但是这仍然会引发一个错误,告诉我我有一个未初始化的'recall'值。
有没有人有任何想法,为什么这仍然抛出一个错误?
错误消息是码块
def classifier_graph(x, y, learning_rate=0.1):
with tf.name_scope('classifier'):
with tf.name_scope('model'):
W = tf.Variable(tf.zeros([xdim, ydim]), name='W')
b = tf.Variable(tf.zeros([ydim]), name='b')
y_ = tf.matmul(x, W) + b
with tf.name_scope('cross_entropy'):
diff = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_)
cross_entropy = tf.reduce_mean(diff)
summary = tf.summary.scalar('cross_entropy', cross_entropy)
with tf.name_scope('train'):
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_), reduction_indices=[1]), name='cross_entropy')
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)
# minimise cross_entropy via GD
#with tf.name_scope('init'):
#init = tf.global_variables_initializer()
#local_init = tf.local_variables_initializer()
#init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
with tf.name_scope('init'):
init = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()
with tf.name_scope('metrics'):
recall = tf.metrics.recall(y, y_)
precision = tf.metrics.precision(y, y_)
v_rec = tf.summary.scalar('recall', recall)
v_prec = tf.summary.scalar('precision', precision)
metrics = tf.summary.merge_all()
return [W, b, y_, cross_entropy, train_step, init, init_l, metrics]
def train_classifier(insamples, outsamples, batch_size, iterations, feature_set_index=1, model=None, device):
x = tf.placeholder(tf.float32, [None, xdim], name='x') # None indications arbitrary first dimension
y = tf.placeholder(tf.float32, [None, ydim], name='y')
W, b, y_, cross_entropy, train_step, init, init_l, metrics = classifier_graph(x, y)
with tf.Session(config=config) as sess, tf.device(device):
sess.run(init)
sess.run(init_l)
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
t = 0
while t < iterations:
t += 1
_, err, metrics_str = sess.run([train_step, cross_entropy, metrics], feed_dict={x: batch_x, y: batch_y })
all_err.append(err)
file_writer.add_summary(metrics_str,t)
return 'Done'
确切的错误信息以下的是:
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value recall/true_positives/count
[[Node: recall/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@recall/true_positives/count"], _device="/job:localhost/replica:0/task:0/gpu:0"](recall/true_positives/count)]]
谢谢!
编辑:
在作出由下面@Ishant Mrinal建议的修改,我遇到我先前被击中一个错误:
InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [2] (tag 'precision_1')
这表明精度张量是不同的形状对其他人来说,它不会为交叉熵或回忆而抛出这个错误。
谢谢,我过去曾尝试这样做,这将引发一个奇怪的错误: 'InvalidArgumentError(见上文回溯):标记和值不一样的形状:[] = [ 2](tag'precision_1')' 您是否知道为什么标签和值仅在精度标签上具有不同的维度?它没有抛出交叉熵或回忆的错误。 –
该问题与'tf.summary.scalar'有关。 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/summary_op.cc#L46 –