2017-07-30 130 views
1

這裏是回溯:tf.hessians() - ValueError異常:無值不支持

Traceback (most recent call last): 
    File "test.py", line 39, in <module> 
    hess = tf.hessians(loss, wrt_variables) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gradients_impl.py", line 970, in hessians 
    _gradients = array_ops.unstack(_gradients) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/array_ops.py", line 952, in unstack 
    value = ops.convert_to_tensor(value) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 639, in convert_to_tensor 
    as_ref=False) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 704, in internal_convert_to_tensor 
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/constant_op.py", line 113, in _constant_tensor_conversion_function 
    return constant(v, dtype=dtype, name=name) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/constant_op.py", line 102, in constant 
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 
    File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_util.py", line 360, in make_tensor_proto 
    raise ValueError("None values not supported.") 
ValueError: None values not supported. 

變量:

import tensorflow as tf 

data_x = [0., 1., 2.] 
data_y = [-1., 1., 3.] 
batch_size = len(data_x) 

x = tf.placeholder(shape=[batch_size], dtype=tf.float32, name="x") 
y = tf.placeholder(shape=[batch_size], dtype=tf.float32, name="y") 

W = tf.Variable(tf.ones(shape=[1]), dtype=tf.float32, name="W") 
b = tf.Variable(tf.zeros(shape=[1]), dtype=tf.float32, name="b") 

pred = x * W + b 
loss = tf.reduce_mean(0.5 * (y - pred)**2) 

然後,跟隨了這個代碼將工作:

wrt_variables = [W, b] 
hess = tf.hessians(loss, wrt_variables) 

但是這個失敗:

wrt_variables = tf.concat([W, b], axis=0) 
hess = tf.hessians(loss, wrt_variables) 

,這也將失敗:

wrt_variables = [tf.concat([W, b], axis=0)] 
hess = tf.hessians(loss, wrt_variables) 

它也沒有爲重塑業務。

的這個代碼,並帶有註釋的完整版本可以在這裏看到: https://gist.github.com/guillaume-chevalier/6b01c4e43a123abf8db69fa97532993f

謝謝!

回答

1

這是因爲在您的圖中,節點loss不依賴節點tf.concat([W,b], axis=0)。一個到另一個沒有反向傳播,因此沒有派生。

Tensorflow不是一個正式的微積分引擎,它只能通過另一個節點估計一個節點的派生值,前者在後者的下游。因此,例如,即使

tf.hessian(loss, 2*W) 

將失敗出於同樣的原因(2*W是一個新節點,loss不依賴於它),即使到tf.hessian(loss, W)的關係是straightfoward。

請注意,儘管它發生了不同的錯誤,但其符號與tf.gradients相同:它返回None而不是拋出異常。