import numpy as np
in_wgt = np.array([[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 = np.array([[0.3,1.1,-0.3], #hurt
[0.1,0.2,0.0], #win
[0.0,1.3,0.1]] ) #sad
weights = [in_wgt,hp_wgt]
def neural_network(input, weights): # input是样本输入
hid = input.dot(weights[0]) # 隐藏函数,调用运算函数
print("input的值是",input)
print("weights[0]的值为", weights[0])
pred = hid.dot(weights[1]) # 预测函数,调用运算函数
# 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]的计算为8.5*0.1-0.65*0.1+1.2*0.1 =0.905
print("hid的值为", hid)
print("weights[1]的值为", weights[1])
print("pred的值为", pred)
print("&&###"*10)
return pred
a1 = np.array([8.5, 9.5, 9.9, 9.0]) # 样本1,输入样本
a2 = np.array([0.65, 0.8, 0.8, 0.9]) # 样本2
a3 = np.array([1.2, 1.3, 0.5, 1.0]) # 样本3
# input1 = np.array([a1[0], a2[0], a3[0]])
# pred1 = neural_network(input1, weights)
# print("neural_network预测的结果是:", pred1)
for j in range(len(a1)):
input = np.array([a1[j], a2[j], a3[j]]) # 将函数的第一个作为样板输入
pred = neural_network(input, weights) # 调用预测函数进行预测
print("****************************************************************")
print("第", j, "次neural_network预测的结果是:", pred)
结果为
"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.905 2.245 -0.145]
weights[1]的值为 [[ 0.3 1.1 -0.3]
[ 0.1 0.2 0. ]
[ 0. 1.3 0.1]]
pred的值为 [ 0.496 1.256 -0.286]
&&###&&###&&###&&###&&###&&###&&###&&###&&###&&###
****************************************************************
第 0 次neural_network预测的结果是: [ 0.496 1.256 -0.286]
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的值为 [ 1. 2.5 -0.1]
weights[1]的值为 [[ 0.3 1.1 -0.3]
[ 0.1 0.2 0. ]
[ 0. 1.3 0.1]]
pred的值为 [ 0.55 1.47 -0.31]
&&###&&###&&###&&###&&###&&###&&###&&###&&###&&###
****************************************************************
第 1 次neural_network预测的结果是: [ 0.55 1.47 -0.31]
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的值为 [ 0.96 2.26 -0.22]
weights[1]的值为 [[ 0.3 1.1 -0.3]
[ 0.1 0.2 0. ]
[ 0. 1.3 0.1]]
pred的值为 [ 0.514 1.222 -0.31 ]
&&###&&###&&###&&###&&###&&###&&###&&###&&###&&###
****************************************************************
第 2 次neural_network预测的结果是: [ 0.514 1.222 -0.31 ]
input的值是 [9. 0.9 1. ]
weights[0]的值为 [[ 0.1 0.2 -0.1]
[-0.1 0.1 0.9]
[ 0.1 0.4 0.1]]
hid的值为 [0.91 2.29 0.01]
weights[1]的值为 [[ 0.3 1.1 -0.3]
[ 0.1 0.2 0. ]
[ 0. 1.3 0.1]]
pred的值为 [ 0.502 1.472 -0.272]
&&###&&###&&###&&###&&###&&###&&###&&###&&###&&###
****************************************************************
第 3 次neural_network预测的结果是: [ 0.502 1.472 -0.272]
进程已结束,退出代码0