Skip to content

Commit 6eb3aa6

Browse files
Merge pull request #1 from RonasIT/initialize-project
Initialize project
2 parents 0c5fa13 + 21c7fcc commit 6eb3aa6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+22679
-1
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

.eslintrc.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"root": true,
3+
"ignorePatterns": ["**/*"],
4+
"plugins": ["@nrwl/nx"],
5+
"overrides": [
6+
{
7+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
8+
"rules": {
9+
"@nrwl/nx/enforce-module-boundaries": [
10+
"error",
11+
{
12+
"enforceBuildableLibDependency": true,
13+
"allow": [],
14+
"depConstraints": [
15+
{
16+
"sourceTag": "*",
17+
"onlyDependOnLibsWithTags": ["*"]
18+
}
19+
]
20+
}
21+
]
22+
}
23+
},
24+
{
25+
"files": ["*.ts", "*.tsx"],
26+
"extends": ["plugin:@nrwl/nx/typescript"],
27+
"rules": {}
28+
},
29+
{
30+
"files": ["*.js", "*.jsx"],
31+
"extends": ["plugin:@nrwl/nx/javascript"],
32+
"rules": {}
33+
}
34+
]
35+
}

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Description
2+
3+
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
4+
5+
Fixes # (issue)
6+
7+
## Type of change
8+
9+
Please delete options that are not relevant.
10+
11+
- [ ] Bug fix (non-breaking change which fixes an issue)
12+
- [ ] New feature (non-breaking change which adds functionality)
13+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
14+
- [ ] This change requires a documentation update
15+
16+
# Checklist:
17+
18+
- [ ] My code follows the style guidelines of this project
19+
- [ ] I have performed a self-review of my own code
20+
- [ ] I have commented my code, particularly in hard-to-understand areas
21+
- [ ] I have made corresponding changes to the documentation
22+
- [ ] My changes generate no new warnings
23+
- [ ] Any dependent changes have been merged and published in downstream modules

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
8+
# dependencies
9+
node_modules
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
20+
# IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
# misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
yarn-error.log
34+
testem.log
35+
/typings
36+
37+
# System Files
38+
.DS_Store
39+
Thumbs.db

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Add files here to ignore them from prettier formatting
2+
3+
/dist
4+
/coverage

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

CONTRIBUTING.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CONTRIBUTING
2+
3+
Contributions are welcome, and are accepted via pull requests. Please review
4+
these guidelines before submitting any pull requests.
5+
6+
## Process
7+
8+
1. Fork the project.
9+
1. Create a new branch.
10+
1. Code, test, commit and push.
11+
1. Open a pull request detailing your changes. Make sure to follow the
12+
[template][1].
13+
14+
## Guidelines
15+
16+
* Please follow the [TypeScript Style Guide by Google][2].
17+
* Send a coherent commit history, making sure each individual commit in your
18+
pull request is meaningful.
19+
* Please follow the [Conventional Commits specification][3].
20+
* You may need to [rebase][4] to avoid merge conflicts.
21+
* Please remember that we follow [SemVer][5].
22+
23+
[1]:.github/PULL_REQUEST_TEMPLATE.md
24+
[2]:https://google.github.io/styleguide/tsguide.html
25+
[3]:https://www.conventionalcommits.org/en/v1.0.0
26+
[4]:https://git-scm.com/book/en/v2/Git-Branching-Rebasing
27+
[5]:http://semver.org

README.md

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,185 @@
1-
# tfjs-node-helpers
1+
# tfjs-node-helpers
2+
3+
## Introduction
4+
5+
This library was created to simplify the work with [TensorFlow.js][1] in
6+
[Node][2].
7+
8+
Currently, this library provides the helpers for the
9+
[binary classification task][3].
10+
The long-term plan is to implement helpers for other tasks, for example,
11+
[regression][4] and [multiclass classification][5]), as well as to cover
12+
different [machine learning approaches][6].
13+
14+
## Installation
15+
16+
Before you start using the helpers in your project, you need to install the
17+
[@ronas-it/tfjs-node-helpers][7] package:
18+
19+
```bash
20+
> npm install @ronas-it/tfjs-node-helpers --save
21+
```
22+
23+
## Usage
24+
25+
### Feature extraction
26+
27+
Before training any model, you need to extract the valuable information
28+
from your dataset. This information is usually called *features*.
29+
This library provides a few helpers to streamline the process of feature extraction.
30+
31+
First, you need to define the feature extractors. In the example below we
32+
extract the `gender` feature from the dataset item. For that we create a
33+
`GenderFeatureExtractor` class extending the `FeatureExtractor` base class
34+
provided by the library. Please note that feature extractors also encode the
35+
extracted information as a number in the range between `0` and `1`, so that it
36+
can be consumed when training the model.
37+
38+
```typescript
39+
type DatasetItem = {
40+
id: number;
41+
gender: string;
42+
age: number;
43+
annual_salary: number;
44+
owns_the_car: number;
45+
};
46+
47+
class GenderFeatureExtractor extends FeatureExtractor<DatasetItem, FeatureType> {
48+
public featureType = FeatureType.GENDER;
49+
50+
public extract(item: DatasetItem): Feature<FeatureType> {
51+
return new Feature({
52+
type: this.featureType,
53+
label: item.gender,
54+
value: (item.gender === 'Male') ? 1 : 0
55+
});
56+
}
57+
}
58+
```
59+
60+
That's it! Now we can use the defined feature extractor to extract valuable
61+
information from our dataset.
62+
63+
### Binary classification
64+
65+
This library provides two classes to train and evaluate binary classification
66+
models:
67+
68+
1. `BinaryClassificationTrainer` – used for training and testing.
69+
1. `BinaryClassifier` – used for evaluation.
70+
71+
#### Creating the trainer
72+
73+
Before training the model, you need to create an instance of the
74+
`BinaryClassificationTrainer` class first and provide a few parameters:
75+
76+
- `batchSize` – the number of training samples in each batch.
77+
- `epochs` – the maximum number of iterations that we should train the model.
78+
- `patience` – the number of iterations after which the trainer will stop if
79+
there is no improvement.
80+
- `hiddenLayers` – a list of hidden layers. You can also provide the custom
81+
model by using the optional `model` parameter instead.
82+
- `inputFeatureExtractors` – a list of feature extractors to extract information
83+
that should be fed into the model as inputs.
84+
- `outputFeatureExtractor` – the feature extractor to extract information that
85+
we want to predict.
86+
87+
An example can be found below:
88+
89+
```typescript
90+
const trainer = new BinaryClassificationTrainer({
91+
batchSize: BATCH_SIZE,
92+
epochs: EPOCHS,
93+
patience: PATIENCE,
94+
hiddenLayers: [
95+
layers.dense({ units: 128, activation: 'mish' }),
96+
layers.dense({ units: 128, activation: 'mish' })
97+
],
98+
inputFeatureExtractors: [
99+
new AgeFeatureExtractor(),
100+
new AnnualSalaryFeatureExtractor(),
101+
new GenderFeatureExtractor()
102+
],
103+
outputFeatureExtractor: new OwnsTheCarFeatureExtractor()
104+
});
105+
```
106+
107+
#### Training and testing
108+
109+
To train the model, you need to call the `trainAndTest` method of the
110+
instantiated `BinaryClassificationTrainer`.
111+
112+
You can pass the `data` parameter, and in this case trainer will extract
113+
features from the provided dataset first. If you want something more customized,
114+
then you can create the datasets for training, validation and testing manually,
115+
and pass them as the `trainingDataset`, `validationDataset` and `testingDataset`
116+
parameters.
117+
118+
You can also print the testing results by setting the `printResults` to `true`.
119+
120+
An example can be found below:
121+
122+
```typescript
123+
await trainer.trainAndTest({
124+
data,
125+
printResults: true
126+
});
127+
```
128+
129+
#### Saving the model
130+
131+
To save the trained model, you need to call the `save` method of the
132+
instantiated `BinaryClassificationTrainer` and pass the path where the model
133+
should be saved:
134+
135+
```typescript
136+
await trainer.save(join(__dirname, './trained_model'));
137+
```
138+
139+
#### Creating the classifier
140+
141+
Before evaluating the model, you need to create an instance of the
142+
`BinaryClassifier` class:
143+
144+
```typescript
145+
const classifier = new BinaryClassifier();
146+
```
147+
148+
#### Loading the trained model
149+
150+
To load the trained model, you need to call the `load` method of the
151+
instantiated `BinaryClassifier` class and pass the path where the model json
152+
file is located:
153+
154+
```typescript
155+
await classifier.load(join(__dirname, './trained_model/model.json'));
156+
```
157+
158+
#### Evaluation
159+
160+
To evaluate the trained model, you need to load it first, and then call the
161+
`predict` method of the instantiated `BinaryClassifier` class and pass an array
162+
of encoded inputs which will be fed into the model:
163+
164+
```typescript
165+
const ownsTheCar = await classifier.predict([0.2, 0.76, 0]);
166+
```
167+
168+
## Contributing
169+
170+
Thank you for considering contributing to `tfjs-node-helpers` library! The
171+
contribution guide can be found in the [Contributing guide][8].
172+
173+
## License
174+
175+
`tfjs-node-helpers` is licensed under the [MIT license][9].
176+
177+
[1]:https://www.tensorflow.org/js
178+
[2]:https://nodejs.org
179+
[3]:https://en.wikipedia.org/wiki/Binary_classification
180+
[4]:https://en.wikipedia.org/wiki/Regression_analysis
181+
[5]:https://en.wikipedia.org/wiki/Multiclass_classification
182+
[6]:https://en.wikipedia.org/wiki/Machine_learning#Approaches
183+
[7]:https://www.npmjs.com/package/@ronas-it/tfjs-node-helpers
184+
[8]:CONTRIBUTING.md
185+
[9]:LICENSE

jest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getJestProjects } from '@nrwl/jest';
2+
3+
export default {
4+
projects: getJestProjects(),
5+
};

jest.preset.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const nxPreset = require('@nrwl/jest/preset').default;
2+
3+
module.exports = { ...nxPreset };

0 commit comments

Comments
 (0)