Skip to content

Add native JavaScript(TypeScript) to replace translation.py #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions translation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
25 changes: 23 additions & 2 deletions translation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ API of TensorFlow.js.
It demonstrates loading a pretrained model hosted at a URL, using
`tf.loadLayersModel()`

## Training Demo

The training data was 149,861 English-French sentence pairs available from [http://www.manythings.org/anki](http://www.manythings.org/anki).

### JavaScript/TypeScript Version

To train the demo in JavaScript, do

```sh
yarn train ${DATA_PATH}
```

The model was trained in Node.js with Tensorflow.js, which the model code is converted from Python to TypeScript by @[huan](https://github.com/huan) based on the [translation.py](https://github.com/tensorflow/tfjs-examples/blob/master/translation/python/translation.py) example.

### Python Version

```sh
python python/translation.py ${DATA_PATH}
```

The model was trained in Python Keras, based on the [lstm_seq2seq](https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py)
example. The training data was 149,861 English-French sentence pairs available
from [http://www.manythings.org/anki](http://www.manythings.org/anki).
example.

## LANCH DEMO

To launch the demo, do

Expand Down
471 changes: 471 additions & 0 deletions translation/TRAIN.javascript.md

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions translation/TRAIN.python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
$ python translation.py ~/git/javascript-concist-chit-chat/data/fra.txt
Using TensorFlow backend.
Number of samples: 10000
Number of unique input tokens: 69
Number of unique output tokens: 93
Max sequence length for inputs: 16
Max sequence length for outputs: 59
Saved metadata at: /tmp/translation.keras/metadata.json
Train on 8000 samples, validate on 2000 samples
Epoch 1/20
2019-03-07 23:40:53.879374: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
8000/8000 [==============================] - 24s 3ms/step - loss: 0.9242 - val_loss: 0.9666
Epoch 2/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.7361 - val_loss: 0.7743
Epoch 3/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.6229 - val_loss: 0.6941
Epoch 4/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.5664 - val_loss: 0.6479
Epoch 5/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.5270 - val_loss: 0.6120
Epoch 6/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.4960 - val_loss: 0.5716
Epoch 7/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.4691 - val_loss: 0.5653
Epoch 8/20
8000/8000 [==============================] - 23s 3ms/step - loss: 0.4458 - val_loss: 0.5349
Epoch 9/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.4261 - val_loss: 0.5236
Epoch 10/20
8000/8000 [==============================] - 23s 3ms/step - loss: 0.4085 - val_loss: 0.5067
Epoch 11/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3930 - val_loss: 0.4963
Epoch 12/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3783 - val_loss: 0.4877
Epoch 13/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3646 - val_loss: 0.4846
Epoch 14/20
8000/8000 [==============================] - 23s 3ms/step - loss: 0.3517 - val_loss: 0.4780
Epoch 15/20
8000/8000 [==============================] - 23s 3ms/step - loss: 0.3396 - val_loss: 0.4721
Epoch 16/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3278 - val_loss: 0.4631
Epoch 17/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3168 - val_loss: 0.4683
Epoch 18/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.3061 - val_loss: 0.4607
Epoch 19/20
8000/8000 [==============================] - 24s 3ms/step - loss: 0.2961 - val_loss: 0.4574
Epoch 20/20
8000/8000 [==============================] - 23s 3ms/step - loss: 0.2863 - val_loss: 0.4564
WARNING:tensorflow:Layer lstm_1 was passed non-serializable keyword arguments: {'initial_state': [<tf.Tensor 'lstm/while/Exit_2:0' shape=(?, 256) dtype=float32>, <tf.Tensor 'lstm/while/Exit_3:0' shape=(?, 256) dtype=float32>]}. They will not be included in the serialized model (and thus will be missing at deserialization time).
-
Input sentence: Go.
Target sentence: Va !
Decoded sentence: Continuez à nouveau.

-
Input sentence: Hi.
Target sentence: Salut !
Decoded sentence: Restez aven !

-
Input sentence: Run!
Target sentence: Cours !
Decoded sentence: Sais-tou ?
5 changes: 1 addition & 4 deletions translation/build-resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ mkdir -p "${RESOURCES_ROOT}"

python "${DEMO_DIR}/python/translation.py" \
"${TRAIN_DATA_PATH}" \
--recurrent_initializer glorot_uniform \
--recurrent_initializer orthogonal \
--artifacts_dir "${RESOURCES_ROOT}" \
--epochs "${TRAIN_EPOCHS}"
# TODO(cais): This --recurrent_initializer is a workaround for the limitation
# in TensorFlow.js Layers that the default recurrent initializer "Orthogonal" is
# currently not supported. Remove this once "Orthogonal" becomes available.

cd ${DEMO_DIR}
yarn
Expand Down
11 changes: 11 additions & 0 deletions translation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
"scripts": {
"watch": "./serve.sh",
"build": "cross-env NODE_ENV=production parcel build index.html --no-minify --public-url ./",
"train": "node -r ts-node/register translation.ts --artifacts_dir dist/resources",
"link-local": "yalc link"
},
"devDependencies": {
"@types/argparse": "^1.0.36",
"@types/mkdirp": "^0.5.2",
"@tensorflow/tfjs-node": "^0.3.1",
"babel-core": "^6.26.3",
"babel-plugin-transform-runtime": "~6.23.0",
"babel-polyfill": "~6.26.0",
Expand All @@ -26,6 +30,13 @@
"cross-env": "^5.1.6",
"http-server": "~0.10.0",
"parcel-bundler": "~1.10.3",
"ts-node": "^8.0.3",
"typescript": "^3.3.3333",
"argparse": "^1.0.10",
"invert-kv": "^2.0.0",
"mkdirp": "^0.5.1",
"readline": "^1.3.0",
"zip-array": "^1.0.1",
"yalc": "~1.0.0-pre.22"
}
}
Loading