From e7a4ef5fafa57494b14351a68d82e07bf470eb18 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Thu, 6 Aug 2020 20:17:40 -0500 Subject: [PATCH] Update to Rubix ML 0.1.0 --- README.md | 28 +++++++++++++++++++++++----- composer.json | 3 +-- train.php | 10 +++++----- validate.php | 4 +++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 838db2b2..614458db 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Neural networks compute a non-linear continuous function and therefore require c First, we'll convert all characters to lowercase using [Text Normalizer](https://docs.rubixml.com/en/latest/transformers/text-normalizer.html) so that every word is represented by only a single token. Then, [Word Count Vectorizer](https://docs.rubixml.com/en/latest/transformers/word-count-vectorizer.html) creates a fixed-length continuous feature vector of word counts from the raw text and [TF-IDF Transformer](https://docs.rubixml.com/en/latest/transformers/tf-idf-transformer.html) applies a weighting scheme to those counts. Finally, [Z Scale Standardizer](https://docs.rubixml.com/en/latest/transformers/z-scale-standardizer.html) takes the TF-IDF weighted counts and centers and scales the sample matrix to have 0 mean and unit variance. This last step will help the neural network converge quicker. -The Word Count Vectorizer is a bag-of-words feature extractor that uses a fixed vocabulary and term counts to quantify the words that appear in a document. We elect to limit the size of the vocabulary to 10,000 of the most frequent words that satisfy the criteria of appearing in at least 3 different documents but no more than 5,000 documents. In this way, we limit the amount of *noise* words that enter the training set. +The Word Count Vectorizer is a bag-of-words feature extractor that uses a fixed vocabulary and term counts to quantify the words that appear in a document. We elect to limit the size of the vocabulary to 10,000 of the most frequent words that satisfy the criteria of appearing in at least 3 different documents but no more than 10,000 documents. In this way, we limit the amount of *noise* words that enter the training set. Another common text feature representation are [TF-IDF](https://en.wikipedia.org/wiki/Tf%E2%80%93idf) values which take the term frequencies (TF) from Word Count Vectorizer and weigh them by their inverse document frequencies (IDF). IDFs can be interpreted as the word's *importance* within the training corpus. Specifically, higher weight is given to words that are more rare. @@ -84,7 +84,7 @@ use Rubix\ML\Persisters\Filesystem; $estimator = new PersistentModel( new Pipeline([ new TextNormalizer(), - new WordCountVectorizer(10000, 3, 5000, new NGram(1, 2)), + new WordCountVectorizer(10000, 3, 10000, new NGram(1, 2)), new TfIdfTransformer(), new ZScaleStandardizer(), ], new MultilayerPerceptron([ @@ -123,8 +123,18 @@ $scores = $estimator->scores(); $losses = $estimator->steps(); ``` +Next, we'll use an [Unlabeled](https://docs.rubixml.com/en/latest/datasets/unlabeled.html) dataset object to temporarily store and convert the scores and losses into CSV format so that we can import the data into our favorite plotting application such as [Plotly](https://plotly.com) or [Excel](https://www.microsoft.com/en-us/microsoft-365/excel). The global `array_transpose()` function takes a 2-dimensional array and changes the rows to columns and vice versa. It is necessary to call this function in order to get the samples into the correct *shape* for the dataset object. -Here is an example of what the validation score and training loss looks like when they are plotted. The validation score should be getting better with each epoch as the loss decreases. You can generate your own plots by importing the `progress.csv` file into your favorite plotting software such as [Plotly](https://plotly.com) or [Excel](https://www.microsoft.com/en-us/microsoft-365/excel). +```php +use Rubix\ML\Datasets\Unlabeled; +use function Rubix\ML\array_transpose; + +$table = array_transpose([$scores, $losses]); + +Unlabeled::build($table)->toCSV()->write('progress.csv'); +``` + +Here is an example of what the validation score and training loss looks like when they are plotted. The validation score should be getting better with each epoch as the loss decreases. You can generate your own plots by importing the `progress.csv` file into your plotting application. ![F1 Score](https://raw.githubusercontent.com/RubixML/Sentiment/master/docs/images/validation-score.svg?sanitize=true) @@ -196,13 +206,21 @@ $report = new AggregateReport([ ]); ``` -To generate the report, pass in the predictions along with the labels from the testing set to the `generate()` method on the report. +To generate the report, pass in the predictions along with the labels from the testing set to the `generate()` method on the report. The return value is a report object that can be echoed out to the console. ```php $results = $report->generate($predictions, $dataset->labels()); + +echo $results; +``` + +We'll also save a copy of the report to a JSON file. + +```php +$results->toJSON()->write('report.json'); ``` -Now we can execute the validation script from the command line like we see below to compute the results. +Now we can execute the validation script from the command line. ```sh $ php validate.php ``` diff --git a/composer.json b/composer.json index 0039421f..8ed73d10 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,7 @@ ], "require": { "php": ">=7.2", - "league/csv": "^9.5", - "rubix/ml": "^0.1.0-rc5" + "rubix/ml": "^0.1.0" }, "suggest": { "ext-tensor": "For faster training and inference" diff --git a/train.php b/train.php index 731fb359..423d558d 100644 --- a/train.php +++ b/train.php @@ -19,7 +19,7 @@ use Rubix\ML\NeuralNet\Optimizers\AdaMax; use Rubix\ML\Persisters\Filesystem; use Rubix\ML\Other\Loggers\Screen; -use League\Csv\Writer; +use Rubix\ML\Datasets\Unlabeled; use function Rubix\ML\array_transpose; @@ -41,7 +41,7 @@ $estimator = new PersistentModel( new Pipeline([ new TextNormalizer(), - new WordCountVectorizer(10000, 3, 5000, new NGram(1, 2)), + new WordCountVectorizer(10000, 3, 10000, new NGram(1, 2)), new TfIdfTransformer(), new ZScaleStandardizer(), ], new MultilayerPerceptron([ @@ -69,9 +69,9 @@ $scores = $estimator->scores(); $losses = $estimator->steps(); -$writer = Writer::createFromPath('progress.csv', 'w+'); -$writer->insertOne(['score', 'loss']); -$writer->insertAll(array_transpose([$scores, $losses])); +Unlabeled::build(array_transpose([$scores, $losses])) + ->toCSV(['scores', 'losses']) + ->write('progress.csv'); echo 'Progress saved to progress.csv' . PHP_EOL; diff --git a/validate.php b/validate.php index 61a0bac0..69ea2ef2 100644 --- a/validate.php +++ b/validate.php @@ -37,6 +37,8 @@ $results = $report->generate($predictions, $dataset->labels()); -file_put_contents('report.json', json_encode($results, JSON_PRETTY_PRINT)); +echo $results; + +$results->toJSON()->write('report.json'); echo 'Report saved to report.json' . PHP_EOL;