【问题标题】:Getting an error like TypeError: cannot unpack non-iterable float object得到一个像 TypeError: cannot unpack non-iterable float object 这样的错误
【发布时间】:2020-06-27 09:54:50
【问题描述】:

正在计算成本,但面临 TypeError: cannot unpack non-iterable float object 的错误

#Compute cost
def compute_cost(A2,y,parameters):
    m=y.shape[0]
    logprobs = y*np.log(A2) + (1-y)*np.log(1-A2)
    cost = -1/m*np.sum(logprobs)
    cost = float(np.squeeze(cost))  # makes sure cost is the dimension we expect.
    assert(isinstance(cost, float))
    return cost

#Calling the function
A2, y, parameters =compute_cost(A2,y,parameters)
print("cost = " + str(compute_cost(A2, y, parameters)))

TypeError                                 Traceback (most recent call last)
<ipython-input-533-089c3da0f833> in <module>
1 A2, y, parameters =compute_cost(A2,y,parameters)
2 print("cost = " + str(compute_cost(A2, y, parameters)))

TypeError: cannot unpack non-iterable float object

【问题讨论】:

标签: python iterable-unpacking


【解决方案1】:

来自A2, y, parameters =compute_cost(A2,y,parameters) 行。您正在调用 compute_cost,但将其返回值(单个浮点数)分配给三个变量。

那一行应该是这样的:

cost = compute_cost(A2,y,parameters)

另外,您似乎没有在任何地方使用parameters 参数。你也没有为A2yparameters设置初始值。

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2021-11-27
    • 2020-08-26
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多