Comments (5)
I took a stab at this - but solution is not so simple.
-
Unfortunately - this guff can't be simply tucked away in protocol extension.
-
these can't be saved I'm afraid...
activationFunction = { _ in return 0.0 }// try container.decode(func.self, forKey: .activationFunction)
derivativeActivationFunction = { _ in return 0.0 }//try container.decode(Double.self, forKey: .derivativeActivationFunction)
class Neuron:Codable{
// CODABLE BEGIN
// guff for saving / restoring
private enum CodingKeys: CodingKey {
case weights
case activationFunction
case derivativeActivationFunction
case inputCache
case delta
case learningRate
}
required convenience init(from decoder: Decoder) throws
{
self.init(weights: [0], activationFunction: { _ in return 0.0 }, derivativeActivationFunction: { _ in return 0.0 })
let container = try decoder.container(keyedBy: CodingKeys.self);
weights = try container.decode([Double].self, forKey: .weights);
activationFunction = { _ in return 0.0 }// try container.decode(func.self, forKey: .activationFunction)
derivativeActivationFunction = { _ in return 0.0 }//try container.decode(Double.self, forKey: .derivativeActivationFunction)
inputCache = try container.decode(Double.self, forKey: .inputCache)
delta = try container.decode(Double.self, forKey: .delta)
learningRate = try container.decode(Double.self, forKey: .learningRate)
}
func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(weights, forKey: .weights)
try container.encode(inputCache, forKey: .inputCache)
try container.encode(delta, forKey: .delta)
try container.encode(learningRate, forKey: .learningRate)
// encoding escape functions ?? activationFunction /derivativeActivationFunction
}
}
from swiftsimpleneuralnetwork.
currently blowing up - https://github.com/johndpope/Bresenham-Line/tree/master/Bresenham-Line/NN
from swiftsimpleneuralnetwork.
N.B. - this library loads what seems as official weights / biases from tensorflow
https://github.com/qoncept/TensorSwift/tree/master/Sources/MNIST/Models
public struct Classifier {
public let W_conv1: Tensor
public let b_conv1: Tensor
public let W_conv2: Tensor
public let b_conv2: Tensor
public let W_fc1: Tensor
public let b_fc1: Tensor
public let W_fc2: Tensor
public let b_fc2: Tensor
public func classify(_ x_image: Tensor) -> Int {
let h_conv1 = (x_image.conv2d(filter: W_conv1, strides: [1, 1, 1]) + b_conv1).relu()
let h_pool1 = h_conv1.maxPool(kernelSize: [2, 2, 1], strides: [2, 2, 1])
let h_conv2 = (h_pool1.conv2d(filter: W_conv2, strides: [1, 1, 1]) + b_conv2).relu()
let h_pool2 = h_conv2.maxPool(kernelSize: [2, 2, 1], strides: [2, 2, 1])
let h_pool2_flat = h_pool2.reshaped([1, Dimension(7 * 7 * 64)])
let h_fc1 = (h_pool2_flat.matmul(W_fc1) + b_fc1).relu()
let y_conv = (h_fc1.matmul(W_fc2) + b_fc2).softmax()
return y_conv.elements.enumerated().max { $0.1 < $1.1 }!.0
}
}
extension Classifier {
public init(path: String) {
W_conv1 = Tensor(shape: [5, 5, 1, 32], elements: loadFloatArray(path, file: "W_conv1"))
b_conv1 = Tensor(shape: [32], elements: loadFloatArray(path, file: "b_conv1"))
W_conv2 = Tensor(shape: [5, 5, 32, 64], elements: loadFloatArray(path, file: "W_conv2"))
b_conv2 = Tensor(shape: [64], elements: loadFloatArray(path, file: "b_conv2"))
W_fc1 = Tensor(shape: [Dimension(7 * 7 * 64), 1024], elements: loadFloatArray(path, file: "W_fc1"))
b_fc1 = Tensor(shape: [1024], elements: loadFloatArray(path, file: "b_fc1"))
W_fc2 = Tensor(shape: [1024, 10], elements: loadFloatArray(path, file: "W_fc2"))
b_fc2 = Tensor(shape: [10], elements: loadFloatArray(path, file: "b_fc2"))
}
}
private func loadFloatArray(_ directory: String, file: String) -> [Float] {
let data = try! Data(contentsOf: URL(fileURLWithPath: directory.stringByAppendingPathComponent(file)))
return Array(UnsafeBufferPointer(start: UnsafeMutablePointer<Float>(mutating: (data as NSData).bytes.bindMemory(to: Float.self, capacity: data.count)), count: data.count / 4))
}
https://github.com/imane0897/simple_OCR/blob/dc54d9275149ee3768536caf534091d029e3596c/mnist_deep.py
saver = tf.train.Saver({"W_conv1": W_conv1, "b_conv1": b_conv1,
"W_conv2": W_conv2, "b_conv2": b_conv2,
"W_fc1": W_fc1, "b_fc1": b_fc1,
"W_fc2": W_fc2, "b_fc2": b_fc2})
from swiftsimpleneuralnetwork.
https://github.com/imane0897/simple_OCR/blob/dc54d9275149ee3768536caf534091d029e3596c/mnist_deep.py
saver = tf.train.Saver({"W_conv1": W_conv1, "b_conv1": b_conv1,
"W_conv2": W_conv2, "b_conv2": b_conv2,
"W_fc1": W_fc1, "b_fc1": b_fc1,
"W_fc2": W_fc2, "b_fc2": b_fc2})
from swiftsimpleneuralnetwork.
also relevant / using swift for tensorflow / python /sklearn to import weights / biases
https://github.com/johndpope/swift-digits/blob/master/Models/main.swift
import Python
let load_digits = Python.import("sklearn.datasets").load_digits
let LinearSVC = Python.import("sklearn.svm").LinearSVC
let train_test_split = Python.import("sklearn.model_selection").train_test_split
let classifier = LinearSVC()
let dataset = load_digits()
let (X_train, X_test, y_train, y_test)
= train_test_split(dataset["data"], dataset["target"]).tuple4
classifier.fit(X_train, y_train)
print("train: \(classifier.score(X_train, y_train))")
print("test: \(classifier.score(X_test, y_test))")
let coremltools = Python.import("coremltools")
let coreml_model = coremltools.converters.sklearn.convert(classifier)
coreml_model.save("Digits.mlmodel")
from swiftsimpleneuralnetwork.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftsimpleneuralnetwork.