Created
July 14, 2020 20:31
-
-
Save JacopoMangiavacchi/36c256d51795b159e5c02ec5cfed234c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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