我有两个功能如下,他们来自Andrew Ng是关于课程的深度学习课程。第一个函数运行,但第二个函数不运行。 logits和标签变量具有与文档requirements相同的形状我将费用更改为[0.0,0.0,1.0,1.0]
,但它没有帮助:(
在第一个函数的情况下,我直接将变量从函数调用传递给函数
1)
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')
#C = tf.placeholder(tf.int32, name = 'C')
#labels = tf.placeholder(tf.int32, name = 'labels')
# Use tf.one_hot, be careful with the axis (approx. 1 line)
one_hot_matrix = tf.one_hot(labels, 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)
one_hot = sess.run(one_hot_matrix)
# 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))
2)
def cost(logits, labels):
"""
Computes the cost using the sigmoid cross entropy
Arguments:
logits -- vector containing z, output of the last linear unit (before the final sigmoid activation)
labels -- vector of labels y (1 or 0)
Note: What we've been calling "z" and "y" in this class are respectively called "logits" and "labels"
in the TensorFlow documentation. So logits will feed into z, and labels into y.
Returns:
cost -- runs the session of the cost (formula (2))
"""
### START CODE HERE ###
# Create the placeholders for "logits" (z) and "labels" (y) (approx. 2 lines)
z = tf.placeholder(tf.float32, name = 'z')
y = tf.placeholder(tf.float32, name = 'y')
# Use the loss function (approx. 1 line)
#cost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z, labels = y)
cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels)
# Create a session (approx. 1 line). See method 1 above.
sess = tf.Session()
# Run the session (approx. 1 line).
#cost = sess.run(cost, feed_dict = {z: logits, y:labels})
cost = sess.run(cost)
# Close the session (approx. 1 line). See method 1 above.
sess.close()
### END CODE HERE ###
return cost
logits = sigmoid(np.array([0.2,0.4,0.7,0.9]))
cost = cost(logits, np.array([0,0,1,1]))
print ("cost = " + str(cost))
错误是
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-51f13e22d2ec> in <module>()
1 logits = sigmoid(np.array([0.2,0.4,0.7,0.9]))
----> 2 cost = cost(logits, np.array([0,0,1,1]))
3 print ("cost = " + str(cost))
<ipython-input-60-3febf014323d> in cost(logits, labels)
26 # Use the loss function (approx. 1 line)
27 #cost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z, labels = y)
---> 28 cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels)
29
30 # Create a session (approx. 1 line). See method 1 above.
/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/nn_impl.py in sigmoid_cross_entropy_with_logits(_sentinel, labels, logits, name)
169 relu_logits = array_ops.where(cond, logits, zeros)
170 neg_abs_logits = array_ops.where(cond, -logits, logits)
--> 171 return math_ops.add(relu_logits - logits * labels,
172 math_ops.log1p(math_ops.exp(neg_abs_logits)),
173 name=name)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
827 if not isinstance(y, sparse_tensor.SparseTensor):
828 try:
--> 829 y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
830 except TypeError:
831 # If the RHS is not a tensor, it might be a tensor aware object
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
674 name=name,
675 preferred_dtype=preferred_dtype,
--> 676 as_ref=False)
677
678
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
739
740 if ret is None:
--> 741 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
742
743 if ret is NotImplemented:
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
612 raise ValueError(
613 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 614 % (dtype.name, t.dtype.name, str(t)))
615 return t
616
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("logistic_loss_4/labels:0", shape=(4,), dtype=int64)'
答案 0 :(得分:1)
你是否认为线条是否创建了C作为张量流常数?这不是问题吗?尝试再次取消注释该行,并将C添加为值。所以看起来应该是这样的:
C = tf.constant(C,tf.int32, name = "C")
因此,您将作为参数获得的值分配给Tensorflow常量C.
答案 1 :(得分:1)
你不必在one_hot = sess.run(one_hot_matrix)中映射de feed_dict,例如:
one_hot = sess.run(one_hot_matrix, feed_dict={labels:labels, C:C})
答案 2 :(得分:0)
2)这是您需要做的。见下文:
### START CODE HERE ###
# Create the placeholders for "logits" (z) and "labels" (y) (approx. 2 lines)
z = tf.placeholder(tf.float64 , name='z')
y = tf.placeholder(tf.float64 , name='y')
# Use the loss function (approx. 1 line)
cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=z, labels=y)
# Create a session (approx. 1 line). See method 1 above.
sess = tf.Session()
# Run the session (approx. 1 line).
cost = sess.run(cost, feed_dict={z:logits, y:labels})
# Close the session (approx. 1 line). See method 1 above.
sess.close()
### END CODE HERE ###
return cost