import os
import numpy as np
import math
def sigmoid(x):
"""
Compute the sigmoid of x
Arguments:
x -- A scalar or numpy array of any size
Return:
s -- sigmoid(x)
"""
# write your code here
fx = []
for num in x:
fx.append(1 / (1 + math.exp(-num)))
return fx
def sigmoid_derivative(x):
"""
Compute the gradient (also called the slope or derivative) of the sigmoid function with respect to its input x.
You can store the output of the sigmoid function into variables and then use it to calculate the gradient.
Arguments:
x -- A scalar or numpy array
Return:
ds -- Your computed gradient.
"""
# write your code here
y = []
fx = []
for num in x:
y.append(1 / (1 + math.exp(-num)))
for n in y:
fx.append(n * (1 - n))
return fx
def softmax(x):
"""Calculates the softmax for the input x.
Argument:
x -- A numpy matrix of shape (n,)
Returns:
s -- A numpy matrix equal to the softmax of x, of shape (n,)
"""
# write your code here
x_exp = np.exp(x)
x_sum = np.sum(x_exp, axis=0, keepdims=True)
fx = x_exp / x_sum
return fx
def cross_entropy_loss(target, prediction):
"""
Compute the cross entropy loss between target and prediction
Arguments:
target -- the real label, a scalar or numpy array size = (n,)
prediction -- the output of model, a scalar or numpy array, size=(n, c)
Return:
mean loss -- cross_entropy_loss(target, prediction)
"""
# write your code here
batch_loss = 0.
for i in range(prediction.shape[0]):
numerator = np.exp(prediction[i, target[i]]) # 分子
denominator = np.sum(np.exp(prediction[i, :])) # 分母
# Calculating individual losses
loss = -np.log(numerator / denominator)
# 损失累加
batch_loss += loss
return batch_loss
if __name__ == '__main__':
## don't edit
x = np.array([-1, 0, 5])
print("sigmoid(x) = ", sigmoid(x))
print("sigmoid_derivative(x) = ", sigmoid_derivative(x))
print("softmax(x) = ", softmax(x))
prediction = np.array([[0.093, 0.1939, -1.0649, 0.4476, -2.0769],
[-1.8024, 0.3696, 0.7796, -1.0346, 0.473],
[0.5593, -2.5067, -2.1275, 0.5548, -1.6639]])
target = np.array([2, 1, 4])
print("cross entropy loss = ", cross_entropy_loss(target, prediction))
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
山东大学人工智能导论实验1工程文件-numpy的基本操作 详解博客地址:https://blog.csdn.net/m0_52316372/article/details/125625648 内容: 使用numpy实现深度学习任务中的一些基本操作,包括以下函数: sigmoid function sigmoid gradient function softmax function cross entropy loss function 目标: 熟悉numpy的一些基本函数 理解sigmoid, softmax, cross entropy loss等函数 代码要求: 按代码模板实现函数功能 文档要求: 代码运行结果截图(main函数里的内容不要修改) sigmoid函数的公式及图像 sigmoid函数梯度求解公式及图像 softmax函数公式 cross entropy loss function公式 它们在神经网络中有什么用处?
资源详情
资源评论
资源推荐
收起资源包目录
AIwork_1.zip (8个子文件)
work_1
work_1.py 2KB
.idea
misc.xml 188B
.name 9B
modules.xml 271B
work_1.iml 454B
workspace.xml 4KB
.gitignore 0B
inspectionProfiles
profiles_settings.xml 174B
共 8 条
- 1
timerring
- 粉丝: 14w+
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 这是一个保存Springboot+MyBaits项目的仓库.zip
- tensorflow-gpu-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.4-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-gpu-2.7.4-cp38-cp38-manylinux2010-x86-64.whl
- 詹佳 陈敏 口语2.mp3
- 图书管理系统毕业设计Java语言实现
- turtle-0.0.2.zip
- 机器学习(股票),基于机器学习算法支持向量机SVM的交易策略
- 1713969763417.png
- 国家药品编码本位码,国家药品编码本位码,国家药品编码本位码,国家药品编码本位码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0