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
sig = 1 / (1 + np.exp(-x))
return sig
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 = np.exp(x) / np.sum(np.exp(x), axis=0, keepdims=True)
return x
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
delta = 1e-6
return -np.sum(prediction * np.log(target + delta))
def forward(w, b, x):
"""
Arguments:
w -- weights, a numpy array of size (m, 1)
b -- bias, a scalar
x -- data of size (n, m)
Return:
prediction
"""
## write your code here
prediction = sigmoid(x @ w + b)
# print(prediction.shape)
return prediction
def backward(x, target, prediction):
"""
Arguments:
x -- data of size (n, m)
target -- data of size (n, num_class)
prediction -- data of size (n, num_class)
Return:
dw, db
"""
delta = target - prediction
db = delta
dw = sigmoid(x.T) @ delta
return dw, db
# don't edit
if __name__ == '__main__':
## three samples
x = np.array([[12, 3, 7, 4], [3, 10, 4, 9], [9, 6, 2, 0]])
target = np.array([0, 1, 2])
num_class = 3
## learning rate of the gradient descent update rule
learning_rate = 0.001
## one-hot label
target = np.eye(num_class)[target]
n, m = x.shape
w = np.zeros([m, num_class])
b = 0
# three iterations of forward and backward
for i in range(3):
prediction = forward(w, b, x)
loss = cross_entropy_loss(target, softmax(prediction))
dw, db = backward(x, target, prediction)
# update w and b
w = w - learning_rate * dw
b = b - learning_rate * db
print("iter = {}, w = {}, b = {}, loss= {}".format(i, w, b, loss))
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
山东大学人工智能导论实验2工程文件-前向传播和反向传播 详解博客地址:https://blog.csdn.net/m0_52316372/article/details/125626360 内容: 假设X有n个样本,属于m=3个类别, a^m表示样本属于第m类的概率,请实现 的三次前向传播及反向传播(更新参数ω和b),每次反向传播结束后更新并输出参数ω和b的值,计算cross entropy loss,其中σ(∙)表示sigmoid函数。 目标: 理解前向传播和反向传播 应用作业一中提到的基本操作 代码要求: 按代码模板实现函数功能 文档要求: 前向传播及反向传播涉及到的公式计算(参考) 粘贴代码输出结果截图。
资源详情
资源评论
资源推荐
收起资源包目录
AIwork_2.zip (8个子文件)
work_2
work_2.py 2KB
.idea
work_2.iml 454B
misc.xml 188B
.name 9B
modules.xml 271B
workspace.xml 3KB
.gitignore 184B
inspectionProfiles
profiles_settings.xml 174B
共 8 条
- 1
timerring
- 粉丝: 16w+
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0