2017-07-17 6 views
0

こんにちは、私はテンソルをマップしようとしているPython関数を持っています。私は本質的にすべての要素を関数を通して実行する必要があります。私はどのようにこの関数に2つのパラメータをマップするか分からない。これだけでなく、私はそれは私にエラーを与えて二番目のパラメータ削除した場合でも:ここTensorflow - 2つのパラメータを使ってPython関数をマッピングする

TypeError: bad operand type for unary -: 'list' 

を私の完全なコードは次のとおりです。

import tensorflow as tf 

def sigmoid(x, derivative = False): 
    if derivative == True: 
     return (1.0/(1+math.exp(-x))) * (1.0 - (1.0/(1+math.exp(-x)))) 
    return 1.0/(1+math.exp(-x)) 

# build computational graph 
a = tf.placeholder('float', None) 

result = tf.map_fn(sigmoid, [a] , tf.float32) 

# initialize variables 
init = tf.global_variables_initializer() 

# create session and run the graph 
with tf.Session() as sess: 
    sess.run(init) 
    print sess.run(result, feed_dict={a: [2,3,4]}) 

# close session 
sess.close() 

私はこの次ました:https://www.tensorflow.org/api_docs/python/tf/py_func

EDITを 私はtensorflowsライブラリを使ってexp関数を実行できます:

def sigmoid(x, derivative = False): 
    if derivative == True: 
     return (1.0/(1+tf.exp(-x))) * (1.0 - (1.0/(1+tf.exp(-x)))) 
    return 1.0/(1+tf.exp(-x)) 

答えて

2

tf.nn.sigmoid()機能を使用できないのはなぜですか?あなたがグラフでnumpy機能を呼び出したい場合は

def sigmoid(x, derivative = False): 
    if derivative == True: 
     return tf.nn.sigmoid(x) * (1.0 - tf.nn.sigmoid(x)) 
    return tf.nn.sigmoid(x) 

は、使用することができますtf.py_func(コードはCPUで実行されますのみ):

def sigmoid(x, derivative = False): 
if derivative == True: 
    return (1.0/(1+np.exp(-x))) * (1.0 - (1.0/(1+np.exp(-x)))) 
return 1.0/(1+np.exp(-x)) 

# build computational graph 
a = tf.placeholder('float', None) 

result = tf.py_func(sigmoid, [a, True] , tf.float32) 
+0

これは動作しますが、私はそれをどのように取得することができますデリバティブを使用する?どのように関数に2つのパラメータを渡すことができますか? – Kevin

+0

result = sigmoid(a、derivative = True)を呼び出すだけですか? –

+0

ありがとう、私はnumpyを必要とする操作を行う必要がありましたか?私が理解していることから、私はこれをすることができませんでしたか? – Kevin

関連する問題