Tho*_*oth 5 python performance julia
所以我在Python中有一些神经网络代码,我在Julia中重写了这些代码.直接的Python代码在大约7秒内运行,而Julia和PyPy代码在大约0.75秒内运行
sigmoid(z::Float64) = 1/(1 + exp(-z))
sigmoidPrime(z::Float64) = sigmoid(z) * (1 - sigmoid(z))
### Types ###
abstract AbstractNode
type Edge
source::AbstractNode
target::AbstractNode
weight::Float64
derivative::Float64
augmented::Bool
Edge(source::AbstractNode, target::AbstractNode) = new(source, target, randn(1,1)[1], 0.0, false)
end
type Node <: AbstractNode
incomingEdges::Vector{Edge}
outgoingEdges::Vector{Edge}
activation::Float64
activationPrime::Float64
Node() = new([], [], -1.0, -1.0)
end
type InputNode <: AbstractNode
index::Int
incomingEdges::Vector{Edge}
outgoingEdges::Vector{Edge}
activation::Float64
InputNode(index::Int) = new(index, [], [], -1.0)
end
type BiasNode <: AbstractNode
incomingEdges::Vector{Edge}
outgoingEdges::Vector{Edge}
activation::Float64
BiasNode() = new([], [], 1.0)
end
type Network
inputNodes::Vector{InputNode}
hiddenNodes::Vector{Node}
outputNodes::Vector{Node}
function Network(sizes::Array, bias::Bool=true)
inputNodes = [InputNode(i) for i in 1:sizes[1]];
hiddenNodes = [Node() for _ in 1:sizes[2]];
outputNodes = [Node() for _ in 1:sizes[3]];
for inputNode in inputNodes
for node in hiddenNodes
edge = Edge(inputNode, node);
push!(inputNode.outgoingEdges, edge)
push!(node.incomingEdges, edge)
end
end
for node in hiddenNodes
for outputNode in outputNodes
edge = Edge(node, outputNode);
push!(node.outgoingEdges, edge)
push!(outputNode.incomingEdges, edge)
end
end
if bias == true
biasNode = BiasNode()
for node in hiddenNodes
edge = Edge(biasNode, node);
push!(biasNode.outgoingEdges, edge)
push!(node.incomingEdges, edge)
end
end
new(inputNodes, hiddenNodes, outputNodes)
end
end
### Methods ###
function evaluate(obj::Node, inputVector::Array)
if obj.activation > -0.5
return obj.activation
else
weightedSum = sum([d.weight * evaluate(d.source, inputVector) for d in obj.incomingEdges])
obj.activation = sigmoid(weightedSum)
obj.activationPrime = sigmoidPrime(weightedSum)
return obj.activation
end
end
function evaluate(obj::InputNode, inputVector::Array)
obj.activation = inputVector[obj.index]
return obj.activation
end
function evaluate(obj::BiasNode, inputVector::Array)
obj.activation = 1.0
return obj.activation
end
function updateWeights(obj::AbstractNode, learningRate::Float64)
for d in obj.incomingEdges
if d.augmented == false
d.augmented = true
d.weight -= learningRate * d.derivative
updateWeights(d.source, learningRate)
d.derivative = 0.0
end
end
end
function compute(obj::Network, inputVector::Array)
output = [evaluate(node, inputVector) for node in obj.outputNodes]
for node in obj.outputNodes
clear(node)
end
return output
end
function clear(obj::AbstractNode)
for d in obj.incomingEdges
obj.activation = -1.0
obj.activationPrime = -1.0
d.augmented = false
clear(d.source)
end
end
function propagateDerivatives(obj::AbstractNode, error::Float64)
for d in obj.incomingEdges
if d.augmented == false
d.augmented = true
d.derivative += error * obj.activationPrime * d.source.activation
propagateDerivatives(d.source, error * d.weight * obj.activationPrime)
end
end
end
function backpropagation(obj::Network, example::Array)
output = [evaluate(node, example[1]) for node in obj.outputNodes]
error = output - example[2]
for (node, err) in zip(obj.outputNodes, error)
propagateDerivatives(node, err)
end
for node in obj.outputNodes
clear(node)
end
end
function train(obj::Network, labeledExamples::Array, learningRate::Float64=0.7, iterations::Int=10000)
for _ in 1:iterations
for ex in labeledExamples
backpropagation(obj, ex)
end
for node in obj.outputNodes
updateWeights(node, learningRate)
end
for node in obj.outputNodes
clear(node)
end
end
end
labeledExamples = Array[Array[[0,0,0], [0]],
Array[[0,0,1], [1]],
Array[[0,1,0], [0]],
Array[[0,1,1], [1]],
Array[[1,0,0], [0]],
Array[[1,0,1], [1]],
Array[[1,1,0], [1]],
Array[[1,1,1], [0]]];
neuralnetwork = Network([3,4,1])
@time train(neuralnetwork, labeledExamples)
Run Code Online (Sandbox Code Playgroud)
我没有提供Python代码,因为我不确定它是否有必要(但是如果你真的想要它我会的话),我当然不希望花大量的时间来完全理解这段代码,我只是基本上寻找与正确的Julia实现相关的明显/系统的低效率(与算法本身相反).
我这样做的动机是以这种方式设计神经网络比矢量化算法和使用Numpy要自然得多,但当然所有循环和跳过类结构的人在Python中都很慢.
因此,这似乎是移植到Julia的自然选择,看看我是否无法获得一些主要的加速,虽然比直接Python快一个数量级的速度很酷,我真正希望的是一个数量级的速度通过PyPy(我在网上找到的一些基准测试似乎表明这是一个合理的期望).
注意:这必须在Julia 0.3中运行才能工作
这似乎更像是一个代码审查而不是一个问题(没有任何问号),但无论如何我都会采取行动.唯一明显的潜在性能问题是你通过理解来分配数组evaluate,compute和backpropagation.evaluate作为for循环,加权和计算将更有效.对于其他两种方法,您可能希望使用预先分配的数组而不是理解.您可以使用Julia的内置分析器来查看代码在大部分时间内花费的时间 - 这可能会显示一些您可以进一步优化的非明显热点.
关于与PyPy的比较,Julia和PyPy很可能在这个代码上做得很好 - 在C性能或接近C性能 - 在这种情况下你不会指望Julia比PyPy快得多,因为它们都很接近最佳.与C实现的性能相比,它将提供非常丰富的信息,因为它将显示Julia和PyPy在桌面上留下了多少性能.幸运的是,这段代码似乎很容易移植到C.
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |