2017-05-05 24 views
-1

我有斧numpy的數組:故障定義numpy的數組作爲tensorflow變量

[0, 6, 3513, 7, 155, 794, 25, 223, 8, 32, 20, 202, 5025, 350, 91, 6, 66, 207, 5, 2] 

我想將它定義爲一個tensorflow變量如下:

tf.Variable(x) 

而且我得到以下錯誤:

TypeError: Expected binary or unicode string, got [0, 6, 3513, 7, 155, 794, 25, 223, 8, 32, 20, 202, 5025, 350, 91, 6, 66, 207, 5, 2]

到底是什麼?

+1

我沒有回答你的問題。只需確認導入numpy爲np; 將tensorflow導入爲tf; x = np.array([0,6,3513,7,155,794,25,223,8,32,20,202,5025,350,91,6,66,207,5,2]) ; tf.Variable(x)在這裏工作(正如你猜測它應該) – tagoma

回答

2

你可以分享你想做什麼,因爲tensorflow只是定義了一個變量,你只能在執行該會話時使用該變量。希望下面的代碼可以幫助你。

import tensorflow as tf 
    import numpy as np 
    x =[0, 6, 3513, 7, 155, 794, 25, 223, 8, 32, 20, 202, 5025, 350, 91, 6, 
     66, 207, 5, 2] 

    # convert it into numpy array 
    w = np.array(x) 

    # this create a tensor variable 
    q = tf.Variable(x) 

    # create an interactive session 
    sess = tf.InteractiveSession() 


    # now you can perform operation on that tensor variable 

    tf.add(q,q) 
+0

首先,我的x數組已經是一個numpy類型。其次,它沒有成功。 – yusuf

+1

什麼都沒有解決,你能解釋你想做什麼。請分享你想要做的事情。 –

+0

有一個numpy數組,我試圖將它轉換爲tensorflow變量。而已。 – yusuf

1
x = np.array(...) 

v = tf.Variable(tf.constant(x))