2017-09-22 9 views
1

私はTensor Flow Courseraコースで作業しています。なぜ型が一致しないのか理解できません。テンソルフロー関数に辞書を入力するとどうなりますか?TypeError:unhashable type: 'numpy.ndarray'

これは私が定義しています関数です。

def one_hot_matrix(labels, C): 
    """ 
    Creates a matrix where the i-th row corresponds to the ith class number and the jth column 
        corresponds to the jth training example. So if example j had a label i. Then entry (i,j) 
        will be 1. 

Arguments: 
labels -- vector containing the labels 
C -- number of classes, the depth of the one hot dimension 

Returns: 
one_hot -- one hot matrix 
""" 

### START CODE HERE ### 

# Create a tf.constant equal to C (depth), name it 'C'. (approx. 1 line) 
C = tf.constant(C, name="C") 
#labels =tf.placeholder(labels, name="labels") 

# Use tf.one_hot, be careful with the axis (approx. 1 line) 
one_hot_matrix = tf.one_hot(indices=labels, depth=C, axis=0) 

# Create the session (approx. 1 line) 
sess = tf.Session() 

# Run the session (approx. 1 line) 
one_hot = sess.run(one_hot_matrix, feed_dict={labels:labels, C:C}) 

# Close the session (approx. 1 line). See method 1 above. 
sess.close() 

### END CODE HERE ### 

return one_hot 

そして、この実行している場合:

labels = np.array([1,2,3,0,2,1]) 
one_hot = one_hot_matrix(labels, C = 4) 
print ("one_hot = " + str(one_hot)) 

を私は、このタイプのエラーを取得:

TypeError         Traceback (most recent call last) 
<ipython-input-113-2b9d0290645f> in <module>() 
     1 labels = np.array([1,2,3,0,2,1]) 
----> 2 one_hot = one_hot_matrix(labels, C = 4) 
     3 print ("one_hot = " + str(one_hot)) 

<ipython-input-112-f9f17c86d0ba> in one_hot_matrix(labels, C) 
    28 
    29  # Run the session (approx. 1 line) 
---> 30  one_hot = sess.run(one_hot_matrix, feed_dict={labels:labels, C:C}) 
    31 
    32  # Close the session (approx. 1 line). See method 1 above. 

TypeError: unhashable type: 'numpy.ndarray'ter code here 

私はTensorflowのマニュアルを確認tf.one_hotとnp.arraysに問題はありません。

https://www.tensorflow.org/api_docs/python/tf/one_hot

答えて

2

labelsCグラフ定義時定数でした。したがって、sess.run()を呼び出すときに、再度フィードする必要はありません。私は少しだけ行をone_hot = sess.run(one_hot_matrix1)に変更しましたが、今はうまくいくはずです。

def one_hot_matrix(labels, C): 
    """ 
    Creates a matrix where the i-th row corresponds to the ith class number and the jth column 
        corresponds to the jth training example. So if example j had a label i. Then entry (i,j) 
        will be 1. 

    Arguments: 
    labels -- vector containing the labels 
    C -- number of classes, the depth of the one hot dimension 

    Returns: 
    one_hot -- one hot matrix 
    """ 

    ### START CODE HERE ### 

    # Create a tf.constant equal to C (depth), name it 'C'. (approx. 1 line) 
    C = tf.constant(C, name="C") 
    #labels =tf.placeholder(labels, name="labels") 

    # Use tf.one_hot, be careful with the axis (approx. 1 line) 
    one_hot_matrix1 = tf.one_hot(indices=labels, depth=C, axis=0) 

    # Create the session (approx. 1 line) 
    sess = tf.Session() 

    # Run the session (approx. 1 line) 
    one_hot = sess.run(one_hot_matrix1) #, feed_dict={labels:labels, C:C} 

    # Close the session (approx. 1 line). See method 1 above. 
    sess.close() 

    ### END CODE HERE ### 

    return one_hot 

ラン:

labels = np.array([1,2,3,0,2,1]) 
one_hot = one_hot_matrix(labels, C = 4) 
print ("one_hot = " + str(one_hot)) 

出力:それをやった

one_hot = [[ 0. 0. 0. 1. 0. 0.] 
[ 1. 0. 0. 0. 0. 1.] 
[ 0. 1. 0. 0. 1. 0.] 
[ 0. 0. 1. 0. 0. 0.]] 
+0

、ありがとう! – IUF

関連する問題