【发布时间】:2021-01-20 20:52:33
【问题描述】:
我正在处理一项要求我用 Python 编写 sigmoid 函数的作业。我对此很陌生,所以我有点理解 Sigmoid 函数的工作原理。
作业要求我对其进行编码,以便无论 z 是数组还是标量,该函数都可以工作。我知道数组是什么,但我不知道标量是什么。并且搜索它只产生了我不明白的数学术语。该作业还建议我使用numpy.exp 来解决问题。无论如何,这是我写的,它显然不起作用。我不确定我的逻辑是否错误,或者仅仅是因为我没有考虑标量与数组的关系。谁能帮助我了解那是什么或在哪里可以找到该信息?
这是代码:
def sigmoid(z):
'''
Input:
z: is the input (can be a scalar or an array)
Output:
h: the sigmoid of z
'''
### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###
# calculate the sigmoid of z
h = 1/1+np.exp(-z)
### END CODE HERE ###
return h
# Testing your function
if (sigmoid(0) == 0.5):
print('SUCCESS!')
else:
print('Oops!')
if (sigmoid(4.92) == 0.9927537604041685):
print('CORRECT!')
else:
print('Oops again!')
【问题讨论】:
标签: python python-3.x numpy sigmoid