numpy-train-study2.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #简单神经网络测试实例
  2. import numpy as np
  3. def tanh(x): #双曲函数
  4. return np.tanh(x)
  5. def tanh_deriv(x):#更新权重时,需要用到双曲函数的倒数
  6. return 1.0 - np.tanh(x)*np.tanh(x)
  7. def logistic(x):#构建逻辑函数
  8. return 1/(1 + np.exp(-x))
  9. def logistic_derivatic(x): #逻辑函数的倒数
  10. return logistic(x)*(1 - logistic(x))
  11. class NeuralNetwork:
  12. def __init__(self,layer,activation='tanh'):
  13. '''
  14. :param layer:A list containing the number of unit in each layer.
  15. Should be at least two values.每层包含的神经元数目
  16. :param activation: the activation function to be used.Can be
  17. "logistic" or "tanh"
  18. '''
  19. if activation == 'logistic':
  20. self.activation = logistic
  21. self.activation_deriv = logistic_derivatic
  22. elif activation == 'tanh':
  23. self.activation = tanh
  24. self.activation_deriv = tanh_deriv
  25. self.weights = []
  26. for i in range(1,len(layer) - 1):#权重的设置
  27. self.weights.append((2*np.random.random((layer[i - 1] + 1,layer[i] + 1))-1)*0.25)
  28. self.weights.append((2*np.random.random((layer[i] + 1,layer[i+1]))-1)*0.25)
  29. '''训练神经网络,通过传入的数据,不断更新权重weights'''
  30. def fit(self,X,y,learning_rate=0.2,epochs=10000):
  31. '''
  32. :param X: 数据集
  33. :param y: 数据输出结果,分类标记
  34. :param learning_rate: 学习率
  35. :param epochs: 随机抽取的数据的训练次数
  36. :return:
  37. '''
  38. X = np.atleast_2d(X) #转化X为np数据类型,试数据类型至少是两维的
  39. temp = np.ones([X.shape[0],X.shape[1]+1])
  40. temp[:,0:-1] = X
  41. X = temp
  42. y = np.array(y)
  43. for k in range(epochs):
  44. i = np.random.randint(X.shape[0]) #随机抽取的行
  45. a = [X[i]]
  46. for I in range(len(self.weights)):#完成正向所有的更新
  47. a.append(self.activation(np.dot(a[I],self.weights[I])))#dot():对应位相乘后相加
  48. error = y[i] - a[-1]
  49. deltas = [error * self.activation_deriv(a[-1])]#*self.activation_deriv(a[I])#输出层误差
  50. # 反向更新
  51. for I in range(len(a) -2,0,-1):
  52. deltas.append(deltas[-1].dot(self.weights[I].T)*self.activation_deriv(a[I]))
  53. deltas.reverse()
  54. for i in range(len(self.weights)):
  55. layer = np.atleast_2d(a[i])
  56. delta = np.atleast_2d(deltas[i])
  57. self.weights[i] += learning_rate*layer.T.dot(delta)
  58. def predict(self,x):
  59. x = np.array(x)
  60. temp = np.ones(x.shape[0] + 1)
  61. temp[0:-1] = x
  62. a = temp
  63. for I in range(0,len(self.weights)):
  64. a = self.activation(np.dot(a,self.weights[I]))
  65. return a #只需要保存最后的值,就是预测出来的值
  66. nn = NeuralNetwork([2,2,1], 'tanh')
  67. X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  68. y = np.array([0, 1, 1, 0])
  69. nn.fit(X, y)
  70. for i in [[0, 0], [0, 1], [1, 0], [1,1]]:
  71. print(i, nn.predict(i))
  72. #from sklearn.datasets import load_digits #导入数据集
  73. #from sklearn.metrics import confusion_matrix,classification_report #对结果的预测的包
  74. #from sklearn.preprocessing import LabelBinarizer #把数据转化为二维的数字类型
  75. #from sklearn.cross_validation import train_test_split #可以把数据拆分成训练集与数据集
  76. #digits = load_digits() #把数据改成0到1之间
  77. #X = digits.data
  78. #y = digits.target
  79. #X -= X.min()
  80. #X /= X.max()
  81. #nn = NeuralNetwork([64,100,10],'logistic')
  82. #X_train,X_test,y_train,y_test = train_test_split(X,y)
  83. #labels_train = LabelBinarizer().fit_transform(y_train)
  84. #labels_test = LabelBinarizer().fit_transform(y_test)
  85. #print("start fitting")
  86. #nn.fit(X_train,labels_train,epochs=3000)
  87. #predictions = []
  88. #for i in range(X_test.shape[0]):
  89. # o = nn.predict(X_test[i])
  90. # predictions.append(np.argmax(o))
  91. #print(confusion_matrix(y_test,predictions))
  92. #print(classification_report(y_test,predictions))