0
テンソルフローのboolean_mask
関数をグラジエントオプティマイザ(Anaconda 3.5のWindows x64ではテンソルフローバージョン0.12.1)とともに使用しようとすると、問題が発生します。Tensorflowがグラデーションを評価できない
[[ 1. 2. 3.]
[ 4. 5. 6.]
[ 7. 8. 9.]
[ 10. 11. 12.]]
[[ True False]
[False False]
[ True True]]
[[ 3. 24. 63. 120.]
[ 3. 6. 9. 12.]]
:正しい出力を生成
import tensorflow as tf
import numpy as np
x = tf.Variable(np.arange(1, 13).reshape(4, 3).astype(np.float32))
map_x_on_y = np.array([[True, False], [False, False], [ True, True]])
transpose_of_x = tf.transpose(x)
fn = lambda t: tf.reduce_prod(tf.boolean_mask(transpose_of_x, t), axis=0)
y = tf.stack([fn(i) for i in tf.unstack(map_x_on_y, axis=1)])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(x.eval())
print(map_x_on_y)
print(y.eval())
:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-cc039c0c3ed9> in <module>()
21 # Train the model
22 sum_of_squared_errors = tf.reduce_mean(tf.square(y_-y))
---> 23 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(sum_of_squared_errors)
24 init = tf.global_variables_initializer()
25
C:\Anaconda3\lib\site-packages\tensorflow\python\training\optimizer.py in minimize(self, loss, global_step, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, name, grad_loss)
274 "No gradients provided for any variable, check your graph for ops"
275 " that do not support gradients, between variables %s and loss %s." %
--> 276 ([str(v) for _, v in grads_and_vars], loss))
277
278 return self.apply_gradients(grads_and_vars, global_step=global_step,
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ['Tensor("Variable/read:0", shape=(3, 10), dtype=bool)', 'Tensor("Variable_1/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_2/read:0", shape=(3, 10), dtype=bool)', 'Tensor("Variable_3/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_4/read:0", shape=(3, 10), dtype=float64)', 'Tensor("Variable_5/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_6/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_7/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_8/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_9/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_10/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_11/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_12/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_13/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_14/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_15/read:0", shape=(5, 3), dtype=int32)', 'Tensor("Variable_16/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_17/read:0", shape=(5, 3), dtype=int32)', 'Tensor("Variable_18/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_19/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_20/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_21/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_22/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_23/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_24/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_25/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_26/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_27/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_28/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_29/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_30/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_31/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_32/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_33/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_34/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_35/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_36/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_37/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_38/read:0", shape=(3, 2), dtype=bool)'] and loss Tensor("Mean_1:0", shape=(), dtype=float32).
操作は変数のみを使用した場合、正常に動作するように見える:
import tensorflow as tf
import numpy as np
# Test data
x_dat = np.arange(1, 13).reshape(4, 3).astype(np.float32)
y_dat = np.arange(1, 9).reshape(4, 2).astype(np.float32)
map_x_on_y = np.array([[True, False], [False, False], [ True, True]])
# Test data
x = tf.placeholder(tf.float32, [None, 3])
y_ = tf.placeholder(tf.float32, [None, 2])
# Model: Take the column product of the elements of x using the rows of
# map_x_on_y as a filter.
# The two columns of map_x_on_y give the two columns of y
transpose_of_x = tf.transpose(x)
fn = lambda t: tf.reduce_prod(tf.boolean_mask(transpose_of_x, t), axis=0)
y = tf.stack([fn(i) for i in tf.unstack(map_x_on_y, axis=1)])
# Train the model
sum_of_squared_errors = tf.reduce_mean(tf.square(y_-y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(sum_of_squared_errors)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
feed_dict ={x: x_dat, y_: y_dat}
for i in range(10):
sess.run([train_step], feed_dict)
は、私は、次のエラーメッセージが表示されます
初心者のテンソルフロー、私は非常にあなたの助けに感謝して、何が間違って定義された操作の勾配評価となるのでしょうか?
ベストバスティーノ