Skip to content

Commit

Permalink
Add L2 normalization input processor
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanntg committed Jan 5, 2016
1 parent 428dd0e commit 92cf10d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions SyllableDetector/NeuralNet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ class PassThrough: InputProcessingFunction, OutputProcessingFunction {
}
}

class L2Normalize: InputProcessingFunction {
func applyInPlace(values: UnsafeMutablePointer<Float>, count: Int) {
// vDSP functions in the copy version support in place operations
applyAndCopy(values, count: count, to: values)
}

func applyAndCopy(values: UnsafePointer<Float>, count: Int, to destination: UnsafeMutablePointer<Float>) {
let len = vDSP_Length(count)

// sum of squares
var sumsq: Float = 0.0, sqrtsumsq: Float = 0.0
vDSP_svesq(values, 1, &sumsq, len)

// get square root
sqrtsumsq = sqrt(sumsq)

// divide by sum of squares
vDSP_vsdiv(values, 1, &sqrtsumsq, destination, 1, len)
}

}

class Normalize: InputProcessingFunction {
func applyInPlace(values: UnsafeMutablePointer<Float>, count: Int) {
// vDSP functions in the copy version support in place operations
Expand Down
3 changes: 3 additions & 0 deletions SyllableDetector/SyllableDetectorConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ extension SyllableDetectorConfig
case "mapstd":
return try SyllableDetectorConfig.parseMapStd(nm, withCount: cnt, from: data)

case "l2normalize":
return L2Normalize()

case "normalize":
return Normalize()

Expand Down

0 comments on commit 92cf10d

Please sign in to comment.