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"); }
public static void Run() { //var ((x_train, y_train), (x_test, y_test)) = IMDB.LoadData(num_words: top_words); var((x_train, y_train), (x_test, y_test)) = LoadDataRussianLanguageToxicComments( trainCount: train_count, testCount: test_count, numWords: top_words, maxWords: max_words); //Не нужно массивы дополнять до 500 элементов, т.к. они уже размером в 500 элементов //x_train = SequenceUtil.PadSequences(x_train, maxlen: max_words); //x_test = SequenceUtil.PadSequences(x_test, maxlen: max_words); //Create model Sequential model = new Sequential(); model.Add(new Embedding(top_words, 32, input_length: max_words)); model.Add(new Conv1D(filters: 32, kernel_size: 3, padding: "same", activation: "relu")); model.Add(new MaxPooling1D(pool_size: 2)); model.Add(new Flatten()); model.Add(new Dense(250, activation: "relu")); model.Add(new Dense(1, activation: "sigmoid")); model.Compile(loss: "binary_crossentropy", optimizer: "adam", metrics: new string[] { "accuracy" }); model.Summary(); // Fit the model model.Fit(x_train, y_train, validation_data: new NDarray[] { x_test, y_test }, epochs: 2 /*10*/, batch_size: 128, verbose: 2); // Final evaluation of the model var scores = model.Evaluate(x_test, y_test, verbose: 0); Console.WriteLine("Accuracy: " + (scores[1] * 100)); model.Save("model.h5"); File.WriteAllText("model.json", model.ToJson()); //save model //model.SaveTensorflowJSFormat("./"); //error - Cannot perform runtime binding on a null reference }
// Training method and helper for a selectable SNN or CNN type network from a given config // Resulting model and scores are stored for further use public void Process(Config _config, int num_classes) { if (x_train == null || y_train == null || x_test == null || y_test == null) { throw new Exception("Dataset was null!"); } dtStart = DateTime.Now; config = _config; Shape input_shape = null; if (config.isCNN) { input_shape = GetShape(); } if (config.isNormalize) { x_train = x_train.astype(np.float32); x_test = x_test.astype(np.float32); x_train /= 255; x_test /= 255; } Console.WriteLine("Test Started {0}", dtStart); Console.WriteLine("Network Type: {0} Neural Network", (!config.isCNN) ? "Simple" : "Convolution"); Console.WriteLine("Width: {0} Height: {1} Size:{2}", width, height, width * height); Console.WriteLine("x_train shape: {0} x_train samples: {1} x_test shape: {2} x_test samples: {3}", x_train.shape, x_train.shape[0], x_test.shape, x_test.shape[0]); y_train = Util.ToCategorical(y_train, num_classes); y_test = Util.ToCategorical(y_test, num_classes); starttime = Utils.GetDateTime_Formatted(); log_file = Utils.GetFileWithExtension(log_dir, starttime, log_ext); if (!config.isCNN) { model = ProcessSnnModel(x_train, y_train, x_test, y_test, num_classes, log_file, config); } else { model = ProcessCnnModel(input_shape, x_train, y_train, x_test, y_test, num_classes, log_file, config); } dtEnd = DateTime.Now; // Score the model for performance score = model.Evaluate(x_test, y_test, verbose: 0); TimeSpan ts = dtEnd - dtStart; model.Summary(); Console.WriteLine("Test End: {0} Duration: {1}:{2}.{3}", dtEnd, ts.Hours, ts.Minutes, ts.Seconds); Console.WriteLine("Loss: {0} Accuracy: {1}", score[0], score[1]); }
public static void Run() { //Load IMDb dataset var((x_train, y_train), (x_test, y_test)) = IMDB.LoadData(); var X = np.concatenate(new NDarray[] { x_train, x_test }, axis: 0); var Y = np.concatenate(new NDarray[] { y_train, y_test }, axis: 0); Console.WriteLine("Shape of X: " + X.shape); Console.WriteLine("Shape of Y: " + Y.shape); //We can get an idea of the total number of unique words in the dataset. Console.WriteLine("Number of words: "); var hstack = np.hstack(new NDarray[] { X }); //var unique = hstack.unique(); //Console.WriteLine(np.unique(np.hstack(new NDarray[] { X })).Item1); // Load the dataset but only keep the top n words, zero the rest int top_words = 1000;// 5000; ((x_train, y_train), (x_test, y_test)) = IMDB.LoadData(num_words: top_words); int max_words = 500; x_train = SequenceUtil.PadSequences(x_train, maxlen: max_words); x_test = SequenceUtil.PadSequences(x_test, maxlen: max_words); //Create model Sequential model = new Sequential(); model.Add(new Embedding(top_words, 32, input_length: max_words)); model.Add(new Conv1D(filters: 32, kernel_size: 3, padding: "same", activation: "relu")); model.Add(new MaxPooling1D(pool_size: 2)); model.Add(new Flatten()); model.Add(new Dense(250, activation: "relu")); model.Add(new Dense(1, activation: "sigmoid")); model.Compile(loss: "binary_crossentropy", optimizer: "adam", metrics: new string[] { "accuracy" }); model.Summary(); // Fit the model model.Fit(x_train, y_train, validation_data: new NDarray[] { x_test, y_test }, epochs: 1 /*10*/, batch_size: 128, verbose: 2); // Final evaluation of the model var scores = model.Evaluate(x_test, y_test, verbose: 0); Console.WriteLine("Accuracy: " + (scores[1] * 100)); model.Save("model.h5"); File.WriteAllText("model.json", model.ToJson()); //save model //model.SaveTensorflowJSFormat("./"); //error - Cannot perform runtime binding on a null reference }
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"); }
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); }