Skip to content

Commit

Permalink
add nn_bp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pfliu-nlp committed Feb 29, 2024
1 parent ca1bc4e commit 4253ef8
Show file tree
Hide file tree
Showing 37 changed files with 124 additions and 35 deletions.
2 changes: 1 addition & 1 deletion 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="generator" content="Docusaurus v3.1.1">
<title data-rh="true">Page Not Found | 大语言模型(Large Language Models)</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" name="twitter:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" property="og:url" content="https://your-docusaurus-site.example.com/cs2916/404.html"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="Page Not Found | 大语言模型(Large Language Models)"><link data-rh="true" rel="icon" href="/cs2916/img/plm.png"><link data-rh="true" rel="canonical" href="https://your-docusaurus-site.example.com/cs2916/404.html"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/404.html" hreflang="en"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/404.html" hreflang="x-default"><link rel="alternate" type="application/rss+xml" href="/cs2916/blog/rss.xml" title="大语言模型(Large Language Models) RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/cs2916/blog/atom.xml" title="大语言模型(Large Language Models) Atom Feed"><link rel="stylesheet" href="/cs2916/assets/css/styles.b9e4a9be.css">
<script src="/cs2916/assets/js/runtime~main.49a98fd3.js" defer="defer"></script>
<script src="/cs2916/assets/js/runtime~main.4370630b.js" defer="defer"></script>
<script src="/cs2916/assets/js/main.a9dae6aa.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
Expand Down
83 changes: 83 additions & 0 deletions assets/files/nn_bp-bee2999c27c7ab32e0788f422816a8b6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import numpy as np

# 初始化参数
np.random.seed(42) # 确保每次运行结果一致
input_size = 2 # 输入层节点数
hidden_size = 3 # 隐藏层节点数
output_size = 2 # 输出层节点数
learning_rate = 0.1 # 学习率

# 随机初始化权重和偏置
W1 = np.random.randn(input_size, hidden_size)
b1 = np.zeros(hidden_size)
W2 = np.random.randn(hidden_size, output_size)
b2 = np.zeros(output_size)

# ReLU激活函数及其导数
def relu(x):
return np.maximum(0, x)

def relu_derivative(x):
return (x > 0).astype(float)

# Softmax函数
def softmax(x):
exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))
return exp_x / np.sum(exp_x, axis=1, keepdims=True)

# 交叉熵损失及其导数
def cross_entropy_loss(y_pred, y_true):
m = y_true.shape[0]
return -np.sum(y_true * np.log(y_pred)) / m

def delta_cross_entropy_softmax(y_pred, y_true):
return y_pred - y_true

# 前向传播
def forward_propagation(X):
Z1 = X.dot(W1) + b1
A1 = relu(Z1)
Z2 = A1.dot(W2) + b2
A2 = softmax(Z2)
return Z1, A1, Z2, A2

# 反向传播
def backward_propagation(X, Y, Z1, A1, Z2, A2):
m = X.shape[0]

dZ2 = delta_cross_entropy_softmax(A2, Y)
dW2 = (1/m) * np.dot(A1.T, dZ2)
db2 = (1/m) * np.sum(dZ2, axis=0)

dA1 = np.dot(dZ2, W2.T)
dZ1 = dA1 * relu_derivative(Z1)
dW1 = (1/m) * np.dot(X.T, dZ1)
db1 = (1/m) * np.sum(dZ1, axis=0)

return dW1, db1, dW2, db2

# 更新参数
def update_parameters(dW1, db1, dW2, db2):
global W1, b1, W2, b2
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
W2 -= learning_rate * dW2
b2 -= learning_rate * db2

# 示例输入和标签
X = np.array([[0.1, 0.2], [0.3, 0.4]]) # 2个样本
Y = np.array([[1, 0], [0, 1]]) # 对应的one-hot标签

# 执行一次前向传播
Z1, A1, Z2, A2 = forward_propagation(X)

# 计算损失
loss = cross_entropy_loss(A2, Y)

# 执行一次反向传播
dW1, db1, dW2, db2 = backward_propagation(X, Y, Z1, A1, Z2, A2)

# 更新参数
update_parameters(dW1, db1, dW2, db2)

print(loss)
1 change: 1 addition & 0 deletions assets/js/98d3bd04.6fe34833.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/js/runtime~main.4370630b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion blog/archive/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="generator" content="Docusaurus v3.1.1">
<title data-rh="true">Archive | 大语言模型(Large Language Models)</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" name="twitter:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" property="og:url" content="https://your-docusaurus-site.example.com/cs2916/blog/archive"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="Archive | 大语言模型(Large Language Models)"><meta data-rh="true" name="description" content="Archive"><meta data-rh="true" property="og:description" content="Archive"><link data-rh="true" rel="icon" href="/cs2916/img/plm.png"><link data-rh="true" rel="canonical" href="https://your-docusaurus-site.example.com/cs2916/blog/archive"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/blog/archive" hreflang="en"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/blog/archive" hreflang="x-default"><link rel="alternate" type="application/rss+xml" href="/cs2916/blog/rss.xml" title="大语言模型(Large Language Models) RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/cs2916/blog/atom.xml" title="大语言模型(Large Language Models) Atom Feed"><link rel="stylesheet" href="/cs2916/assets/css/styles.b9e4a9be.css">
<script src="/cs2916/assets/js/runtime~main.49a98fd3.js" defer="defer"></script>
<script src="/cs2916/assets/js/runtime~main.4370630b.js" defer="defer"></script>
<script src="/cs2916/assets/js/main.a9dae6aa.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
Expand Down
2 changes: 1 addition & 1 deletion blog/first-blog-post/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="generator" content="Docusaurus v3.1.1">
<title data-rh="true">First Blog Post | 大语言模型(Large Language Models)</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" name="twitter:image" content="https://your-docusaurus-site.example.com/cs2916/img/plm.png"><meta data-rh="true" property="og:url" content="https://your-docusaurus-site.example.com/cs2916/blog/first-blog-post"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="First Blog Post | 大语言模型(Large Language Models)"><meta data-rh="true" name="description" content="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet"><meta data-rh="true" property="og:description" content="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet"><meta data-rh="true" property="og:type" content="article"><meta data-rh="true" property="article:published_time" content="2019-05-28T00:00:00.000Z"><meta data-rh="true" property="article:author" content="https://github.com/wgao19"><meta data-rh="true" property="article:tag" content="hola,docusaurus"><link data-rh="true" rel="icon" href="/cs2916/img/plm.png"><link data-rh="true" rel="canonical" href="https://your-docusaurus-site.example.com/cs2916/blog/first-blog-post"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/blog/first-blog-post" hreflang="en"><link data-rh="true" rel="alternate" href="https://your-docusaurus-site.example.com/cs2916/blog/first-blog-post" hreflang="x-default"><link rel="alternate" type="application/rss+xml" href="/cs2916/blog/rss.xml" title="大语言模型(Large Language Models) RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/cs2916/blog/atom.xml" title="大语言模型(Large Language Models) Atom Feed"><link rel="stylesheet" href="/cs2916/assets/css/styles.b9e4a9be.css">
<script src="/cs2916/assets/js/runtime~main.49a98fd3.js" defer="defer"></script>
<script src="/cs2916/assets/js/runtime~main.4370630b.js" defer="defer"></script>
<script src="/cs2916/assets/js/main.a9dae6aa.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
Expand Down
Loading

0 comments on commit 4253ef8

Please sign in to comment.