我正在尝试在张量流中编写自己的成本函数,但是显然我无法“切片”张量对象?
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'>
目前,除了第一个轴之外,tensorflow不能在其他轴上聚集-它是被请求的。
但是对于在这种特定情况下要执行的操作,可以转置,然后收集0,2,4,然后转回。它不会很快发疯,但是它可以工作:
tf.transpose(tf.gather(tf.transpose(y), [0,2,4]))
这是一个有用的解决方法,用于解决当前collect实施中的某些限制。
(但是在tensorflow节点上不能使用numpy slice也是正确的-您可以运行它并对输出进行切片,并且还需要在运行之前初始化这些变量。:)。您正在以无效的方式混合tf和np。
x = tf.Something(...)
是张量流图对象。Numpy不知道如何应对此类对象。
foo = tf.run(x)
回到了python可以处理的对象。
您通常希望将损失计算保持在纯张量流中,因此在tf中也要进行交叉和其他函数处理。您可能需要做很长一段路,因为tf没有它的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句