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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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