【问题标题】:When feeding a dictionary to a tensorflow function I get Why do I get TypeError: unhashable type: 'numpy.ndarray'将字典提供给 tensorflow 函数时,我得到为什么我会得到 TypeError: unhashable type: 'numpy.ndarray'
【发布时间】:2017-09-23 05:15:54
【问题描述】:

我正在学习 Tensor Flow Coursera 课程,但我不明白为什么会出现类型不匹配。

这是我定义的函数:

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

我查看了 tf.one_hot 的 Tensorflow 文档,np.arrays 应该没有问题。

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

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    labels 和 C 在图形定义期间是常量。因此,调用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.]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      相关资源
      最近更新 更多