-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.php
75 lines (60 loc) · 1.99 KB
/
train.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
include __DIR__ . '/vendor/autoload.php';
use Rubix\ML\Loggers\Screen;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\PersistentModel;
use Rubix\ML\Pipeline;
use Rubix\ML\Transformers\TextNormalizer;
use Rubix\ML\Transformers\WordCountVectorizer;
use Rubix\ML\Tokenizers\NGram;
use Rubix\ML\Transformers\TfIdfTransformer;
use Rubix\ML\Transformers\ZScaleStandardizer;
use Rubix\ML\Classifiers\MultilayerPerceptron;
use Rubix\ML\NeuralNet\Layers\Dense;
use Rubix\ML\NeuralNet\Layers\Activation;
use Rubix\ML\NeuralNet\Layers\PReLU;
use Rubix\ML\NeuralNet\Layers\BatchNorm;
use Rubix\ML\NeuralNet\ActivationFunctions\LeakyReLU;
use Rubix\ML\NeuralNet\Optimizers\AdaMax;
use Rubix\ML\Persisters\Filesystem;
use Rubix\ML\Extractors\CSV;
ini_set('memory_limit', '-1');
$logger = new Screen();
$logger->info('Loading data into memory');
$samples = $labels = [];
foreach (['positive', 'negative'] as $label) {
foreach (glob("train/$label/*.txt") as $file) {
$samples[] = [file_get_contents($file)];
$labels[] = $label;
}
}
$dataset = new Labeled($samples, $labels);
$estimator = new PersistentModel(
new Pipeline([
new TextNormalizer(),
new WordCountVectorizer(10000, 2, 0.4, new NGram(1, 2)),
new TfIdfTransformer(),
new ZScaleStandardizer(),
], new MultilayerPerceptron([
new Dense(100),
new Activation(new LeakyReLU()),
new Dense(100),
new Activation(new LeakyReLU()),
new Dense(100, 0.0, false),
new BatchNorm(),
new Activation(new LeakyReLU()),
new Dense(50),
new PReLU(),
new Dense(50),
new PReLU(),
], 256, new AdaMax(0.0001))),
new Filesystem('sentiment.rbx', true)
);
$estimator->setLogger($logger);
$estimator->train($dataset);
$extractor = new CSV('progress.csv', true);
$extractor->export($estimator->steps());
$logger->info('Progress saved to progress.csv');
if (strtolower(trim(readline('Save this model? (y|[n]): '))) === 'y') {
$estimator->save();
}