public static void FitMnistSimple() { var model = new Sequential(); model.Add(new Dense(512, activation: "relu", inputShape: new int[] { 784 })); model.Add(new Dropout(0.2)); model.Add(new Dense(512, activation: "relu")); model.Add(new Dropout(0.2)); model.Add(new Dense(10, activation: "softmax")); var optimizer = new SGD(lr: 0.01); model.Compile("categorical_crossentropy", optimizer, new string[] { "accuracy" }); var xtrain = TensorUtils.Deserialize(new FileStream(GetDataPath("datasets/nda_mnist/mnist_xtrain.nda"), FileMode.Open)); var ytrain = TensorUtils.Deserialize(new FileStream(GetDataPath("datasets/nda_mnist/mnist_ytrain.nda"), FileMode.Open)); xtrain = xtrain.Cast(DType.Float32); xtrain = Ops.Div(null, xtrain, 255f); ytrain = ytrain.Cast(DType.Float32); model.Fit(xtrain, ytrain, batchSize: 128, epochs: 20); var stream = new FileStream("c:/ttt/mnist-simple.model", FileMode.OpenOrCreate, FileAccess.Write); stream.SetLength(0); model.Save(stream); }
public static void FitMnist() { var model = new Sequential(); model.Add(new Conv2D(32, kernelSize: new int[] { 3, 3 }, inputShape: new int[] { 28, 28, 1 }, activation: "relu")); model.Add(new Conv2D(64, kernelSize: new int[] { 3, 3 }, activation: "relu")); // model.Add(new MaxPooling1D(poolSize: 2)); model.Add(new MaxPooling2D(poolSize: new int[] { 2, 2 })); model.Add(new Dropout(0.25)); model.Add(new Flatten()); model.Add(new Dense(128, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(10, activation: "softmax")); var optimizer = new SGD(lr: 0.01); model.Compile("categorical_crossentropy", optimizer, new string[] { "accuracy" }); var xtrain = TensorUtils.Deserialize(new FileStream(GetDataPath("datasets/nda_mnist/mnist_xtrain.nda"), FileMode.Open)); var ytrain = TensorUtils.Deserialize(new FileStream(GetDataPath("datasets/nda_mnist/mnist_ytrain.nda"), FileMode.Open)); xtrain = xtrain.Cast(DType.Float32); xtrain = Ops.Div(null, xtrain, 255f); ytrain = ytrain.Cast(DType.Float32); model.Fit(xtrain, ytrain, batchSize: 128, epochs: 12); var stream = new FileStream("c:/ttt/mnist.model", FileMode.OpenOrCreate, FileAccess.Write); stream.SetLength(0); model.Save(stream); }
static void PredictMnist(string modelPath, string xtestPath, string ytestPath = null) { var xtest = TensorUtils.Deserialize(File.OpenRead(xtestPath)); xtest = xtest.Cast(DType.Float32); xtest = Ops.Div(null, xtest, 255f); // xtest = xtest.Narrow(0, 0, 101); var model = Sequential.Load(modelPath); var result = model.Predict(xtest, batchSize: 32); if (ytestPath == null) { return; } var ytest = TensorUtils.Deserialize(File.OpenRead(ytestPath)); ytest = ytest.Cast(DType.Float32); // ytest = ytest.Narrow(0, 0, 101); ytest = Ops.Argmax(null, ytest, 1).Squeeze(); var t = result.Narrow(0, 0, 11); // Console.WriteLine(t.Format()); result = Ops.Argmax(null, result, 1).Squeeze(); t = result.Narrow(0, 0, 11); // Console.WriteLine(t.Format()); double sum = 0.0; for (var i = 0; i < ytest.Sizes[0]; ++i) { sum += (int)ytest.GetElementAsFloat(i) == (int)result.GetElementAsFloat(i) ? 1.0 : 0.0; } Console.WriteLine($"Accuracy: {sum / ytest.Sizes[0] * 100}%"); }
public void TestLoadMnist() { string path = "../../../datasets/nda_mnist/mnist_xtrain.nda"; var nda = TensorUtils.Deserialize(File.OpenRead(path)); CollectionAssert.AreEqual(nda.Sizes, new long[] { 60000, 28, 28 }); Assert.AreEqual(nda.GetElementAsFloat(1, 0, 0), 0f); Assert.AreEqual(nda.GetElementAsFloat(0, 6, 9), 36f); // Cast the array to float nda = nda.Cast(DType.Float32); CollectionAssert.AreEqual(nda.Sizes, new long[] { 60000, 28, 28 }); Assert.AreEqual(nda.GetElementAsFloat(1, 0, 0), 0f); Assert.AreEqual(nda.GetElementAsFloat(0, 6, 9), 36f); // Divide by 255 nda = Ops.Div(null, nda, 255f); Assert.AreEqual(nda.GetElementAsFloat(1, 0, 0), 0f); Assert.AreEqual(nda.GetElementAsFloat(0, 6, 9), 36f / 255f, float.Epsilon); Assert.AreEqual(nda.GetElementAsFloat(0, 6, 9), 36.0 / 255, 0.000001); }