【问题标题】:writing a custom cost function in tensorflow在 tensorflow 中编写自定义成本函数
【发布时间】:2016-02-14 13:04:17
【问题描述】:

我正在尝试在张量流中编写自己的成本函数,但显然我无法“切片”张量对象?

import tensorflow as tf
import numpy as np

# Establish variables
x = tf.placeholder("float", [None, 3])
W = tf.Variable(tf.zeros([3,6]))
b = tf.Variable(tf.zeros([6]))

# Establish model
y = tf.nn.softmax(tf.matmul(x,W) + b)

# Truth
y_ = tf.placeholder("float", [None,6])

def angle(v1, v2):
  return np.arccos(np.sum(v1*v2,axis=1))

def normVec(y):
  return np.cross(y[:,[0,2,4]],y[:,[1,3,5]])

angle_distance = -tf.reduce_sum(angle(normVec(y_),normVec(y)))
# This is the example code they give for cross entropy
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

我收到以下错误: TypeError: Bad slice index [0, 2, 4] of type <type 'list'>

【问题讨论】:

    标签: numpy tensorflow


    【解决方案1】:

    目前tensorflowcan't gather on axes other than the first - it's requested.

    但是对于你在这种特定情况下想要做的事情,你可以转置,然后收集 0,2,4,然后转置回来。它不会很快,但它确实有效:

    tf.transpose(tf.gather(tf.transpose(y), [0,2,4]))
    

    这是一个有用的解决方法,可以解决当前收集实现中的一些限制。

    (但您不能在 tensorflow 节点上使用 numpy 切片也是正确的 - 您可以运行它并对输出进行切片,而且您需要在运行之前初始化这些变量。:)。您正在以一种行不通的方式混合 tf 和 np。

    x = tf.Something(...)
    

    是一个张量流图对象。 Numpy 不知道如何处理这些对象。

    foo = tf.run(x)
    

    回到python可以处理的对象。

    您通常希望将损失计算保持在纯 tensorflow 中,因此 tf.cross 和其他函数也是如此。你可能不得不做很长的路要走 arccos,因为 tf 没有它的功能。

    【讨论】:

      【解决方案2】:

      刚刚意识到以下失败:

      cross_entropy = -tf.reduce_sum(y_*np.log(y))
      

      你不能在 tf 对象上使用 numpy 函数,而且索引也不同。

      【讨论】:

      • 这不是问题。问题是你的变量没有初始化。
      【解决方案3】:

      我认为你可以在 tensorflow 中使用“Wraps Python function”方法。这是文档的link

      至于回答“为什么不直接使用 tensorflow 的内置函数来构建它?”的人呢? - 有时人们正在寻找的成本函数无法用 tf 的函数表示或极其困难。

      【讨论】:

        【解决方案4】:

        这是因为你还没有初始化你的变量,因此它现在没有你的张量(可以阅读更多in my answer here

        只要做这样的事情:

        def normVec(y):
            print y
            return np.cross(y[:,[0,2,4]],y[:,[1,3,5]])
        
        t1 = normVec(y_)
        # and comment everything after it.
        

        看看你现在没有张量,只有Tensor("Placeholder_1:0", shape=TensorShape([Dimension(None), Dimension(6)]), dtype=float32)

        尝试初始化变量

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

        并评估您的变量sess.run(y)。附言到目前为止,您还没有喂过占位符。

        【讨论】:

        • 我试过这些: sess.run(y) 或 sess.run(y_) 都给出错误。并且变量的初始化似乎没有帮助。即使我在创建成本函数之前运行它们(这在此处给出的示例脚本中似乎并不重要:tensorflow.org/tutorials/mnist/beginners/index.md
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 2016-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多