Tensorflow摘要指标未初始化(用于Tensorboard)

我正在尝试使用tensorflow记录精确度和召回率的摘要统计数据,以便与tensor-board一起使用,代码如下所示。

我已经添加了全局和本地变量初始化器,但仍然抛出错误,告诉我’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')

这表明精确度张量的形状与其他不同,对于交叉熵或召回率不会抛出此错误。


回答:

这是因为两个初始化行的位置,将这两行移动到train_classifier函数中。错误表明某些变量未初始化。

def train_classifier(...):    ...    init = tf.global_variables_initializer()    init_l = tf.local_variables_initializer()    with tf.Session(config=config) as sess, tf.device(device):        sess.run(init)        sess.run(init_l)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注