我有兩個函數如下,他們來自Andrew Ng是coursera上的深度學習課程。第一個函數運行,但第二個函數不運行。 logits和標籤變量具有相同的形狀按我改的成本[0.0,0.0,1.0,1.0]
文檔requirements,但它並沒有我直接傳遞變量從函數調用到函數tensorflow tf在構建函數中獲得交叉熵
幫助
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)'