-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathRandomLoader.swift
More file actions
36 lines (29 loc) · 849 Bytes
/
RandomLoader.swift
File metadata and controls
36 lines (29 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// RandomLoader.swift
// Example
//
// Created by Mathias Claassen on 5/24/17.
//
//
import Foundation
import MetalBender
class RandomParameterLoader: ParameterLoader {
private(set) var pointer: UnsafeMutablePointer<Float>
var checkpoint: String = ""
init(maxSize: Int) {
let p = malloc(maxSize * MemoryLayout<Float>.stride)
pointer = p!.bindMemory(to: Float.self, capacity: maxSize)
uniformRandom(pointer, count: maxSize, scale: 0.1)
}
deinit {
free(pointer)
}
func uniformRandom(_ x: UnsafeMutablePointer<Float>, count: Int, scale: Float) {
for i in 0..<count {
x[i] = (Float.random()*2 - 1) * scale
}
}
func loadWeights(for id: String, modifier: String, size: Int) -> UnsafePointer<Float> {
return UnsafePointer(pointer)
}
}