6.2神经网络算法实现--python机器学习
来源:互联网 发布:淘宝销售数据 编辑:程序博客网 时间:2024/05/20 18:19
参考彭亮老师的视频教程:转载请注明出处及彭亮老师原创
视频教程: http://pan.baidu.com/s/1kVNe5EJ
1. 关于非线性转化方程(non-linear transformation function)
sigmoid函数(S 曲线)用来作为activation function:
1.1 双曲函数(tanh)
1.2 逻辑函数(logistic function)
2. 实现一个简单的神经网络算法
import numpy as np
def tanh(x):
return np.tanh(x)
def tanh_deriv(x):
return 1.0 - np.tanh(x)*np.tanh(x)
def logistic(x):
return 1/(1 + np.exp(-x))
def logistic_derivative(x):
return logistic(x)*(1-logistic(x))
class NeuralNetwork:
def __init__(self, layers, activation='tanh'):
"""
:param layers: A list containing the number of units in each layer.
Should be at least two values
:param activation: The activation function to be used. Can be
"logistic" or "tanh"
"""
if activation == 'logistic':
self.activation = logistic
self.activation_deriv = logistic_derivative
elif activation == 'tanh':
self.activation = tanh
self.activation_deriv = tanh_deriv
self.weights = []
for i in range(1, len(layers) - 1):
self.weights.append((2*np.random.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)
self.weights.append((2*np.random.random((layers[i] + 1, layers[i + 1]))-1)*0.25)
def fit(self, X, y, learning_rate=0.2, epochs=10000):
X = np.atleast_2d(X)
temp = np.ones([X.shape[0], X.shape[1]+1])
temp[:, 0:-1] = X # adding the bias unit to the input layer
X = temp
y = np.array(y)
for k in range(epochs):
i = np.random.randint(X.shape[0])
a = [X[i]]
for l in range(len(self.weights)): #going forward network, for each layer
a.append(self.activation(np.dot(a[l], self.weights[l]))) #Computer the node value for each layer (O_i) using activation function
error = y[i] - a[-1] #Computer the error at the top layer
deltas = [error * self.activation_deriv(a[-1])] #For output layer, Err calculation (delta is updated error)
#Staring backprobagation
for l in range(len(a) - 2, 0, -1): # we need to begin at the second to last layer
#Compute the updated error (i,e, deltas) for each node going from top layer to input layer
deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))
deltas.reverse()
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate * layer.T.dot(delta)
def predict(self, x):
x = np.array(x)
temp = np.ones(x.shape[0]+1)
temp[0:-1] = x
a = temp
for l in range(0, len(self.weights)):
a = self.activation(np.dot(a, self.weights[l]))
return a
1 0
- 6.2神经网络算法实现--python机器学习
- 机器学习算法及代码实现--神经网络
- 机器学习算法及代码实现--神经网络
- 机器学习算法及代码实现--神经网络
- 机器学习算法实现03-神经网络
- 机器学习之深入理解神经网络理论基础、BP算法及其Python实现
- 机器学习实战——python实现SOM神经网络聚类算法
- 机器学习算法练习之(二):Python和Tensorflow分别实现简单的神经网络
- 机器学习算法-python实现
- 机器学习算法python实现
- 斯坦福机器学习课程神经网络作业的Python实现
- 机器学习--神经网络算法系列--卷积神经网络
- 6.1神经网络--python机器学习
- 机器学习----神经网络(python)
- 自己动手实现机器学习算法:神经网络(附源代码)
- 机器学习之神经网络算法
- 机器学习之神经网络算法
- Python机器学习(二)--kNN算法实现
- 有两个磁盘文件“A"和”B",各存放一行字母,先要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C"中去。
- 我的IT相关网址收藏
- Java知识点6
- java基础学习之final
- iOS开发 - 桥接
- 6.2神经网络算法实现--python机器学习
- Swift中if与switch语句使用一例
- 练习37——复习符号
- Java知识点7
- OSG点击场景中的某个物体,如何判断点击的物体,及处理此模型相关变换
- Android 解决ListView初始化时默认滑动到底部问题
- 《第一行代码》拾遗
- 瀑布流照片墙—RecyclerView(最新官方控件实现)
- 通过jQuery从服务器获取或设置Header的自定义参数值,如会话参数:SID。