前段时间,我开始了机器学习的冒险之旅(在我最近 2 年的学习期间)。我读了很多书,写了很多机器学习算法的代码,除了神经网络,这些超出了我的范围。我对这个话题很感兴趣,但是我有一个很大的问题: 我读过的所有书都有两个主要问题:
- 包含大量数学方程式。课后我对它们非常熟悉,而且我可以在纸上手工计算。
- 包含嵌入在某些复杂上下文中的大型示例(例如调查网店销售率),并且要深入了解神经网络实现,我必须编写大量代码来重现上下文。 缺少什么 - 简单直接的实现,没有大量上下文和方程式。
您能否告诉我,我在哪里可以找到多层感知(神经网络)的简单实现?我不需要理论知识,也不需要上下文嵌入的示例。我更喜欢一些脚本语言来节省时间和精力 - 我以前的工作 99% 都是用 Python 完成的。
这是我以前读过的书(但没有找到我想要的):
- 机器学习实践
- 编程集体智慧
- 机器学习:算法视角
- Java 中的神经网络简介
- C# 中的神经网络简介
最佳答案
一个简单的实现
这是一个使用 Python
中的类的可读实现。此实现以效率换取可理解性:
import math
import random
BIAS = -1
"""
To view the structure of the Neural Network, type
print network_name
"""
class Neuron:
def __init__(self, n_inputs ):
self.n_inputs = n_inputs
self.set_weights( [random.uniform(0,1) for x in range(0,n_inputs+1)] ) # +1 for bias weight
def sum(self, inputs ):
# Does not include the bias
return sum(val*self.weights[i] for i,val in enumerate(inputs))
def set_weights(self, weights ):
self.weights = weights
def __str__(self):
return 'Weights: %s, Bias: %s' % ( str(self.weights[:-1]),str(self.weights[-1]) )
class NeuronLayer:
def __init__(self, n_neurons, n_inputs):
self.n_neurons = n_neurons
self.neurons = [Neuron( n_inputs ) for _ in range(0,self.n_neurons)]
def __str__(self):
return 'Layer:\n\t'+'\n\t'.join([str(neuron) for neuron in self.neurons])+''
class NeuralNetwork:
def __init__(self, n_inputs, n_outputs, n_neurons_to_hl, n_hidden_layers):
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.n_hidden_layers = n_hidden_layers
self.n_neurons_to_hl = n_neurons_to_hl
# Do not touch
self._create_network()
self._n_weights = None
# end
def _create_network(self):
if self.n_hidden_layers>0:
# create the first layer
self.layers = [NeuronLayer( self.n_neurons_to_hl,self.n_inputs )]
# create hidden layers
self.layers += [NeuronLayer( self.n_neurons_to_hl,self.n_neurons_to_hl ) for _ in range(0,self.n_hidden_layers)]
# hidden-to-output layer
self.layers += [NeuronLayer( self.n_outputs,self.n_neurons_to_hl )]
else:
# If we don't require hidden layers
self.layers = [NeuronLayer( self.n_outputs,self.n_inputs )]
def get_weights(self):
weights = []
for layer in self.layers:
for neuron in layer.neurons:
weights += neuron.weights
return weights
@property
def n_weights(self):
if not self._n_weights:
self._n_weights = 0
for layer in self.layers:
for neuron in layer.neurons:
self._n_weights += neuron.n_inputs+1 # +1 for bias weight
return self._n_weights
def set_weights(self, weights ):
assert len(weights)==self.n_weights, "Incorrect amount of weights."
stop = 0
for layer in self.layers:
for neuron in layer.neurons:
start, stop = stop, stop+(neuron.n_inputs+1)
neuron.set_weights( weights[start:stop] )
return self
def update(self, inputs ):
assert len(inputs)==self.n_inputs, "Incorrect amount of inputs."
for layer in self.layers:
outputs = []
for neuron in layer.neurons:
tot = neuron.sum(inputs) + neuron.weights[-1]*BIAS
outputs.append( self.sigmoid(tot) )
inputs = outputs
return outputs
def sigmoid(self, activation,response=1 ):
# the activation function
try:
return 1/(1+math.e**(-activation/response))
except OverflowError:
return float("inf")
def __str__(self):
return '\n'.join([str(i+1)+' '+str(layer) for i,layer in enumerate(self.layers)])
更高效的实现(带学习)
如果您正在寻找更有效的学习(反向传播)神经网络示例,请查看我的 neural network Github repository here .
关于python - 简单的多层神经网络实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15395835/