Swift library for tensor operations, machine learning and artificial intelligence
Swift library for multidimensional data, tensor operations and machine learning on OS X. For additional comments and documentation, please take a look at the Wiki.
Already implemented:
MultidimensionData
protocol for elegant handling of multidimensional data of any kindCreate data tensor:
var a = Tensor<Float>(modeSizes: [3, 3, 3], repeatedValue: 0)
Read and write single values:
let b: Float = a[1, 2, 0]
a[2, 0, 1] = 3.14
Read and write a tensor slice
let c: Tensor<Float> = a[1..<3, all, [0]]
a[1...1, [0, 2], all] = Tensor<Float>(modeSizes: [2, 3], values: [1, 2, 3, 4, 5, 6])
modes of size 1 will be trimmed
Modes with same symbolic index will be summed over. Simple matrix multiplication:
var m = Tensor<Float>(modeSizes: [4, 6], repeatedValue: 1)
var n = Tensor<Float>(modeSizes: [6, 5], repeatedValue: 2)
let matrixProduct = m[.i, .j] * n[.j, .k]
Geodesic deviation:
var tangentVector = Tensor<Float>(modeSizes: [4], values: [0.3, 1.7, 0.2, 0.5])
tangentVector.isCartesian = false
var deviationVector = Tensor<Float>(modeSizes: [4], values: [0.1, 0.9, 0.4, 1.2])
deviationVector.isCartesian = false
var riemannianTensor = Tensor<Float>(diagonalWithModeSizes: [4, 4, 4, 4])
riemannianTensor.isCartesian = false
riemannianTensor.variances = [.contravariant, .covariant, .covariant, .covariant]
let relativeAcceleration = riemannianTensor[.μ, .ν, .ρ, .σ] * tangentVector[.ν] * tangentVector[.ρ] * deviationTensor[.σ]
Setting up and training a simple feedforward neural net:
var estimator = NeuralNet(layerSizes: [28*28, 40, 10])
estimator.layers[0].activationFunction = ReLU(secondarySlope: 0.01)
estimator.layers[1].activationFunction = ReLU(secondarySlope: 0.01)
var neuralNetCost = SquaredErrorCost(forEstimator: estimator)
stochasticGradientDescent(neuralNetCost, inputs: trainingData[.a, .b], targets: trainingLabels[.a, .c], updateRate: 0.1, minibatchSize: 50, validationCallback: ({ (epoch, estimator) -> (Bool) in
if(epoch >= 30) {return true}
else {return false}
}))
Extended PCA algorithms to work with tensors with arbitrary mode count
To use this framework in an OSX XCode project:
If possible, whole module optimization should be used for very significant performance gains.