Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions python-windows-machine-learning-setup/README.md
Original file line number Diff line number Diff line change
@@ -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`.
25 changes: 25 additions & 0 deletions python-windows-machine-learning-setup/nnxor.py
Original file line number Diff line number Diff line change
@@ -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))
3 changes: 3 additions & 0 deletions python-windows-machine-learning-setup/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
keras==3.15.1
numpy==2.5.3
tensorflow==2.21.0
Loading