2017-01-19 32 views
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) 

は、私は、次のエラーメッセージが表示されます

初心者のテンソルフロー、私は非常にあなたの助けに感謝して、何が間違って定義された操作の勾配評価となるのでしょうか?

ベストバスティーノ

答えて

0

私は問題を解決することができました。この例では、最適化するパラメータを追加するのを忘れていました。

C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py:91: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory. 
    "Converting sparse IndexedSlices to a dense Tensor of unknown shape. " 

懸念のこの警告は、次のとおりです、私はまだ、次の警告を受ける

[[ 0.50838345 0.80531198 0.87862223 0.91182482] 
[ 0.66666669 0.83333331 0.8888889 0.91666669] 
[ 0.76236677 0.86516243 0.90587789 0.92770731]] 
[[ 0.38757464 0.76236677] 
[ 0.69672567 0.86516243] 
[ 0.79592443 0.90587789] 
[ 0.84590656 0.92770731]] 

けれども:

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) 

# Test data 
x = tf.placeholder(tf.float32, [None, 3]) 
y_ = tf.placeholder(tf.float32, [None, 2]) 

parameter = tf.Variable(tf.ones([3, 1], dtype=tf.float32)) 

# Model: Take the column product of the elements of x using the rows of map_x_on_y as a filter. 
# Map for projecting x on y 
map_x_on_y = tf.Variable(np.array([[True, False], [False, False], [ True, True]])) 

intermediate = tf.map_fn(lambda t: tf.div(t[0], tf.add(t[1], t[0])), 
         (tf.transpose(x), parameter), dtype=tf.float32) 

fn = lambda t: tf.reduce_prod(tf.boolean_mask(intermediate, t), axis=0) 
y = tf.transpose(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(1): 
     sess.run([train_step], feed_dict)  
     print(sess.run(intermediate, feed_dict)) 
     print(sess.run(y, feed_dict)) 

は、これは正しい出力を生成しましたか?

関連する問題