第1关:神经网络概述

  • A
  • ABCD
  • A


第2关:BP 神经网络

  1. import numpy as np
  2. def loaddataset(filename):
  3. fp = open(filename)
  4. #存放数据
  5. dataset = []
  6. #存放标签
  7. labelset = []
  8. for i in fp.readlines():
  9. a = i.strip().split()
  10. #每个数据行的最后一个是标签数据
  11. dataset.append([float(j) for j in a[:len(a)-1]])
  12. labelset.append(int(float(a[-1])))
  13. return dataset, labelset
  14. #x为输入层神经元个数,y为隐层神经元个数,z输出层神经元个数
  15. def parameter_initialization(x, y, z):
  16. #隐层阈值
  17. value1 = np.random.randint(-5, 5, (1, y)).astype(np.float64)
  18. #输出层阈值
  19. value2 = np.random.randint(-5, 5, (1, z)).astype(np.float64)
  20. #输入层与隐层的连接权重
  21. weight1 = np.random.randint(-5, 5, (x, y)).astype(np.float64)
  22. #隐层与输出层的连接权重
  23. weight2 = np.random.randint(-5, 5, (y, z)).astype(np.float64)
  24. return weight1, weight2, value1, value2
  25. #返回sigmoid函数值
  26. def sigmoid(z):
  27. #********** Begin **********#
  28. return 1/(1+np.exp(-z))
  29. #********** End **********#
  30. '''
  31. weight1:输入层与隐层的连接权重
  32. weight2:隐层与输出层的连接权重
  33. value1:隐层阈值
  34. value2:输出层阈值
  35. '''
  36. def trainning(dataset, labelset, weight1, weight2, value1, value2):
  37. #dataset:数据集 labelset:标签数据
  38. #x为步长
  39. x = 0.01
  40. for i in range(len(dataset)):
  41. #输入数据
  42. inputset = np.mat(dataset[i]).astype(np.float64)
  43. #数据标签
  44. outputset = np.mat(labelset[i]).astype(np.float64)
  45. #隐层输入
  46. input1 = np.dot(inputset, weight1).astype(np.float64)
  47. #隐层输出
  48. #********** Begin **********#
  49. output2 = sigmoid(input1 - value1).astype(np.float64)
  50. #********** End **********#
  51. #输出层输入
  52. input2 = np.dot(output2, weight2).astype(np.float64)
  53. #输出层输出
  54. output3 = sigmoid(input2 - value2).astype(np.float64)
  55. #更新公式由矩阵运算表示
  56. a = np.multiply(output3, 1 - output3)
  57. g = np.multiply(a, outputset - output3)
  58. b = np.dot(g, np.transpose(weight2))
  59. c = np.multiply(output2, 1 - output2)
  60. e = np.multiply(b, c)
  61. value1_change = -x * e
  62. value2_change = -x * g
  63. weight1_change = x * np.dot(np.transpose(inputset), e)
  64. weight2_change = x * np.dot(np.transpose(output2), g)
  65. #更新连接权重、阈值参数value1、value2、weight1、weight2
  66. #********** Begin **********#
  67. value1 -= value1_change
  68. value2 -= value2_change
  69. weight1 -= weight1_change
  70. weight2 -= weight2_change
  71. #********** End **********#
  72. return weight1, weight2, value1, value2
  73. def testing(dataset, labelset, weight1, weight2, value1, value2):
  74. #记录预测正确的个数
  75. rightcount = 0
  76. for i in range(len(dataset)):
  77. #计算每一个样例通过该神经网路后的预测值
  78. inputset = np.mat(dataset[i]).astype(np.float64)
  79. outputset = np.mat(labelset[i]).astype(np.float64)
  80. output2 = sigmoid(np.dot(inputset, weight1) - value1)
  81. output3 = sigmoid(np.dot(output2, weight2) - value2)
  82. #确定其预测标签:输出大于 0.5 置 flag 为 1,否则置 flag 为 0
  83. #********** Begin **********#
  84. flag = 0
  85. if output3 > 0.5:
  86. flag = 1
  87. #********** End **********#
  88. if labelset[i] == flag:
  89. rightcount += 1
  90. #返回正确率
  91. return rightcount / len(dataset)


第3关:Hopfield 神经网络


  1. import numpy as np
  2. import random
  3. from random import randint
  4. from matplotlib import pyplot as plt
  5. #根据Hebb学习规则计算神经元之间的连接权值
  6. def calcWeight(savedsample):
  7. N = len(savedsample[0])
  8. P = len(savedsample)
  9. mat = [0]*N
  10. returnMat = []
  11. for i in range(N):
  12. m = mat[:]
  13. returnMat.append(m)
  14. for i in range(N):
  15. for j in range(N):
  16. if i==j:
  17. continue
  18. sum = 0
  19. for u in range(P):
  20. sum += savedsample[u][i] * savedsample[u][j]
  21. returnMat[i][j] = sum/float(N)
  22. return returnMat
  23. #根据神经元的输入计算神经元的输出(静态突触)
  24. def calcXi(inMat , weighMat):
  25. #假设计算第t次循环后神经元的输出时,输入的参数inMat表示第t-1次循环后神经元的输出。即用上一次循环的输出做本次循环的输入。
  26. #weighMat权值矩阵
  27. returnMat = inMat
  28. choose = []
  29. for i in range(len(inMat)//5):
  30. #随机改变N/5个神经元的值,该参数可调,也可同时改变所有神经元的值
  31. choose.append(random.randint(0,len(inMat)-1))
  32. for i in choose:
  33. sum = 0
  34. #网络动态演变
  35. #********** Begin **********#
  36. for j in range(len(inMat)):
  37. sum += weighMat[i][j]*inMat[j]
  38. if sum >= 0:
  39. returnMat[i] = 1
  40. else:
  41. returnMat[i] = -1
  42. #********** End **********#
  43. return returnMat
  44. #加噪函数,在记忆样本的基础上增加30%的噪声:
  45. def addnoise(mytest_data,n):
  46. #mytest_data:网络矩阵 n:节点数
  47. #********** Begin **********#
  48. for i in range(n):
  49. for j in range(n):
  50. if random.randint(0, 10)>7:
  51. mytest_data[i*n+j] *= -1
  52. #********** End **********#
  53. return mytest_data
  54. #标准输出函数:data 为联想记忆后的矩阵,N 为方阵行数:
  55. def regularout(data,N):
  56. for j in range(N):
  57. ch = ""
  58. for i in range(N):
  59. #矩阵元素值为 1 则输出“ X ”,否则输出“ ”
  60. #********** Begin **********#
  61. ch += "X" if data[j*N+i]==1 else " "
  62. #********** Begin **********#
  63. print(ch)



第4关:卷积神经网络

  1. import numpy as np
  2. def my_conv(inputmatrix,kernel):
  3. output_size = (len(inputmatrix)-len(kernel)+1)
  4. res = np.zeros([output_size,output_size],np.float32)
  5. for i in range(len(res)):
  6. for j in range(len(res)):
  7. res[i][j] = compute_conv(inputmatrix,kernel,i,j)
  8. return res
  9. #两个矩阵的卷积算法
  10. def compute_conv(inputmatrix,kernel,i,j):
  11. #inputmatrix为输入矩阵,kernel为卷积核,i,j分别是每次卷积运算的首元素地址
  12. res = 0
  13. #********** Begin **********#
  14. for x in range(len(kernel)):
  15. for y in range(len(kernel[0])):
  16. res += kernel[x][y] * inputmatrix[i+x][j+y]
  17. #********** End **********#
  18. return res