public void sequential_guide_stateful_stacked_lstm() { int data_dim = 16; int timesteps = 8; int num_classes = 10; int batch_size = 32; // Expected input batch shape: (batch_size, timesteps, data_dim) // Note that we have to provide the full batch_input_shape since the network is stateful. // the sample of index i in batch k is the follow-up for the sample i in batch k-1. var model = new Sequential(); model.Add(new LSTM(32, return_sequences: true, stateful: true, batch_input_shape: new int?[] { batch_size, timesteps, data_dim })); model.Add(new LSTM(32, return_sequences: true, stateful: true)); model.Add(new LSTM(32, stateful: true)); model.Add(new Dense(10, activation: "softmax")); model.Compile(loss: "categorical_crossentropy", optimizer: "rmsprop", metrics: new[] { "accuracy" }); // Generate dummy training data double[][][] x_train = null; // Accord.Math.Jagged.Random(1000, timesteps, data_dim); // TODO: Add better method in Accord int[] y_train = Accord.Math.Vector.Random(1000, min: 0, max: num_classes); // Generate dummy validation data double[,,] x_val = null; // Accord.Math.Jagged.Random(1000, timesteps, data_dim); // TODO: Add better method in Accord int[] y_val = Accord.Math.Vector.Random(1000, min: 0, max: num_classes); model.fit(x_train, y_train, batch_size: batch_size, epochs: 5, shuffle: Shuffle.False, validation_data: new Array[] { x_val, y_val }); }
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_stacked_lstm() { int data_dim = 16; int timesteps = 8; int num_classes = 10; // expected input data shape: (batch_size, timesteps, data_dim) var model = new Sequential(); model.Add(new LSTM(32, return_sequences: true, input_shape: new[] { timesteps, data_dim })); // returns a sequence of vectors of dimension 32 model.Add(new LSTM(32, return_sequences: true)); // returns a sequence of vectors of dimension 32 model.Add(new LSTM(32)); // return a single vector of dimension 32 model.Add(new Dense(10, activation: "softmax")); model.Compile(loss: "categorical_crossentropy", optimizer: "rmsprop", metrics: new[] { "accuracy" }); // Generate dummy training data double[][][] x_train = null; // Accord.Math.Jagged.Random(1000, timesteps, data_dim); // TODO: Add better method in Accord int[] y_train = Accord.Math.Vector.Random(1000, min: 0, max: num_classes); // Generate dummy validation data double[,,] x_val = null; // Accord.Math.Jagged.Random(1000, timesteps, data_dim); // TODO: Add better method in Accord int[] y_val = Accord.Math.Vector.Random(1000, min: 0, max: num_classes); model.fit(x_train, y_train, batch_size: 64, epochs: 5, validation_data: new Array[] { x_val, y_val }); }
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 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_training_2() { var model = new Sequential(); model.Add(new Dense(32, activation: "relu", input_dim: 100)); model.Add(new Dense(10, activation: "softmax")); model.Compile(optimizer: "rmsprop", loss: "categorical_crossentropy", metrics: new[] { "accuracy" }); // Generate dummy data double[][] data = Accord.Math.Jagged.Random(1000, 100); int[] labels = Accord.Math.Vector.Random(1000, min: 0, max: 10); // Convert labels to categorical one-hot encoding double[][] one_hot_labels = Accord.Math.Jagged.OneHot(labels, columns: 10); // Train the model, iterating on the data in batches of 32 samples model.fit(data, one_hot_labels, epochs: 10, batch_size: 32); }
public void sequential_guide_training_1() { // For a single-input model with 2 classes (binary classification): var model = new Sequential(); model.Add(new Dense(32, activation: "relu", input_dim: 100)); model.Add(new Dense(1, activation: "sigmoid")); model.Compile(optimizer: "rmsprop", loss: "binary_crossentropy", metrics: new[] { "accuracy" }); // Generate dummy data double[,] data = Accord.Math.Matrix.Random(1000, 100); int[] labels = Accord.Math.Vector.Random(1000, min: 0, max: 10); // Train the model, iterating on the data in batches of 32 samples model.fit(data, labels, epochs: 10, batch_size: 32); // For a single-input model with 10 classes (categorical classification): }
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 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) { GradientEngine.UseEnvironmentFromVariable(); TensorFlowSetup.Instance.EnsureInitialized(); // this allows SIREN to oversaturate channels without adding to the loss var clampToValidChannelRange = PythonFunctionContainer.Of <Tensor, Tensor>(ClampToValidChannelValueRange); var siren = new Sequential(new object[] { new GaussianNoise(stddev: 1f / (128 * 1024)), new Siren(2, Enumerable.Repeat(256, 5).ToArray()), new Dense(units: 4, activation: clampToValidChannelRange), new GaussianNoise(stddev: 1f / 128), }); siren.compile( // too slow to converge //optimizer: new SGD(momentum: 0.5), // lowered learning rate to avoid destabilization optimizer: new Adam(learning_rate: 0.00032), loss: "mse"); if (args.Length == 0) { siren.load_weights("sample.weights"); Render(siren, 1034 * 3, 1536 * 3, "sample6X.png"); return; } foreach (string imagePath in args) { using var original = new Bitmap(imagePath); byte[,,] image = ToBytesHWC(original); int height = image.GetLength(0); int width = image.GetLength(1); int channels = image.GetLength(2); Debug.Assert(channels == 4); var imageSamples = PrepareImage(image); var coords = ImageTools.Coord(height, width).ToNumPyArray() .reshape(new[] { width *height, 2 }); var upscaleCoords = ImageTools.Coord(height * 2, width * 2).ToNumPyArray(); var improved = ImprovedCallback.Create((sender, eventArgs) => { if (eventArgs.Epoch < 10) { return; } ndarray <float> upscaled = siren.predict( upscaleCoords.reshape(new[] { height *width * 4, 2 }), batch_size: 1024); upscaled = (ndarray <float>)upscaled.reshape(new[] { height * 2, width * 2, channels }); using var bitmap = ToImage(RestoreImage(upscaled)); bitmap.Save("sample4X.png", ImageFormat.Png); siren.save_weights("sample.weights"); Console.WriteLine(); Console.WriteLine("saved!"); }); siren.fit(coords, imageSamples, epochs: 100, batchSize: 16 * 1024, shuffleMode: TrainingShuffleMode.Epoch, callbacks: new ICallback[] { improved }); } }
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(); }
public Task TrainAsync(MLPointCollection points) { return(Task.Factory.StartNew(() => { _modelDispatcher.Invoke(() => { Log.Info("Training"); var xydata = _dataGenerator.GetPointsXYData(points); var x = np.array(xydata.x.ToArray()); var y = np.array(xydata.y.ToArray()); var count = (int)(xydata.x.Count / (decimal)xydata.dataItemsPerX); x = x.reshape(count, xydata.dataItemsPerX); y = y.reshape(count, 3); //Tensorflow.InvalidArgumentError: 'In[0] mismatch In[1] shape: 28 vs. 1120: [5,28] [1120,60] 0 0' /*_model = keras.Sequential( * new List<ILayer> * { * new Flatten(new FlattenArgs * { * InputShape = new TensorShape(xydata.dataItemsPerX) * }), * //keras.layers.Flatten(), * keras.layers.Dense(xydata.dataItemsPerX, activation: "relu"),//, input_shape: new TensorShape(-1, xydata.dataItemsPerX)), * keras.layers.Dense(60, activation: "relu"), * keras.layers.Dense(40, activation: "relu"), * keras.layers.Dense(3, activation: "softmax"), * }); * * _model.compile(keras.optimizers.SGD(0.01F), keras.losses.CategoricalCrossentropy(from_logits: true), */ var numberOfClasses = 3; _model = keras.Sequential( new List <ILayer> { new Flatten(new FlattenArgs { InputShape = new TensorShape(xydata.dataItemsPerX) }), //keras.layers.Flatten(), keras.layers.Dense(xydata.dataItemsPerX, activation: "relu"), //, input_shape: new TensorShape(-1, xydata.dataItemsPerX)), keras.layers.Dropout(0.2F), keras.layers.Dense(12, activation: "relu"), keras.layers.Dropout(0.2F), keras.layers.Dense(6, activation: "relu"), keras.layers.Dense(numberOfClasses, activation: "softmax"), }); //var loss = new SGD(0.05F); //var optimiser = new SparseCategoricalCrossentropy(); //model.compile(loss, optimiser, new[] { "accuracy" }); //model.compile(new SGD(0.1F), new SparseCategoricalCrossentropy(), new[] { "accuracy" }); // logits and labels must have the same first dimension, got logits shape [5,3] and labels shape [15]' _model.compile( keras.optimizers.Adam(0.01F), keras.losses.CategoricalCrossentropy(), new[] { "acc" }); //here // SparseCategoricalCrossentropy? Validation set? More generated data? _model.fit(x, y, 5, 100, 1, validation_split: 0.1F); Log.Info("Training complete"); }); }));//, TaskCreationOptions.LongRunning); }