Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当将字典馈送给tensorflow函数时,我得到了为什么会得到TypeError:Unhabable type:';numpy.ndarray和#x27;_Tensorflow - Fatal编程技术网

当将字典馈送给tensorflow函数时,我得到了为什么会得到TypeError:Unhabable type:';numpy.ndarray和#x27;

当将字典馈送给tensorflow函数时,我得到了为什么会得到TypeError:Unhabable type:';numpy.ndarray和#x27;,tensorflow,Tensorflow,我正在学习张量流课程,我不明白为什么会出现类型不匹配 这就是我定义的功能: 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

我正在学习张量流课程,我不明白为什么会出现类型不匹配

这就是我定义的功能:

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
TypeError回溯(最近一次调用)
在()
1标签=np.数组([1,2,3,0,2,1])
---->2一个热=一个热矩阵(标签,C=4)
3次打印(“一次热=“+str(一次热))
在一个热矩阵中(标签,C)
28
29#运行会话(约1行)
--->30 one_hot=sess.run(one_hot_矩阵,feed_dict={labels:labels,C:C})
31
32#结束课程(约1行)。见上文方法1。
TypeError:无法损坏的类型:“numpy.ndarray”代码在此处
我检查了tf.one_hot的Tensorflow文档,np.array应该没有问题


在图形定义过程中,
标签
C
是常量。因此,在调用
sess.run()
时,不需要再次向它们提供数据。我只是稍微将行更改为
one\u hot=sess.run(one\u hot\u 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
运行:

输出:

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.]]
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.]]