diff --git a/python-windows-machine-learning-setup/README.md b/python-windows-machine-learning-setup/README.md new file mode 100644 index 0000000000..8ddd124077 --- /dev/null +++ b/python-windows-machine-learning-setup/README.md @@ -0,0 +1,28 @@ +# Setting Up Python for Machine Learning on Windows + +This folder provides the sample code for the Real Python tutorial [Setting Up Python for Machine Learning on Windows](https://realpython.com/python-windows-machine-learning-setup/). + +## Setup + +The tutorial uses Conda. Create and activate an environment with the tested versions: + +```sh +conda create --name nnxor python=3.13 keras=3.15 tensorflow=2.21 +conda activate nnxor +``` + +Alternatively, you can install the pinned dependencies with `pip` into a virtual environment running Python 3.12 or 3.13, which are the versions that these pins support: + +```sh +python -m pip install -r requirements.txt +``` + +## Usage + +Train the XOR neural network and print its predictions: + +```sh +python nnxor.py +``` + +Training runs for 5000 epochs and takes a couple of minutes. The final predictions should be close to `0`, `1`, `1`, and `0`. diff --git a/python-windows-machine-learning-setup/nnxor.py b/python-windows-machine-learning-setup/nnxor.py new file mode 100644 index 0000000000..bfdb9b9848 --- /dev/null +++ b/python-windows-machine-learning-setup/nnxor.py @@ -0,0 +1,25 @@ +import numpy as np +from keras.layers import Activation, Dense, Input +from keras.models import Sequential +from keras.optimizers import SGD +from keras.utils import set_random_seed + +set_random_seed(444) + +X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) +y = np.array([[0], [1], [1], [0]]) + +model = Sequential() +model.add(Input(shape=(2,))) +model.add(Dense(2)) +model.add(Activation("sigmoid")) +model.add(Dense(1)) +model.add(Activation("sigmoid")) + +sgd = SGD(learning_rate=0.1) +model.compile(loss="mean_squared_error", optimizer=sgd) + +model.fit(X, y, batch_size=1, epochs=5000) + +if __name__ == "__main__": + print(model.predict(X)) diff --git a/python-windows-machine-learning-setup/requirements.txt b/python-windows-machine-learning-setup/requirements.txt new file mode 100644 index 0000000000..2a60ce9bbc --- /dev/null +++ b/python-windows-machine-learning-setup/requirements.txt @@ -0,0 +1,3 @@ +keras==3.15.1 +numpy==2.5.3 +tensorflow==2.21.0