def w_sum(a, b): # 定义了一个运算函数
assert (len(a) == len(b)) # len()是 Python 的内置函数之一,它返回对象的长度。
# 当a和b长度一样的时,返回值为Ture
# Python的assert是用来检查一个条件,如果它为真,就不做任何事。如果它为假,则会抛出AssertError并且包含错误信息。
output = 0 # 赋值output的初始值为0
for i in range(len(a)): # for循环函数,自0开始便利全部函数
output += (a[i] * b[i]) # output = a[1]*b[1]+a[2]*b[3]+……+a[i]*b[i]
return output
def vect_mat_mul(vect, matrix):
assert (len(vect) == len(matrix))
output = [0, 0, 0]
for i in range(len(vect)):
output[i] = w_sum(vect, matrix[i])
# print("matrix[i]的值是",matrix[i],"其中i是",i)
return output
in_wgt = [[0.1,0.2,-0.1], #hid[0]
[-0.1,0.1,0.9], #hid[1]
[0.1,0.4,0.1]] #hid[2]
hp_wgt = [[0.3,1.1,-0.3], #hurt
[0.1,0.2,0.0], #win
[0.0,1.2,0.1]] #sad
weights = [in_wgt,hp_wgt]
def neural_network(input, weights): # input是样本输入
hid = vect_mat_mul(input, weights[0]) # 隐藏函数,调用运算函数
print("input的值是",input)
print("weights[0]", weights[0])
print("hid的值是:", hid)
# 隐藏层的计算规则
# input的值是 [8.5, 0.65, 1.2]
# weights[0] [[0.1, 0.2, -0.1], [-0.1, 0.1, 0.9], [0.1, 0.4, 0.1]]
# hid的值是: [0.8600000000000001, 0.29499999999999993, 1.23]
# 上述hid的第1个元素0.8600000000000001计算:8.5*0.1+0.65*0.2+1.2*(-0.1)
# 上述hid的第2个元素0.29499999999999993计算:8.5*(-0.1)+0.65*0.1+1.2*0.9
# 上述hid的第3个元素计算:[8.5, 0.65, 1.2]与[0.1, 0.4, 0.1]求取点积
pred = vect_mat_mul(hid, weights[1]) # 预测函数,调用运算函数
print("weights[1]", weights[1])
# hid的值是: [0.8600000000000001, 0.29499999999999993, 1.23]
# weights[1] [[0.3, 1.1, -0.3], [0.1, 0.2, 0.0], [0.0, 1.2, 0.1]]
# pred的值是: [0.21350000000000002, 0.14500000000000002, 0.4769999999999999]
print("pred的值是:", pred)
return pred # 返回预测结果
a1 = [8.5, 9.5, 9.9, 9.0] # 样本1,输入样本
a2 = [0.65, 0.8, 0.8, 0.9] # 样本2
a3 = [1.2, 1.3, 0.5, 1.0] # 样本3
for j in range(len(a1)):
input = [a1[j], a2[j], a3[j]] # 将函数的第一个作为样板输入
pred = neural_network(input, weights) # 调用预测函数进行预测
print("****************************************************************")
# print("input的值是",input)
# print("weights[0]", weights[0])
print("第", j, "次neural_network预测的结果是:", pred)
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
#第一次是a1[0]*weights[0][0]+a2[0]*weights[0][1]+a3[0]*weights[0][2]
#0.65*0.2+0.65*0.1+0.65*0.4
输出结果为
"E:\Python Test1\venv\Scripts\python.exe" "E:/Python Test1/more_pred.py"
input的值是 [8.5, 0.65, 1.2]
weights[0] [[0.1, 0.2, -0.1], [-0.1, 0.1, 0.9], [0.1, 0.4, 0.1]]
hid的值是: [0.8600000000000001, 0.29499999999999993, 1.23]
weights[1] [[0.3, 1.1, -0.3], [0.1, 0.2, 0.0], [0.0, 1.2, 0.1]]
pred的值是: [0.21350000000000002, 0.14500000000000002, 0.4769999999999999]
****************************************************************
第 0 次neural_network预测的结果是: [0.21350000000000002, 0.14500000000000002, 0.4769999999999999]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
input的值是 [9.5, 0.8, 1.3]
weights[0] [[0.1, 0.2, -0.1], [-0.1, 0.1, 0.9], [0.1, 0.4, 0.1]]
hid的值是: [0.9800000000000001, 0.30000000000000004, 1.4]
weights[1] [[0.3, 1.1, -0.3], [0.1, 0.2, 0.0], [0.0, 1.2, 0.1]]
pred的值是: [0.20400000000000013, 0.15800000000000003, 0.5]
****************************************************************
第 1 次neural_network预测的结果是: [0.20400000000000013, 0.15800000000000003, 0.5]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
input的值是 [9.9, 0.8, 0.5]
weights[0] [[0.1, 0.2, -0.1], [-0.1, 0.1, 0.9], [0.1, 0.4, 0.1]]
hid的值是: [1.1, -0.46000000000000013, 1.36]
weights[1] [[0.3, 1.1, -0.3], [0.1, 0.2, 0.0], [0.0, 1.2, 0.1]]
pred的值是: [-0.5840000000000003, 0.017999999999999988, -0.41600000000000015]
****************************************************************
第 2 次neural_network预测的结果是: [-0.5840000000000003, 0.017999999999999988, -0.41600000000000015]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
input的值是 [9.0, 0.9, 1.0]
weights[0] [[0.1, 0.2, -0.1], [-0.1, 0.1, 0.9], [0.1, 0.4, 0.1]]
hid的值是: [0.9800000000000001, 0.08999999999999997, 1.36]
weights[1] [[0.3, 1.1, -0.3], [0.1, 0.2, 0.0], [0.0, 1.2, 0.1]]
pred的值是: [-0.015000000000000013, 0.11600000000000002, 0.24399999999999997]
****************************************************************
第 3 次neural_network预测的结果是: [-0.015000000000000013, 0.11600000000000002, 0.24399999999999997]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
进程已结束,退出代码0