鱼C论坛

 找回密码
 立即注册
查看: 870|回复: 12

为什么我输出的Draw_H都是[1 1 1]

[复制链接]
发表于 2020-5-19 13:32:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
    for c in range(n):
        # H = np.zeros([3])
        #print(Weight_Hid_Out)
        n_h1 = np.dot(Weight_In_Hid[:,0].T,x_Drawing_In1Out1[c])
        n_h2 = np.dot(Weight_In_Hid[:,1].T,x_Drawing_In1Out1[c])
        
        h0 = 1.0                       
        h1 = 1.0 / (1+np.exp(-n_h1))
        h2 = 1.0 / (1+np.exp(-n_h2))
        Draw_H = [h0,h1,h2]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 13:33:45 | 显示全部楼层
发下完整代码吧

如何正确地发代码、上传图片和附件?
https://fishc.com.cn/thread-52272-1-1.html
(出处: 鱼C论坛)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 13:40:16 | 显示全部楼层
数量积都为零?检查下这边的数据第1列和第2列:
  1. n_h1 = np.dot(Weight_In_Hid[:,0].T,x_Drawing_In1Out1[c])
  2.         n_h2 = np.dot(Weight_In_Hid[:,1].T,x_Drawing_In1Out1[c])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 13:46:46 | 显示全部楼层
Twilight6 发表于 2020-5-19 13:33
发下完整代码吧

如何正确地发代码、上传图片和附件?
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import xlwt

  5. data = pd.read_excel(io='D:/人工智能/作业/Homework7.xlsx', sheet_name='In1Out1')  
  6. data.insert(loc=1, column='x0', value=1)     # 在每行数据的第二列添加x0=1
  7. x_train = data.iloc[:, 1:3].to_numpy()       # 抽取第二三列的数据,转换成数组格式
  8. targets = data.iloc[:, 3].to_numpy()         # 抽取第四列targets

  9. data1 = pd.read_excel(io='D:/人工智能/作业/Homework7.xlsx', sheet_name='Drawing_In1Out1')
  10.    
  11. data1.insert(loc=1, column='x0', value=1)
  12. x_Drawing_In1Out1 = data1.iloc[:, 1:3].to_numpy()
  13. x1 = data1.iloc[1:,2].to_numpy()
  14. y1 = data1.iloc[1:,3].to_numpy()

  15. DeltaHid = 0.3
  16. DeltaOut = 0.001
  17. α = 0.1

  18. CCount = 0                                   # 初始化计数变量

  19. n = x_train.shape[0]                         # 得到提取数据的数量

  20. Max_iteration = int(input('Total Iteration:'))         # 输入最大迭代次数

  21. Weight_In_Hid = np.random.normal(size=(2,2))
  22. Weight_Hid_Out = np.random.normal(size=(3,1))

  23. Weight_In_Hid_Delta =np.zeros((2,2))
  24. Weight_Hid_Out_Delta = np.zeros((3,1))

  25. for i in range(Max_iteration):               
  26.     for k in range(n):                          

  27.         H = np.zeros([3])
  28.         
  29.         neth1 = np.dot(Weight_In_Hid[:,0],x_train[k])
  30.         neth2 = np.dot(Weight_In_Hid[:,1],x_train[k])

  31.         H[0] = 1.0                       
  32.         H[1] = 1/(1+np.e**(-neth1))
  33.         H[2] = 1/(1+np.e**(-neth2))
  34.         # 确定输出层
  35.         
  36.         Y = np.dot(Weight_Hid_Out.T,H)                     # Y值
  37.         
  38.         Delta_Out = targets[k] - Y
  39.         
  40.         Delta_Hid = np.zeros((2,1))
  41.         Delta_Hid[0] = Delta_Out * Weight_Hid_Out[1] * H[1] * (1-H[1])
  42.         Delta_Hid[1] = Delta_Out * Weight_Hid_Out[2] * H[2] * (1-H[2])
  43.         for a in range(3):
  44.             Weight_Hid_Out_Delta[a] = α * Weight_Hid_Out_Delta[a] + DeltaOut * Delta_Out * H[a]
  45.         for b in range(2):
  46.             for j in range(1,3):
  47.                 Weight_In_Hid_Delta[b,j-1] = α *  Weight_In_Hid_Delta[b,j-1] + DeltaHid * x_train[k,b]
  48.         Weight_In_Hid = Weight_In_Hid + Weight_In_Hid_Delta
  49.         Weight_Hid_Out = Weight_Hid_Out + Weight_Hid_Out_Delta
  50.       
  51.         
  52.         CCount += 1                          # 计数

  53.         if CCount == n:                      # 当循环次数等于数据数时跳出循环进行下一次循环
  54.             break
  55.    
  56.     print('Weight_In_Hid:')
  57.     print(Weight_In_Hid)
  58.     print('Weight_Hid_Out:')
  59.     print(Weight_Hid_Out)
  60.    
  61.     for c in range(n):
  62.       
  63.         n_h1 = np.dot(Weight_In_Hid[:,0].T,x_Drawing_In1Out1[c])
  64.         n_h2 = np.dot(Weight_In_Hid[:,1].T,x_Drawing_In1Out1[c])
  65.         
  66.         h0 = 1.0                       
  67.         h1 = 1.0 / (1+np.exp(-n_h1))
  68.         h2 = 1.0 / (1+np.exp(-n_h2))
  69.         Draw_H = [h0,h1,h2]
  70.         # 确定输出层
  71.         
  72.         Draw_Y = np.zeros([n])
  73.         Draw_Y[c] = np.dot(Weight_Hid_Out.T[0],Draw_H)# Y值

  74. # 画图

  75. plt.ion()
  76. plt.cla()                                # 清空画布
  77. plt.ylim(-1, 4)                          # 固定y轴的取值范围
  78. plt.xlim(-10, 10)                          # 固定x轴的取值范围
  79. x = np.linspace(-10, 10, 8)
  80.                
  81. plt.plot(x1,y1, color='red')
  82.       
  83. plt.plot(x_Drawing_In1Out1[:,1],Draw_Y,color='blue')
  84.         
  85. plt.pause(0.1)           # 暂停一会
  86.    

  87. plt.ioff()                                   # 关闭交互
  88. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 13:48:39 | 显示全部楼层

我刚刚忘记改评论了,发现你是numpy 处理数据发全代码也没得测试哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 13:51:28 | 显示全部楼层

你试着把自己一些地方数据加上print ,自己人工调试下看下哪里数据有问题

  1. x_train = data.iloc[:, 1:3].to_numpy()
复制代码

试着加上print 看下数据有没问题
  1. Weight_In_Hid = np.random.normal(size=(2, 2))
  2. Weight_Hid_Out = np.random.normal(size=(3, 1))
复制代码

这里也试试 , 这种情况一般只能自己慢慢测了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 13:51:41 | 显示全部楼层
Twilight6 发表于 2020-5-19 13:48
我刚刚忘记改评论了,发现你是numpy 处理数据发全代码也没得测试哈哈

呜呜呜呜,好吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 13:53:10 | 显示全部楼层
Wss312 发表于 2020-5-19 13:51
呜呜呜呜,好吧

幸苦了 ,你可用试试我刚刚建议你的

如果对你有帮助,记得设置最佳哈~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 13:53:57 | 显示全部楼层
Twilight6 发表于 2020-5-19 13:40
数量积都为零?检查下这边的数据第1列和第2列:

第一列和第二列都是正确的数据,不知道为什么算出来就不对了,而且画出图来就是一条直线。我上传了完整代码,可以麻烦看一下吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 13:58:06 | 显示全部楼层
Twilight6 发表于 2020-5-19 13:51
你试着把自己一些地方数据加上print ,自己人工调试下看下哪里数据有问题

好的,我也试了,就是H值算的老是1,不知道哪错了,感觉式子都对。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 13:58:24 | 显示全部楼层
Wss312 发表于 2020-5-19 13:53
第一列和第二列都是正确的数据,不知道为什么算出来就不对了,而且画出图来就是一条直线。我上传了完整代 ...
  1. n_h1 = np.dot(Weight_In_Hid[:, 0].T, x_Drawing_In1Out1[c])
  2. n_h2 = np.dot(Weight_In_Hid[:, 1].T, x_Drawing_In1Out1[c])
复制代码

这下面你print一下试试?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 14:06:13 | 显示全部楼层
Twilight6 发表于 2020-5-19 13:58
这下面你print一下试试?

print的值都差不多,太难了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 14:07:24 | 显示全部楼层
Wss312 发表于 2020-5-19 14:06
print的值都差不多,太难了

同情你  哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 12:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表