Python Sigmoid 函数实现及代码注释
# 导入numpy库
import numpy as np
# 定义函数,参数为x
def sigmoid(x):
# 计算sigmoid函数
return 1 / (1 + np.exp(-x))
# 定义x为一个长度为3的数组
x = np.array([0.5, 0.1, -0.2])
# 定义weights为一个长度为3的数组
weights = np.array([0.5, -0.2, 0.1])
# 定义bias为一个标量
bias = 0.3
# 计算wx + b,其中wx为x向量与weights向量的点积
# dot()函数表示点积
# sum()函数表示对数组进行求和
# 因为x和weights都是长度为3的数组,所以点积后得到的是一个标量
# 最后再加上bias
# 最终的结果是一个标量
output = sigmoid(np.dot(weights, x) + bias)
# 打印输出结果
print('Output:', output)
AI 优化版
原文地址:https://www.cveoy.top/t/topic/m0Lz 著作权归作者所有。请勿转载和采集!