我试图在张量流中编写自己的成本函数,但显然我不能"切割"张量对象?
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))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: Bad slice index [0, 2, 4] of type <type 'list'>
但是对于你想要在这种特定情况下做什么,你可以转置,然后收集0,2,4,然后转置回来.它不会疯狂,但它有效:
tf.transpose(tf.gather(tf.transpose(y), [0,2,4]))
Run Code Online (Sandbox Code Playgroud)
对于当前的collect实现中的一些限制,这是一个有用的解决方法.
(但是你也不能在tensorflow节点上使用numpy切片 - 你可以运行它并切片输出,还需要在运行之前初始化这些变量.:).你是以一种不起作用的方式混合tf和np.
x = tf.Something(...)
Run Code Online (Sandbox Code Playgroud)
是张量流图形对象.Numpy不知道如何应对这些对象.
foo = tf.run(x)
Run Code Online (Sandbox Code Playgroud)
回到python可以处理的对象.
您通常希望将损耗计算保持在纯张量流中,因此tf中的交叉和其他函数也是如此.你可能不得不长途跋涉,因为tf没有它的功能.
| 归档时间: |
|
| 查看次数: |
4315 次 |
| 最近记录: |