Tensorflow摘要指标未初始化(与Tensorboard一起使用)

时间:2017-08-09 11:41:58

标签: python machine-learning tensorflow data-science tensorboard

我正在尝试记录精度和召回的汇总统计数据,并使用张量流与张量板一起使用以下代码。

我已经添加了全局变量和局部变量初始值设定项,但是这仍然会导致错误,告诉我我有一个未初始化的值来回忆'。

有没有人知道为什么这仍然会引发错误?

错误消息在代码块

下面
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')

这表明精确张量与其他张量的形状不同,它不会引起交叉熵或召回的错误。

2 个答案:

答案 0 :(得分:2)

第二个问题是由>>> int('0b100', base=0) 4 返回两个张量的事实引起的。相反,做

# There's no need to import sys; you never use it

#binary to decimal

# Your original program do the same thing in Python 2 and 3
print("Value:")

digits=input()
print(int(digits, 2))

答案 1 :(得分:0)

由于两个初始化行的放置,将这两行移动到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)