正向传播算法

在这里插入图片描述

import numpy as np
import math
"""正向传播"""
n = 3  # n 输入层 神经元个数
m = 13  # m 隐藏层 神经元
win = np.ones((n, 1))  # 输入层 权重
# w12 = np.mat(np.random.randint(1, 1, size=(m, n)))  # 创建随机 矩阵 m行 n 列
# w23 = np.mat(np.random.randint(1, 1, size=(m, n)))  #
w12 = np.ones((m, n))  # 创建随机 矩阵 m行 n 列
w23 = np.ones((m, n))  #
out = np.ones((n, 1))  # 输出层 权重
print(w12)
x = np.ones((n, 1))  # 输入层
th1 = np.zeros((n, 1))
th2 = np.zeros((m, 1))
th3 = np.zeros((n, 1))
# 第一层
xin = x  # 输入层 (n, 1)
# 0. 输入层 给予 激励函数
y0 = np.ones((n, 1))
y0 = 1.0 / (1 + pow(math.e, -xin-th1))  # 激励函数 (n,1)

# 1.经过 系数矩阵 和 偏正矩阵 计算后 得
xhid = np.dot(w12, xin)  # 隐藏层(m, n)* (n, 1) =(m, 1)

# 2. 将 隐藏层 给予激励函数
y1 = np.ones((m, 1))
y1 = 1.0 / (1 + pow(math.e, -xhid-th2))  # 激励函数

"""
此时隐藏层 为 y1 (m行1列的   列向量)
"""
# 3.经过 系数矩阵 和 偏正矩阵 计算后 得
xout = np.dot(w23.T, y1) - th3  # 输出层 (n, m) * (m, 1)= (n,1)  - (n,1)

# 4. 将 输出层 给予激励函数
y2 = np.ones((n, 1))
y2 = 1.0 / (1 + pow(math.e, -xout-th3))  # 激励函数

print(y2)  # (n,1)

"""正向传播 结束 """
posted @ 2019-12-22 15:53  JimmyYang_MJ  阅读(312)  评论(0编辑  收藏  举报