Files
kotlin-fork/kotlin-native/samples/torch
Stanislav Erokhin f624800b84 Move everything under kotlin-native folder
I was forced to manually do update the following files, because otherwise
they would be ignored according .gitignore settings. Probably they
should be deleted from repo.

Interop/.idea/compiler.xml
Interop/.idea/gradle.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml
Interop/.idea/modules.xml
Interop/.idea/modules/Indexer/Indexer.iml
Interop/.idea/modules/Runtime/Runtime.iml
Interop/.idea/modules/StubGenerator/StubGenerator.iml
backend.native/backend.native.iml
backend.native/bc.frontend/bc.frontend.iml
backend.native/cli.bc/cli.bc.iml
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt
backend.native/tests/link/lib/foo.kt
backend.native/tests/link/lib/foo2.kt
backend.native/tests/teamcity-test.property
2020-10-27 21:00:28 +03:00
..

Torch demo

Trains a handwritten digit classifier using the Torch C backend. Like other Torch clients, most prominently PyTorch, this example is built on top of the ATen C API, showing how a Torch client for Kotlin/Native could look like.

Installation

To build ATen (Torch for C), make sure you have Python 2.X and pyyaml installed:

# macOS: if you don't have pip
sudo easy_install pip
# Linux: if you don't have pip
apt-get -y install python-pip

# if you don't have pyyaml or typing
sudo pip install pyyaml typing

Now

./downloadTorch.sh

will install it into $HOME/.konan/third-party/torch (if not yet done). One may override the location of third-party/torch by setting the KONAN_DATA_DIR environment variable.

To build use ../gradlew assemble.

./downloadMNIST.sh

will download and unzip the MNIST dataset of 70000 labeled handwritten digits for training and testing a classifier (if not yet done).

Then run

../gradlew runReleaseExecutableTorch

Alternatively you can run the artifact directly through

./build/bin/torch/main/release/executable/torch.kexe

You may need to specify LD_LIBRARY_PATH or DYLD_LIBRARY_PATH environment variables to point to $HOME/.konan/third-party/torch/lib if the ATen dynamic library cannot be found.

Even on a CPU, training should only take some minutes, and you should observe a classification accuracy of about 95% on the test dataset.