【深度学习】1.2:简单神经网络的python实现
2017-02-04 09:53
1206 查看
资料参考:
[1]知乎专栏总结:https//zhuanlan.zhihu.com/p/21423252
[2]Youtube上的视频network demystified playlist
以下实现神经网络,但此处的矩阵是Youtube视频上的转置
参数的选取:
learn_rate:步子大了,容易跳过最优点;步子小了,容易陷入局部最优
hidden_nodes: 隐藏层节点个数多了,容易过拟合,节点个数少,容易欠拟合
epho: 迭代次数多了,容易过拟合,时间长;次数少了,容易欠拟合
[1]知乎专栏总结:https//zhuanlan.zhihu.com/p/21423252
[2]Youtube上的视频network demystified playlist
以下实现神经网络,但此处的矩阵是Youtube视频上的转置
参数的选取:
learn_rate:步子大了,容易跳过最优点;步子小了,容易陷入局部最优
hidden_nodes: 隐藏层节点个数多了,容易过拟合,节点个数少,容易欠拟合
epho: 迭代次数多了,容易过拟合,时间长;次数少了,容易欠拟合
代码块
#有56个输入节点,2个隐藏节点,1个输出节点,做个示例 #隐藏层激活函数为sigmoid,输出层为f(x)=x. class NeuralNetwork(object): def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate): # Set number of nodes in input, hidden and output layers. self.input_nodes = input_nodes self.hidden_nodes = hidden_nodes self.output_nodes = output_nodes # Initialize weights,注意权重的shape self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5, (self.hidden_nodes, self.input_nodes)) self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5, (self.output_nodes, self.hidden_nodes)) self.lr = learning_rate #### Set this to your implemented sigmoid function #### # Activation function is the sigmoid function def sigmoid(x): return 1 / (1 + np.exp(-x)) self.activation_function = sigmoid def train(self, inputs_list, targets_list): # Convert inputs list to 2d array inputs = np.array(inputs_list, ndmin=2).T # inputs的shape为 [feature_diemension, 1]=[56,1],1是指1个实例 targets = np.array(targets_list, ndmin=2).T#[1,1] #print("input",inputs.shape) #print("targets",targets.shape) #### Implement the forward pass here #### ### Forward pass ### # TODO: Hidden layer hidden_inputs = np.dot(self.weights_input_to_hidden,inputs)#hidden_inputs是[hidden_nodes,1]=[2,1] hidden_outputs = self.activation_function(hidden_inputs)#hidden_outputs是[hidden_nodes,1]=[2,1] #print("hidden_inputs",(hidden_inputs.shape)) #print(("hidden_outputs"),hidden_outputs.shape) # TODO: Output layer final_inputs = np.dot(self.weights_hidden_to_output,hidden_outputs)#[output_nodes,1] final_outputs = final_inputs #[output,input]=[1,1] #print("final_inputs",final_inputs.shape) #print("final_outputs",final_outputs.shape) #### Implement the backward pass here #### ### Backward pass ### # TODO: Output error output_errors = targets-final_outputs #[output,1] # TODO: Backpropagated error hidden_errors = np.dot(self.weights_hidden_to_output.T, output_errors)#[hidden_nodes,1] hidden_grads = hidden_outputs * ( 1-hidden_outputs )#[hidden_nodes,1] # Update the weights # update hidden-to-output weights with gradient descent step self.weights_hidden_to_output += np.dot(output_errors, hidden_outputs.T) * self.lr #[output,hidden] # update input-to-hidden weights with gradient descent step self.weights_input_to_hidden += np.dot(hidden_errors * hidden_grads, inputs.T) * self.lr #[hidden,input] def run(self, inputs_list): # Run a forward pass through the network inputs = np.array(inputs_list, ndmin=2).T #### Implement the forward pass here #### # TODO: Hidden layer hidden_inputs = np.dot(self.weights_input_to_hidden,inputs) hidden_outputs = self.activation_function(hidden_inputs) # TODO: Output layer final_inputs = np.dot(self.weights_hidden_to_output,hidden_outputs) final_outputs = final_inputs return final_outputs
相关文章推荐
- [置顶] 【深度学习】RNN循环神经网络Python简单实现
- 深度学习5:python实现三层神经网络
- 深度学习第一课 第四周 深层神经网络用python的实现
- 小白学习机器学习---第五章:神经网络简单模型python实现
- 吴恩达 深度学习第三周 浅层神经网络 logistic_regression python代码实现
- 深度学习与神经网络-吴恩达(Part1Week3)-单隐层神经网络编程实现(python)
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
- [action] deep learning 深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- 深度学习基础(二):简单神经网络,后向传播算法及实现
- 神经网络与深度学习笔记(二)python 实现随机梯度下降
- (尤其是训练集验证集的生成)深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- 深度学习与神经网络-吴恩达(Part1Week4)-深度神经网络编程实现(python)-基础篇
- 深度学习中简单神经网络的实现
- 深度学习论文-神经网络的代码实现(python版本)
- 【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
- 深度学习笔记(二):简单神经网络,后向传播算法及实现
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- Python实现深度学习之-神经网络识别手写数字(更新中,更新日期:2017-07-12)
- TensorFlow:实战Google深度学习框架(二)实现简单神经网络