MLPNeuralNet 1.0.10

MLPNeuralNet 1.0.10

测试已测试
Lang语言 Obj-CObjective C
许可证 BSD
发布日期最后发布的版本2015年9月

Mykola Pavlov维护。




  • Mykola Pavlov

MLPNeuralNet是一个适用于iOS和Mac OS X的平台快速多层感知器(MLP)神经网络库。使用MLPNeuralNet可以通过训练好的神经网络预测新的实例。它基于Apple的Accelerate框架,通过矢量操作以及(如果可用)硬件加速来构建。

Neural Network

您为什么要使用它?

想象一下,您使用Matlab(Python或R)设计了一个预测模型,并希望将其用于iOS应用中。如果是这种情况,那么MLPNeuralNet正是您所需要的。MLPNeuralNet专门设计为仅通过前向传播模式加载和运行模型。

特性

  • 分类多分类和回归输出
  • 向量化实现
  • 支持双精度
  • 包含多个隐藏层或无隐藏层(在这后者情况下,它与逻辑回归/线性回归相同)

快速示例

让我们部署一个AND函数(逻辑合取)的模型,其工作方式如下:(当然,在现实世界中您不需要为此使用神经网络)

X1 X2 Y
0 0 0
1 0 0
0 1 0
1 1 1

我们的模型下列权重和网络配置

AND Model Example

// Use the designated initialiser to pass the network configuration and weights to the model.
// Note: You do not need to specify the biased units (+1 above) in the configuration.

NSArray *netConfig = @[@2, @1];
double wts[] = {-30, 20, 20};
NSData *weights = [NSData dataWithBytes:wts length:sizeof(wts)];

MLPNeuralNet *model = [[MLPNeuralNet alloc] initWithLayerConfig:netConfig
                                                        weights:weights
                                                     outputMode:MLPClassification];
// Predict output of the model for new sample
double sample[] = {0, 1};
NSData * vector = [NSData dataWithBytes:sample length:sizeof(sample)];
NSMutableData * prediction = [NSMutableData dataWithLength:sizeof(double)];
[model predictByFeatureVector:vector intoPredictionVector:prediction];

double * assessment = (double *)prediction.bytes;
NSLog(@"Model assessment is %f", assessment[0]);

扩展示例

假设您使用pybrain或其他自定义实现训练了一个网络。

Extended Example

// Use the designated initialiser to pass the network configuration and weights to the model.
// Note: You do not need to specify the biased units (+1 above) in the configuration.

NSArray *netConfig = @[@3, @2, @1];
double wts[] = {b1, w1, w2, w3, b2, w4, w5, w6, b3, w7, w8};
NSData *weights = [NSData dataWithBytes:wts length:sizeof(wts)];

MLPNeuralNet *model = [[MLPNeuralNet alloc] initWithLayerConfig:netConfig
                                                        weights:weights
                                                     outputMode:MLPClassification];
model.hiddenActivationFunction = MLPSigmoid;
model.outputActivationFunction = MLPNone;

// Predict output of the model for new sample
double sample[] = {0, 1, 2};
NSData * vector = [NSData dataWithBytes:sample length:sizeof(sample)];
NSMutableData * prediction = [NSMutableData dataWithLength:sizeof(double)];
[model predictByFeatureVector:vector intoPredictionVector:prediction];

double * assessment = (double *)prediction.bytes;
NSLog(@"Model assessment is %f", assessment[0]);

入门指南

以下说明如何使用CocoaPods设置并安装MLPNeuralNet,适用于Xcode 5和iOS 7.x(+) SDK。如果您不熟悉CocoaPods,只需克隆仓库并将MLPNeuralNet直接作为子项目导入。

导入MLPNeuralNet.h

请务必在模型顶部添加以下行

#import "MLPNeuralNet.h"

我需要多少权重才能初始化X->Y->Z网络?

大多数流行的库(包括 MLPNeuralNet)默认在每个层(除了最后一层)中添加带偏置的单元。假设这些附加单元,总权重数为 (X + 1) * Y + (Y + 1) * Z

从其他库导入权重。

您可以对一些可用的神经网络包执行此操作。

R nnet 库

#Assuming nnet_model is a trained neural network
nnet_model$wts

Python NeuroLab

#Where net argument is an neurolab.core.Net object
import neurolab as nl
import numpy as np

def getweights(net):
     vec = []
     for layer in net.layers:
         b = layer.np['b']
         w = layer.np['w']
         newvec = np.ravel(np.concatenate((b, np.ravel(w,order='F'))).reshape((layer.ci+1, layer.cn)), order = 'F')
         [vec.append(nv) for nv in newvec]
     return np.array(vec)

Python neon

import numpy as np

def layer_names(params):
    layer_names = params.keys()
    layer_names.remove('epochs_complete')
    # Sort layers by their appearance in the model architecture
    # Since neon appands the index to the layer name we will use it to sort
    layer_names.sort(key=lambda x: int(x.split("_")[-1]))
    return layer_names

def getweights(file_name):
    vec = []
    # Load a stored model file from disk (should have extension prm)
    params = pkl.load(open(file_name, 'r'))
    layers = layer_names(params)

    for layer in layers:
        # Make sure our model has biases activated, otherwise add zeros here
        b = params[layer]['biases']
        w = params[layer]['weights']

        newvec = np.ravel(np.hstack((b,w)))
        [vec.append(nv) for nv in newvec]
    return vec

# An example call
getweights(expanduser('~/data/workout-dl/workout-ep100.prm'))

性能基准测试

在此测试中,神经网络从 1 -> 1 配置逐层增长到 200 -> 200 -> 200 -> 1 配置。在每一步,使用随机输入矢量和权重计算和基准测试输出。相应地,总权重数从 2 增长到 80601。我理解这个测试相当人为,但希望它能说明性能。如果您能提出更好的方法,我将很乐意! :)

MLPNeuralNet Performance Benchmark

单元测试

MLPNeuralNet/MLPNeuralNetTests 子目录中包含了一个多样化的单元测试。您可以使用 Xcode 中的 MLPNeuralNet 方案执行它们。

致谢

MLPNeuralNet 受以下启发:

致谢

联系我

维护者: Mykola Pavlov ([email protected])

请告诉我您如何使用 MLPNeuralNet 解决一些实际问题。

许可协议

MLPNeuralNet 采用 BSD 许可发布。有关更多信息,请参阅 LICENSE 文件。