自己学着写了一个简单的深度学习的神经网络。使用样本进行训练,而后进行预测结论。
包含6个输入元,第一隐藏层8个神经元,第二隐藏层4个神经元,输出层用于最后决策,1个神经元
利用Sigmoid函数进行权重的计算和更新。
代码如下:
from numpy import array, exp, random, dot
import tqdm
import time
print("[预测型机器学习神经网络模型]-[粗饼]-[V 1.0版本]")
print("[AI代号][AHN546-3]")
print("[神经元数量][13]")
print('''
**************************************************
* *
* Deep Neural Network 1.0 Pancake *
* *
* IRRIGATION BIRD INC 2022.8 *
* *
* -Up to 13 Neural Training !!!- *
**************************************************
''')
# 训练样本
X = array([
[1, 1, 1, 0, 0, 0], [1, 0, 0, 1, 1, 1], [1, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 1], [0, 0, 1, 1, 1, 0]
])
y = array([
[1, 1, 0, 0, 1, 1, 0, 0]
]).T
def fp(inputs):
l1 = 1 / (1 + exp(-dot(inputs, w0)))
l2 = 1 / (1 + exp(-dot(l1, w1)))
l3 = 1 / (1 + exp(-dot(l2, w2)))
return l1, l2, l3
def bp(l1, l2, l3, y):
# 计算误差
# 最终决策层
error = y - l3
slope = l3 * (1 - l3)
l2_delta = error * slope
# 副决策层
l1_slope = l2 * (1 - l2)
l1_error = l2_delta.dot(w2.T)
l1_delta = l1_error * l1_slope
# 隐藏层/计算层
l0_slope = l1 * (1 - l1)
l0_error = l1_delta.dot(w1.T)
l0_delta = l0_error * l0_slope
return l0_delta, l1_delta, l2_delta
# 初始化权重
random.seed(1)
# 第一层神经元,a输入,b神经元
w0 = random.random((6, 8)) * 2 - 1
# 第二层神经元,a输入,b神经元
w1 = random.random((8, 4)) * 2 - 1
# 第三层神经元,a输入,b神经元
w2 = random.random((4, 1)) * 2 - 1
print("——————————————————————————————————————————————————")
k = input("[请输入训练次数][单位:万]").strip()
k = int(k)
run_times = 1
all_data = []
while True:
data_list = []
data = input(f"[请输入预测数据{run_times}:][格式000000]").strip()
if data == "00" and len(all_data) > 0:
print(f"[共{len(all_data)}条数据][开始训练神经网络]")
print()
print("[已输入全部数据特征列表:]")
for i in all_data:
print(i)
print()
break
elif len(data) != X.shape[1]:
print("[数据长度错误]")
continue
elif data.isdigit():
a = int(data[0])
b = int(data[1])
c = int(data[2])
d = int(data[3])
e = int(data[4])
f = int(data[5])
data_list = [a, b, c, d, e, f]
all_data.append(data_list)
print(f"[数据参数]{data_list}")
run_times += 1
continue
else:
print("[数据格式不正确]")
continue
# 训练中
for _ in tqdm.tqdm(range(k), desc="[深度神经网络训练中]", ncols=100, colour="blue"):
for i in range(10000):
l0 = X
l1, l2, l3 = fp(l0)
l0_delta, l1_delta, l2_delta = bp(l1, l2, l3, y)
w2 = w2 + dot(l2.T, l2_delta)
w1 = w1 + dot(l1.T, l1_delta)
w0 = w0 + dot(l0.T, l0_delta)
time.sleep(0.03)
# 新特征预测
f = open("deeplearing.txt", "w")
print("——————————————————————————————————————————————————")
for new in all_data:
# 返回矩阵最后值
s = fp([new])[2]
j = s[0][0]
f.write(str(j) + "\n")
print(f"[{new}][预测概率:][{round(j, 4) * 100}%]")
f.close()
end = input("[全部工作完成][感谢使用粗饼深度神经网络]")