Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JacopoMangiavacchi/36c256d51795b159e5c02ec5cfed234c to your computer and use it in GitHub Desktop.
Save JacopoMangiavacchi/36c256d51795b159e5c02ec5cfed234c to your computer and use it in GitHub Desktop.
// Load in memory and split is not performant
private func getFileLine(filePath: String, process: (String) -> Void) {
guard let filePointer:UnsafeMutablePointer<FILE> = fopen(filePath,"r") else {
preconditionFailure("Could not open file at \(filePath)")
}
defer {
fclose(filePointer)
}
var lineByteArrayPointer: UnsafeMutablePointer<CChar>? = nil
var lineCap: Int = 0
while getline(&lineByteArrayPointer, &lineCap, filePointer) > 0 {
let line = String.init(cString:lineByteArrayPointer!).trimmingCharacters(in: .whitespacesAndNewlines)
process(line)
}
}
private func oneHotEncoding(_ number: Int, length: Int = 10) -> [Float] {
guard number < length else {
fatalError("wrong ordinal vs encoding length")
}
var array = Array<Float>(repeating: 0.0, count: length)
array[number] = 1.0
return array
}
private func oneHotDecoding(_ encoding: [Float]) -> Int {
var value: Int = 0
for i in 0..<encoding.count {
if encoding[i] == 1 {
value = i
break
}
}
return value
}
private func argmaxDecoding(_ encoding: [Float]) -> Int {
var max: Float = 0
var pos: Int = 0
for i in 0..<encoding.count {
if encoding[i] > max {
max = encoding[i]
pos = i
}
}
return pos
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment