首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >失败断言测试的浮点数的Python ()

失败断言测试的浮点数的Python ()
EN

Stack Overflow用户
提问于 2018-03-20 19:10:07
回答 1查看 3.6K关注 0票数 2

背景:

我已经完成了一门机器学习和神经网络的课程,接下来我遇到了一个问题,我们需要计算成本函数。通过应用np.multiply和np.sum或np.dot,有两种方法可以做到这一点。我在示例中分别调用了这些cost1和cost2。他们产生了同样的结果。

问题:

我的问题是,函数(已经为我完成了)断言成本是使用isinstance()浮动的。第一个方法产生一个通过这个测试的值,而第二个方法没有。但是,当我打印两个值及其相关的dtype时,它们似乎都是浮动的,尽管cost2具有更高的精度。为什么cost2不能通过断言测试?

“守则”:

代码语言:javascript
复制
def compute_cost(A2, Y, parameters):
    """ 
    Computes the cross-entropy cost given in equation (13)

    Arguments:
    A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
    Y -- "true" labels vector of shape (1, number of examples)
    parameters -- python dictionary containing your parameters W1, b1, W2 and b2

    Returns:
    cost -- cross-entropy cost given equation (13)
    """

    m = Y.shape[1] # number of example

    # Compute the cross-entropy cost
    ### START CODE HERE ### (≈ 2 lines of code)
    logprobs = np.multiply(np.log(A2), Y)
    cost1 = -np.sum(logprobs)
    cost2 = -np.dot(np.log(A2), Y.T)
    ### END CODE HERE ###

    cost1 = np.squeeze(cost1)     # makes sure cost is the dimension we expect. 
    cost2 = np.squeeze(cost2)     # E.g., turns [[17]] into 17 


    # Troubleshooting
    print(cost1.dtype,cost2.dtype)
    print(cost1,cost2)

    assert(isinstance(cost1, float))
    assert(isinstance(cost2, float))

    return cost1


A2, Y_assess, parameters = compute_cost_test_case()
print("cost = " + str(compute_cost(A2, Y_assess, parameters)))

输出:

代码语言:javascript
复制
float64 float64
0.692685886972 0.6926858869721941
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-84-92a25de13cb3> in <module>()
  1 A2, Y_assess, parameters = compute_cost_test_case()
  2 
----> 3 print("cost = " + str(compute_cost(A2, Y_assess, parameters)))

<ipython-input-83-411aa6cb57b7> in compute_cost(A2, Y, parameters)
 30 
 31     assert(isinstance(cost1, float))
---> 32     assert(isinstance(cost2, float))
 33 
 34     return cost1

AssertionError: 
EN

回答 1

Stack Overflow用户

发布于 2018-07-19 16:44:33

好吧,让我从头开始。

First,这不是你问的问题,而是科学秘书处的问题。np.dot()并不总是等于np.sum and np.multiply,理论上它们是相等的,但是在成本函数的计算上,它们是不一样的,因为np.dot的计算会因矩阵的不同而不同,更不用说它会因为尺寸失配而给你一个误差。因此,为了安全起见,使用乘然后求和函数,你可以检查这篇文章,以获得更多关于这件事的信息。Python implementation of the cost function in logistic regression: why dot multiplication in one expression but element-wise multiplication in another

第二个,这是您最初的问题,要进入问题,您应该打印关于cost1和cost2的所有信息。

例如,在用keepdims = True计算成本方程之后,您会发现cost.shape = ()cost.dtype = float64type(cost) = numpy.ndarray是一个scalar,这就是您的问题所在。

函数squeeze可以成功地将维度降到顶点,例如,[[.364]]将是.364。然而,.364将是numpy.ndarray类型。

因此,要将scalar转换为浮动,只需执行以下操作

解决方案:

代码语言:javascript
复制
cost = np.asscalar(cost)    # make sure to convert scalar with shape () to normal float

因此,你的主张肯定会运作良好。

您可以检查这个问题How to convert singleton array to a scalar value in Python?和这个也可以What is a ndarray of shape () returned after np.squeeze from shape (1,1)?

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49392396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档