public static void Run(int epochs = 5) { // requires Internet connection (dynamic train, dynamic test) = tf.keras.datasets.fashion_mnist.load_data(); // will be able to do (trainImages, trainLabels) = train; ndarray trainImages = np.expand_dims(train.Item1 / 255.0f, axis: 3); ndarray trainLabels = train.Item2; ndarray testImages = np.expand_dims(test.Item1 / 255.0f, axis: 3); ndarray testLabels = test.Item2; bool loaded = 60000 == trainImages.Length; Debug.Assert(loaded); var model = new Sequential(new Layer[] { // will be able to do: new Flatten(kwargs: new { input_shape = (28, 28) }), new ResNetBlock(kernelSize: 3, filters: new [] { 1, 2, 3 }), new ResNetBlock(kernelSize: 3, filters: new [] { 1, 2, 3 }), new Flatten(), new Dense(units: 10, activation: tf.nn.softmax_fn), }); model.compile( optimizer: new AdamOptimizer(), loss: "sparse_categorical_crossentropy", metrics: new dynamic[] { "accuracy" }); model.fit(trainImages, trainLabels, epochs: epochs); var testEvalResult = model.evaluate(testImages, testLabels); double testAcc = testEvalResult[1]; Console.WriteLine($"Test accuracy: {testAcc}"); model.summary(); }
public void sequential_guide_convnet() { // Generate dummy data double[,,,] x_train = (double[, , , ])Accord.Math.Matrix.Zeros <double>(new int[] { 100, 100, 100, 3 }); // TODO: Add a better overload in Accord int[] y_train = Accord.Math.Vector.Random(100, min: 0, max: 10); double[,,,] x_test = (double[, , , ])Accord.Math.Matrix.Zeros <double>(new int[] { 20, 100, 100, 3 }); // TODO: Add a better overload in Accord int[] y_test = Accord.Math.Vector.Random(100, min: 0, max: 10); var model = new Sequential(); // input: 100x100 images with 3 channels -> (100, 100, 3) tensors. // this applies 32 convolution filters of size 3x3 each. model.Add(new Conv2D(32, new[] { 3, 3 }, activation: "relu", input_shape: new int?[] { 100, 100, 3 })); model.Add(new Conv2D(32, new[] { 3, 3 }, activation: "relu")); model.Add(new MaxPooling2D(pool_size: new[] { 2, 2 })); model.Add(new Dropout(0.25)); model.Add(new Conv2D(64, new[] { 3, 3 }, activation: "relu")); model.Add(new Conv2D(64, new[] { 3, 3 }, activation: "relu")); model.Add(new MaxPooling2D(pool_size: new[] { 2, 2 })); model.Add(new Dropout(0.25)); model.Add(new Flatten()); model.Add(new Dense(256, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(10, activation: "softmax")); var sgd = new SGD(lr: 0.01, decay: 1e-6, momentum: 0.9, nesterov: true); model.Compile(loss: "categorical_crossentropy", optimizer: sgd); model.fit(x_train, y_train, batch_size: 32, epochs: 10); var score = model.evaluate(x_test, y_test, batch_size: 32); }
public void sequential_guide_mlp_binary() { // Generate dummy data double[,] x_train = Accord.Math.Matrix.Random(1000, 20); int[] y_train = Accord.Math.Vector.Random(1000, min: 0, max: 10); double[,] x_test = Accord.Math.Matrix.Random(1000, 20); int[] y_test = Accord.Math.Vector.Random(1000, min: 0, max: 10); var model = new Sequential(); model.Add(new Dense(64, input_dim: 20, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(64, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(1, activation: "sigmoid")); model.Compile(loss: "binary_crossentropy", optimizer: "rmsprop", metrics: new[] { "accuracy" }); model.fit(x_train, y_train, epochs: 20, batch_size: 128); var score = model.evaluate(x_test, y_test, batch_size: 128); }
public void sequential_guide_mlp_multiclass() { // Generate dummy data double[,] x_train = Accord.Math.Matrix.Random(1000, 20); int[] y_train = Accord.Math.Vector.Random(1000, min: 0, max: 10); double[,] x_test = Accord.Math.Matrix.Random(1000, 20); int[] y_test = Accord.Math.Vector.Random(1000, min: 0, max: 10); var model = new Sequential(); // Dense(64) is a fully-connected layer with 64 hidden units. // in the first layer, you must specify the expected input data shape: // here, 20-dimensional vectors. model.Add(new Dense(64, activation: "relu", input_dim: 20)); model.Add(new Dropout(0.5)); model.Add(new Dense(64, activation: "relu")); model.Add(new Dropout(0.5)); model.Add(new Dense(10, activation: "softmax")); var sgd = new SGD(lr: 0.01, decay: 1e-6, momentum: 0.9, nesterov: true); model.Compile(loss: "categorical_crossentropy", optimizer: sgd, metrics: new[] { "accuracy" }); model.fit(x_train, y_train, epochs: 20, batch_size: 128); var score = model.evaluate(x_test, y_test, batch_size: 128); }
public void DoSomeWork(double[][] myoData) { using (var session = new TFSession()) { float[,] x = myoData.ToMatrix().ToSingle(); float[] y = myoData.GetColumn(0).ToSingle(); var inputDim = x.GetLength(1); KerasSharp.Backends.Current.Switch("KerasSharp.Backends.TensorFlowBackend"); // Create the model var model = new Sequential(); model.Add(new Dense(512, input_dim: inputDim, activation: new ReLU())); model.Add(new Dense(8, activation: new Softmax())); // Compile the model (for the moment, only the mean square // error loss is supported, but this should be solved soon) model.Compile(loss: new CategoricalCrossEntropy(), optimizer: new SGD(), metrics: new[] { new Accuracy() }); // Fit the model for 150 epochs model.fit(x, y, epochs: 150, batch_size: 32); // Use the model to make predictions float[] pred = model.predict(x)[0].To <float[]>(); // Evaluate the model double[] scores = model.evaluate(x, y); Console.WriteLine($"{model.metrics_names[1]}: {scores[1] * 100}"); } /* * using (var session = new TFSession()) * { * var graph = session.Graph; * * var a = graph.Const(2); * var b = graph.Const(3); * * TFTensor addingTensor = session.GetRunner().Run(graph.Add(a, b)); * object TResVal = addingTensor.GetValue(); * * } */ }
public Model CanLearn() { // requires Internet connection (dynamic train, dynamic test) = tf.keras.datasets.fashion_mnist.load_data(); ndarray trainImage = NormalizeChannelValue(train.Item1)[0]; trainImage = (ndarray)np.expand_dims(trainImage, axis: 2).reshape(new [] { 28 * 28, 1 }); var coords = Coord(28, 28).ToNumPyArray().reshape(new [] { 28 * 28, 2 }); var model = new Sequential(new object[] { new Siren(2, Enumerable.Repeat(128, 3).ToArray()), new Dense(units: 1, activation: tf.keras.activations.relu_fn), }); model.compile( optimizer: new Adam(), loss: "mse"); model.fit(coords, targetValues: trainImage, epochs: 2, batchSize: 28 * 28, stepsPerEpoch: 1024); double testLoss = model.evaluate(coords, trainImage); return(model); }
public void TestModelCompileAndFit() { print("Test base Squential Model"); float[,] x = { { 2, 3, 4, 5 }, { 4, 3, 2, 1 }, { 8, 44, 22, 11 }, { 1, 3, 3, 1 } }; float[,] y = { { 0.2f, 0.2f }, { 0.4f, 0.4f }, { 0.8f, 0.8f }, { 0.1f, 0.1f } }; var model = new Sequential(); model.Add(new Dense(12, input_dim: 4, activation: new ReLU())); model.Add(new Dense(8, activation: new ReLU())); model.Add(new Dense(2, activation: new Sigmoid())); // Compile the model (for the moment, only the mean square // error loss is supported, but this should be solved soon) model.Compile(loss: new MeanSquareError(), optimizer: new Adam(0.001)); print("fit 1"); model.fit(x, y, batch_size: 4, epochs: 30, verbose: 1); print("fit 2"); model.fit(x, y, batch_size: 4, epochs: 30, verbose: 1); // Use the model to make predictions //var test = model.predict(x)[0]; //float[,] pred = model.predict(x)[0].To<float[,]>(); // Evaluate the model double[] scores = model.evaluate(x, y); Debug.Log("Eval results: " + string.Join(",", scores)); //scores = model.evaluate(x, y); scores = model.evaluate(x, y); //Debug.Log($"{model.metrics_names[1]}: {scores[1] * 100}"); ((UnityTFBackend)K).ExportGraphDef("SavedGraph/sequentialtest.pb"); }
public void sequential_guide_lstm() { // Generate dummy data double[][][] x_train = null; // TODO: Generate 100 random sequences, with random lengths int[] y_train = Accord.Math.Vector.Random(100, min: 0, max: 10); double[][][] x_test = null; // TODO: Generate 50 random sequences, with random lengths int[] y_test = Accord.Math.Vector.Random(50, min: 0, max: 10); int max_features = 1024; var model = new Sequential(); model.Add(new Embedding(max_features, output_dim: 256)); model.Add(new LSTM(128)); model.Add(new Dropout(0.5)); model.Add(new Dense(1, activation: "sigmoid")); model.Compile(loss: "binary_crossentropy", optimizer: "rmsprop", metrics: new[] { "accuracy" }); model.fit(x_train, y_train, batch_size: 16, epochs: 10); var score = model.evaluate(x_test, y_test, batch_size: 16); }
static void Main(string[] args) { /* # Example from https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/ # # from keras.models import Sequential # from keras.layers import Dense # import numpy # # fix random seed for reproducibility # numpy.random.seed(7) # # load pima indians dataset # dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") # split into input (X) and output (Y) variables # X = dataset[:,0:8] # Y = dataset[:,8] # # create model # model = Sequential() # model.add(Dense(12, input_dim=8, activation='relu')) # model.add(Dense(8, activation='relu')) # model.add(Dense(1, activation='sigmoid')) # # Compile model # model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # # Fit the model # model.fit(X, Y, epochs=150, batch_size=10) # # evaluate the model # scores = model.evaluate(X, Y) # print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) */ //KerasSharp.Backends.Current.Switch("KerasSharp.Backends.TensorFlowBackend"); KerasSharp.Backends.Current.Switch("KerasSharp.Backends.CNTKBackend"); // Load the Pima Indians Data Set var pima = new Accord.DataSets.PimaIndiansDiabetes(); float[,] x = pima.Instances.ToMatrix().ToSingle(); float[] y = pima.ClassLabels.ToSingle(); // Create the model var model = new Sequential(); model.Add(new Dense(12, input_dim: 8, activation: new ReLU())); model.Add(new Dense(8, activation: new ReLU())); model.Add(new Dense(1, activation: new Sigmoid())); // Compile the model (for the moment, only the mean square // error loss is supported, but this should be solved soon) model.Compile(loss: new MeanSquareError(), optimizer: new Adam(), metrics: new[] { new Accuracy() }); // Fit the model for 150 epochs model.fit(x, y, epochs: 150, batch_size: 10); // Use the model to make predictions float[] pred = model.predict(x)[0].To <float[]>(); // Evaluate the model double[] scores = model.evaluate(x, y); Console.WriteLine($"{model.metrics_names[1]}: {scores[1] * 100}"); Console.ReadLine(); }