public static void Train() { var model = new Sequential(); // embedding layer model.Add(new Embedding(output, 100, input_length: 32)); model.Add(new Conv1D(64, 3, padding: "causal", activation: "tanh")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); model.Add(new Conv1D(128, 3, activation: "relu", dilation_rate: 2, padding: "causal")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); model.Add(new Conv1D(256, 3, activation: "relu", dilation_rate: 4, padding: "causal")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); //model.Add(new Conv1D(256, 5, activation: "relu")); model.Add(new GlobalMaxPooling1D()); model.Add(new Dense(256, activation: "relu")); model.Add(new Dense(output, activation: "softmax")); model.Compile(loss: "sparse_categorical_crossentropy", optimizer: new Adam()); model.Summary(); var mc = new ModelCheckpoint("best_model.h5", monitor: "val_loss", mode: "min", save_best_only: true, verbose: 1); var history = model.Fit(train_x, train_y, batch_size: 32, epochs: 100, validation_split: 0.25f, verbose: 1, callbacks: new Callback[] { mc }); model.Save("last_epoch.h5"); }
private Sequential FitAndEvaluate(Sequential newModel, string bestWeightsFile) { int imgHeight = 75; int trainSamples = 31980; int testSamples = 4420; int epochs = 20; int batchSize = 26; string trainDirectory = "E:\\Projects\\MLProject1\\MLProject1\\bin\\Debug\\data\\Train"; string testDirectory = "E:\\Projects\\MLProject1\\MLProject1\\bin\\Debug\\data\\Test"; string validationDirectory = "E:\\Projects\\MLProject1\\MLProject1\\bin\\Debug\\data\\Valid"; ImageDataGenerator generator = new ImageDataGenerator(rescale: (float)(1.00 / 255.00)); //load and iterate training dataset KerasIterator trainIterator = generator.FlowFromDirectory(trainDirectory, class_mode: "categorical", target_size: new Tuple <int, int>(imgHeight, imgHeight), batch_size: batchSize); //color_mode:"grayscale"); // load and iterate testing dataset KerasIterator testIterator = generator.FlowFromDirectory(testDirectory, class_mode: "categorical", target_size: new Tuple <int, int>(imgHeight, imgHeight), batch_size: batchSize); //color_mode: "grayscale"); KerasIterator validationIterator = generator.FlowFromDirectory(validationDirectory, class_mode: "categorical", target_size: new Tuple <int, int>(imgHeight, imgHeight), batch_size: batchSize); ModelCheckpoint checkpoint = new ModelCheckpoint(filepath: bestWeightsFile, monitor: "val_accuracy", verbose: 1, save_best_only: true); newModel.FitGenerator(trainIterator, steps_per_epoch: trainSamples / batchSize, epochs: epochs, verbose: 1, validation_data: validationIterator, callbacks: new Callback[] { checkpoint }); //evaluate model double[] loss = newModel.EvaluateGenerator(testIterator, steps: testSamples / batchSize); foreach (double d in loss) { Console.Write(d + " "); } return(newModel); }
public static Sequential TrainNetworkWithData(TrainingData train, TrainingData validation = null) { // Disable gpu computing for dense network (due to limited parallelization possibilities and empirically observed negative speedup) Environment.SetEnvironmentVariable("CUDA_VISIBLE_DEVICES", "-1"); const int batchSize = 128; const int epochs = 500; const int verbose = 2; const bool shuffle = false; var data = TrainValidationSamplesToNumPyTypes(train, validation); Sequential BuildNetworkTopology() { var dnn = new Sequential(); dnn.Add(new Dense(512, NumFeatures, "relu", kernel_initializer: "uniform")); var hiddenLayerSizes = new List <int> { 256, 128, 64, 32, 16 }; foreach (var size in hiddenLayerSizes) { dnn.Add(new Dense(size, activation: "relu", kernel_initializer: "uniform")); } dnn.Add(new Dense(1, activation: "sigmoid", kernel_initializer: "uniform")); return(dnn); } var model = BuildNetworkTopology(); model.Compile("adam", "mse", new string[] { }); model.Summary(); var checkpoint = new ModelCheckpoint("best_model.hdf5"); var callbacks = new Callback[] { checkpoint }; if (validation == null) { model.Fit(data.TrainXs, data.TrainYs, batchSize, epochs, verbose, shuffle: shuffle, callbacks: callbacks); } else { var validationData = new[] { data.ValidationXs, data.ValidationYs }; model.Fit(data.TrainXs, data.TrainYs, batchSize, epochs, verbose, shuffle: shuffle, validation_data: validationData, callbacks: callbacks); } return(model); }
public static void BuildAndTrain() { //Model to hold the neural network architecture which in this case is WaveNet var model = new Sequential(); // Starts with embedding layer model.Add(new Embedding(output, 100, input_length: 32)); model.Add(new Conv1D(64, 3, padding: "causal", activation: "tanh")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); model.Add(new Conv1D(128, 3, activation: "relu", dilation_rate: 2, padding: "causal")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); model.Add(new Conv1D(256, 3, activation: "relu", dilation_rate: 4, padding: "causal")); model.Add(new Dropout(0.2)); model.Add(new MaxPooling1D(2)); //model.Add(new Conv1D(256, 5, activation: "relu")); model.Add(new GlobalMaxPooling1D()); model.Add(new Dense(256, activation: "relu")); model.Add(new Dense(output, activation: "softmax")); // Compile with Adam optimizer model.Compile(loss: "sparse_categorical_crossentropy", optimizer: new Adam()); model.Summary(); // Callback to store the best trained model var mc = new ModelCheckpoint("best_model.h5", monitor: "val_loss", mode: "min", save_best_only: true, verbose: 1); //Method to actually train the model for 100 iteration var history = model.Fit(train_x, train_y, batch_size: 32, epochs: 100, validation_split: 0.25f, verbose: 1, callbacks: new Callback[] { mc }); // Save the final trained model which we are going to use for prediction model.Save("last_epoch.h5"); }
int Train(List <string[]>[] filesByExtension) { var random = this.Seed is null ? new Random() : new Random(this.Seed.Value); if (this.Seed != null) { tf.set_random_seed(this.Seed.Value); } var test = Split(random, filesByExtension, this.TestSplit, out filesByExtension); byte[] trainData = Sample(random, filesByExtension, CSharpOrNot.Size, count: this.TrainingSamples / SamplePart, out int[] trainValues); byte[] testData = Sample(random, test, CSharpOrNot.Size, count: this.TestSamples / SamplePart, out int[] testValues); //var checkpoint = new ModelCheckpoint( // filepath: Path.Combine(Environment.CurrentDirectory, "weights.{epoch:02d}-{val_loss:.4f}.hdf5"), // save_best_only: true, save_weights_only: true); var checkpointBest = new ModelCheckpoint( filepath: Path.Combine(Environment.CurrentDirectory, "weights.best.hdf5"), save_best_only: true, save_weights_only: true); // not present in 1.10, but present in 1.14 ((dynamic)checkpointBest).save_freq = "epoch"; ndarray <float> @in = GreyscaleImageBytesToNumPy(trainData, imageCount: this.TrainingSamples / SamplePart, width: Width, height: Height); ndarray <int> expectedOut = OutputToNumPy(trainValues); string runID = DateTime.Now.ToString("s").Replace(':', '-'); string logDir = Path.Combine(".", "logs", runID); Directory.CreateDirectory(logDir); var tensorboard = new TensorBoard(log_dir: logDir); var model = CreateModel(classCount: IncludeExtensions.Length); model.compile( optimizer: new Adam(), loss: tf.keras.losses.sparse_categorical_crossentropy_fn, metrics: new dynamic[] { "accuracy" }); model.build(input_shape: new TensorShape(null, Height, Width, 1)); model.summary(); tf.io.write_graph( tf.keras.backend.get_session().graph_def, Path.GetFullPath("logs"), name: "model.pbtxt", as_text: true); GC.Collect(); var validationInputs = GreyscaleImageBytesToNumPy(testData, imageCount: this.TestSamples / SamplePart, width: Width, height: Height); var validationOutputs = OutputToNumPy(testValues); model.fit(@in, expectedOut, batchSize: this.BatchSize, epochs: this.Epochs, callbacks: new ICallback[] { checkpointBest, tensorboard, }, validationInput: validationInputs, validationTarget: validationOutputs); model.save_weights(Path.GetFullPath("weights.final.hdf5")); var fromCheckpoint = CreateModel(classCount: IncludeExtensions.Length); fromCheckpoint.build(new TensorShape(null, Height, Width, 1)); fromCheckpoint.load_weights(Path.GetFullPath("weights.best.hdf5")); var evaluationResults = fromCheckpoint.evaluate(@in, (ndarray)expectedOut); Console.WriteLine($"reloaded: loss: {evaluationResults[0]} acc: {evaluationResults[1]}"); evaluationResults = model.evaluate(@in, (ndarray)expectedOut); Console.WriteLine($"original: loss: {evaluationResults[0]} acc: {evaluationResults[1]}"); return(0); }
private (History, Sequential, Dictionary <string, int>) LearnNeuralNetwork(string trainCsvPath, int num_words, int max_news_len, int nb_classes) { NDarray x_train = null; NDarray y_train = null; var trainCSV = Frame.ReadCsv(trainCsvPath, false, separators: ";"); var trainYFloat = trainCSV.Rows.Select(kvp => { return(kvp.Value.GetAs <float>("Column1")); }).ValuesAll.ToList(); var trainXString = trainCSV.Rows.Select(kvp => { return(kvp.Value.GetAs <string>("Column2")); }).ValuesAll.ToList(); var trainXStringArray = trainXString.ToArray(); //x_train = np.array(new float[,] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }); y_train = np.array(trainYFloat.ToArray()); y_train = Util.ToCategorical(y_train, nb_classes); string[][] tokens = trainXStringArray.Tokenize(); var dictionaryLikeIMDB = FrequencyDictionary.Learn(tokens); var bow = FrequencyDictionary.Transform(tokens, dictionaryLikeIMDB); // Create a new TF-IDF with options: /*var codebook = new Accord.MachineLearning.TFIDF() * { * Tf = TermFrequency.Log, * Idf = InverseDocumentFrequency.Default * }; * * codebook.Learn(tokens); * * double[][] bow = codebook.Transform(tokens);*/ var list = new List <NDarray>(); foreach (var item in bow) { //var newItem = item.Take(max_news_len).ToArray(); //var ndarray = np.array(newItem); var ndarray = np.array(item); list.Add(ndarray); } var sequences = np.array(list); //x_train = SequenceUtil.PadSequences(sequences, maxlen: max_news_len, dtype: "double"); x_train = SequenceUtil.PadSequences(sequences, maxlen: max_news_len); var model = new Sequential(); model.Add(new Embedding(num_words, 32, null, null, null, null, false, max_news_len)); model.Add(new GRU(138));//16 model.Add(new Dense(12, activation: "softmax")); model.Compile(optimizer: "adam", loss: "categorical_crossentropy", metrics: new string[] { "accuracy" }); model.Summary(); var model_gru_save_path = "best_model_gru.h5"; var checkpoint_callback_gru = new ModelCheckpoint( model_gru_save_path, "val_accuracy", 1, true ); var callbacks = new List <Callback>() { checkpoint_callback_gru }; float validation_split = (float)0.1; var history_gru = model.Fit(x_train, y_train, batch_size: 128, epochs: 10, validation_split: validation_split, callbacks: callbacks.ToArray()); //Save model and weights string json = model.ToJson(); File.WriteAllText("model.json", json); return(history_gru, model, dictionaryLikeIMDB); }