NeuralKit
Swift 中神经网络结构的实现,以简化 iOS 应用中的机器学习。
安装
NeuralKit 可在 CocoaPods 上找到。要下载,请将以下内容添加到 Podfile 中:
pod 'NeuralKit'
为什么我要创建 NeuralKit?
当前默认的 Swift 机器学习库(CoreML)不太直观,并且不给予用户控制网络实际训练的太多控制权。也因为我无聊,想更多了解机器学习 :)
文档
https://sylvanm.github.io/NeuralKit/
如何使用
创建网络
// Set the dimension for the network
// This is an array that has the sizes of each layer.
//
// The following will describe a network with an input layer size of 2, hidden layer sizes of 3, 3, and 4, and an output layer size of 2.
let dimension = [2, 3, 3, 4, 2]
// You can initialize a network with the dimension array, and optionally with random weights and biases.
let network = Network(networkDimension: dimension, withRandomWeights: true)
输入和输出
// Set input
let input = [3.0, 42.1]
network.set(input: input)
// Compute output
let output = network.compute()
高级网络设置
// The network uses an activation function to decide how to 'squish' the weighted sum into a neuron activation.
// By default, this is the "Rectified Linear Units" functions.
//
// There are default functions that do this defined in the ActivationFunctions struct, and you can tell the network to use those:
network.activationFunction = ActivationFunctions.sigmoid // Sets the activation function to the sigmoid function
network.activationFunction = ActivationFunctions.relu // Sets the activation function to the rectified linear units function
// You can also make your own function if you so wish.
// It be a closure that fits the typealias Activation Function (Defined in ActivationFunctions.swift), or (Double) -> Double.
let myActivationFunction: ActivationFunction = { x in
return 1 / (1 + exp(-x)) // This is techincally the same as the ActivationFunctions.sigmoid function
}
network.activationFunction = myActivationFunction
训练
// To do any training operations, you must first have dataset, an NKTrainingData object, and an NKTrainer.
// You must first define your dataset, which is a dictionary, with inputs corresponding to desired outputs.
let dataset: [[Double] : [Double]] = [ // data was chosen arbitrarily
[0.0, 0.0] : [0.4, 0.2],
[0.1, 0.8] : [2.1, 9.3],
[5.2, 9.0] : [13.3, 19.0]
]
// Now make an NKTrainingData object
let trainingData = NKTrainingData(dataset)
// Now make an NKTrainer, and set the training data
let trainer = NKTrainer()
trainer.loadTrainingData(trainingData)
// With your dataset, you can get the cost (or loss) of your network.
do {
print(network)
let cost = try trainer.cost(of: network)
print("Cost:", cost)
} catch {
print("Error:", error)
}
导出
在训练网络时,您想保存已训练网络的架构,以便不必从头开始。使用NeuralKit,您可以将其导出为Data对象,您可以做任何想做的事情;保存、加密等等。然后您可以将网络导入回来,从保存的数据创建新网络。
do {
// save the network
let networkData: Data = try network.exportNetwork()
// from here, you can do whatever with the data, maybe write it to a file to save, and continue training later.
// now load a network from the data
let loadedNetwork: Network = try Network.importNetwork(fromData: networkData)
print(loadedNetwork)
} catch {
print(error)
}