|
| 1 | +# Python Bindings for neural-network |
| 2 | + |
| 3 | +This subdirectory contains the Visual Studio 2022 C++ project and code necessary to build and run native Python bindings for the `myoddweb::nn` neural network library using `pybind11`. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Folder Layout |
| 8 | + |
| 9 | +* `bindings.cpp`: C++ source file defining the `pybind11` wrapper layer, mapping C++ types, enums, and classes into Python. |
| 10 | +* `neuralnetwork_py.vcxproj`: Visual Studio C++ project file configured to build a dynamic library output with a `.pyd` file extension. |
| 11 | +* `neuralnetwork_py.vcxproj.filters`: Project filters mapping for Solution Explorer organization. |
| 12 | +* `neuralnetwork_py.sln`: Main Visual Studio solution. |
| 13 | +* `example.py`: Python script illustrating options configuration, model instantiation, callback monitoring, training, inference, and serialization. |
| 14 | +* `import_check.py`: A lightweight validation script that verifies the binary module loads, initializes enums, and starts up correctly (used in CI). |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Build Prerequisites |
| 19 | + |
| 20 | +1. **Visual Studio 2022** with the **Desktop development with C++** workload installed. |
| 21 | +2. **Python 3.x** (64-bit version recommended, matching the target build platform). |
| 22 | +3. **pybind11**: Install the header-only package via `pip`: |
| 23 | + ```bash |
| 24 | + pip install pybind11 |
| 25 | + ``` |
| 26 | +4. **Environment Setup**: |
| 27 | + To allow Visual Studio to locate Python headers (`python.h`) and libraries (`python3x.lib`), you should configure the `PYTHON_HOME` environment variable on your computer: |
| 28 | + * Set `PYTHON_HOME` to your Python installation folder (e.g. `C:\Users\<Name>\AppData\Local\Programs\Python\Python312` or `C:\Program Files\Python312`). |
| 29 | + * *Alternatively*, if you have Visual Studio's **Python development** workload installed, MSBuild will automatically search for the default Python paths. |
| 30 | +
|
| 31 | +--- |
| 32 | +
|
| 33 | +## Building the Module |
| 34 | +
|
| 35 | +1. Open the solution file [neuralnetwork_py.sln](file:///H:/projects/github/neural-network2/python/neuralnetwork_py.sln) in Visual Studio 2022. |
| 36 | +2. Set the solution build configuration to: |
| 37 | + * **Configuration:** `Release` |
| 38 | + * **Platform:** `x64` |
| 39 | +3. Build the solution (`Ctrl+Shift+B` or right-click the project -> **Build**). |
| 40 | +4. This compiles the library and generates `neuralnetwork.pyd` inside the `python/x64/Release/` folder. |
| 41 | +
|
| 42 | +--- |
| 43 | +
|
| 44 | +## Running the Python Example |
| 45 | +
|
| 46 | +Once the `.pyd` module is built, you can run the example script: |
| 47 | +
|
| 48 | +```bash |
| 49 | +python example.py |
| 50 | +``` |
| 51 | +
|
| 52 | +This script trains a neural network on the XOR classification task and validates predictions. |
| 53 | +
|
| 54 | +--- |
| 55 | +
|
| 56 | +## API Usage Reference |
| 57 | +
|
| 58 | +The Python bindings expose the C++ API in a clean, Pythonic wrapper inside the `neuralnetwork` module: |
| 59 | +
|
| 60 | +### Enums |
| 61 | +* `nn.ActivationMethod`: `Linear`, `Sigmoid`, `Tanh`, `Relu`, `LeakyRelu`, `PRelu`, `Selu`, `Swish`, `Mish`, `Gelu`, `Elu`, `Softmax`. |
| 62 | +* `nn.OptimiserType`: `SGD`, `Momentum`, `Nesterov`, `RMSProp`, `Adam`, `AdamW`, `AdaGrad`, `AdaDelta`, `Nadam`, `NadamW`, `AMSGrad`, `LAMB`, `Lion`, `None`. |
| 63 | +* `nn.ErrorCalculationType`: `None`, `HuberLoss`, `HuberDirectionLoss`, `MAE`, `MSE`, `RMSE`, `BCELoss`, `CrossEntropy`, etc. |
| 64 | +
|
| 65 | +### Core Classes & Properties |
| 66 | +* `nn.Activation(method, alpha, temperature=1.0)`: Represents the activation configuration. |
| 67 | +* `nn.NeuralNetworkOptions.create(topology)`: Returns a builder options object. Exposes builder methods such as: |
| 68 | + * `.with_number_of_epoch(500)` |
| 69 | + * `.with_learning_rate(0.15)` |
| 70 | + * `.with_batch_size(4)` |
| 71 | + * `.with_progress_callback(callback_function)` (allows native Python functions to serve as callbacks during training). |
| 72 | +* `nn.NeuralNetwork(options)`: Core model executor. Methods: |
| 73 | + * `.train(inputs, outputs)`: Input arguments are standard Python nested lists of floats (e.g. `[[0, 0, 1], [1, 0, 1]]`). |
| 74 | + * `.think(inputs)`: Returns predictions as Python lists. |
| 75 | +* `nn.NeuralNetworkSerializer`: Exposes static methods: |
| 76 | + * `nn.NeuralNetworkSerializer.save(net, filepath)` |
| 77 | + * `nn.NeuralNetworkSerializer.load(filepath)` (returns a deserialized network instance). |
0 commit comments