张量流标量摘要标签名称异常 [英] tensorflow scalar summary tags name exception

查看:34
本文介绍了张量流标量摘要标签名称异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 HowTo mnist 教程学习如何使用 tensorflow 摘要编写器.该教程为损失函数添加了标量摘要.我通过建立正则化项以不寻常的方式编写了一个损失函数,但我得到了这个例外:

I am trying to learn how to work with tensorflow summary writers by following the HowTo mnist tutorial. That tutorial adds a scalar summary for the loss function. I wrote a loss function in an unusual by building up a regularization term, and I get this exception:

W tensorflow/core/common_runtime/executor.cc:1027] 0x1e9ab70 Compute status: Invalid argument: tags and values not the same shape: [] != [1]
     [[Node: ScalarSummary = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](ScalarSummary/tags, loss)]]

损失函数和添加摘要看起来像

The loss function and adding the summary look like

loss = tf.add(modelError, regularizationTerm, name='loss')
tf.scalar_summary(loss.op.name, loss)

如果我像这样建立正则化项

and if I build up the regularizationTerm like this

regularizationTerm = tf.Variable(tf.zeros([1], dtype=np.float32), name='regterm')
regularizationTerm +=  tf.mul(2.0, regA)
regularizationTerm +=  tf.mul(3.0, regB)

regA 和 regB 是之前定义的 tf.Variables,我得到了异常,而我像

were regA and regB are tf.Variables previously defined, I get the exception, whereas is I build it up like

regularizationTerm = tf.add(tf.mul(2.0, regA), tf.mul(3.0, regB), name='regterm')

然后就可以了.所以我想我没有正确设置名称,当我执行 += 时,我创建了一个未命名的新张量?但是为什么我不能将其添加到损失中,然后命名损失?这是我唯一要总结的事情吗?

then it works. So I guess I am not setting the name correctly, when I do the += I create a new tensor that is unamed? But why can't I add that into the loss, and then name the loss? That is the only thing I am trying to summarize?

是否有类似 += 之类的东西,我可以在其中命名输出,或者保留我正在修改的张量的名称?

Is there something like += where I can name the output, or preserve the name of the tensor I am modifying?

如果问题与其他问题有关,以下是我确定问题的简单示例:

In case the issue is related to something else, here is my simple example where I identified the problem:

import numpy as np
import tensorflow as tf

def main():
    x_input = tf.placeholder(tf.float32, shape=(None, 1))
    y_output = tf.placeholder(tf.float32, shape=(None, 1))

    hidden_weights = tf.Variable(tf.truncated_normal([1,10], stddev=0.1), name='weights')
    output_weights = tf.Variable(tf.truncated_normal([10,1], stddev=0.1), name='output')
    inference = tf.matmul(tf.matmul(x_input, hidden_weights), output_weights)
    regA = tf.reduce_sum(tf.pow(hidden_weights, 2))
    regB = tf.reduce_sum(tf.pow(output_weights, 2))
    modelError = tf.reduce_mean(tf.pow(tf.sub(inference,y_output),2), name='model-error')

    fail = True
    if fail:
        regularizationTerm = tf.Variable(tf.zeros([1], dtype=np.float32), name='regterm')
        regularizationTerm +=  tf.mul(2.0, regA)
        regularizationTerm +=  tf.mul(3.0, regB)
    else:
        regularizationTerm = tf.add(tf.mul(2.0, regA), tf.mul(3.0, regB), name='regterm')

    loss = tf.add(modelError, regularizationTerm, name='loss')
    tf.scalar_summary(loss.op.name, loss)
    optimizer = tf.train.GradientDescentOptimizer(0.05)
    global_step = tf.Variable(0, name='global_step', trainable=False)
    train_op = optimizer.minimize(loss, global_step=global_step)

    summary_op = tf.merge_all_summaries()

    saver = tf.train.Saver()

    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)

    summary_writer = tf.train.SummaryWriter('train_dir', 
                                            graph_def=sess.graph_def)

    feed_dict = {x_input:np.ones((30,1), dtype=np.float32),
                 y_output:np.ones((30,1), dtype=np.float32)}

    for step in xrange(1000):
        _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
        if step % 100 == 0:
            print( "step=%d loss=%.2f" % (step, loss_value))
            summary_str = sess.run(summary_op, feed_dict=feed_dict)
            summary_writer.add_summary(summary_str, step)

if __name__ == '__main__':
    main()

推荐答案

TL;DR: 问题在于 tf.scalar_summary(),而不是名称.

TL;DR: The problem is the shape of the argument to tf.scalar_summary(), not the names.

我认为问题是与形状相关的问题,源于这一行:

I think the problem is a shape-related issue, stemming from this line:

regularizationTerm = tf.Variable(tf.zeros([1], dtype=np.float32), name='regterm')

这定义了一个变量,其形状是长度为 1 的向量.随后的 += 运算符(它们是 tf.add()) 和 tf.add() 计算 loss 将产生矢量形状的结果,因为 tf.add() 广播 标量参数成为向量.最后,tf.scalar_summary() 期望它的两个参数具有相同的形状——与广播 add 不同,tf.scalar_summary() 对其输入的形状不宽容.tags 输入是一个标量字符串(loss 操作的名称),而 values 输入是一个长度为 1 的向量(loss 张量).因此,您会收到您报告的错误.

This defines a variable whose shape is a vector of length 1. The subsequent += operators (which are syntactic sugar for tf.add()) and the tf.add() to compute loss will produce vector-shaped results, because tf.add() broadcasts the scalar argument to become a vector. Finally, tf.scalar_summary() expects its two arguments to have the same shape—unlike the broadcasting add, tf.scalar_summary() is not permissive about the shapes of its inputs. The tags input is a scalar string (the name of the loss op) whereas the values input is a vector of length one (the value of the loss tensor). Therefore you get the error that you reported.

幸运的是,解决方案很简单!将 regularizationTerm 变量定义为标量,如下所示:

Fortunately, the solution is simple! Either define the regularizationTerm variable as a scalar, like so:

# Note that `[]` is the scalar shape.
regularizationTerm = tf.Variable(tf.zeros([], dtype=np.float32), name='regterm')

...或将字符串向量(长度为 1)传递给 tf.scalar_summary():

...or pass a vector (of length 1) of strings to tf.scalar_summary():

# Wrap `loss.op.name` in a list to make it a vector.
tf.scalar_summary([loss.op.name], loss)

这篇关于张量流标量摘要标签名称异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆