ML: Python Virtual Environment + GPU

From OnnoWiki
Revision as of 05:14, 28 June 2026 by Onnowpurbo (talk | contribs) (Created page with "Berikut setup praktis di Ubuntu/Linux. mkdir -p ~/Apps/Python cd ~/Apps/Python == 1. Install paket dasar== sudo apt update sudo apt install -y python3 python3-venv pyth...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Berikut setup praktis di Ubuntu/Linux.

mkdir -p ~/Apps/Python
cd ~/Apps/Python

1. Install paket dasar

sudo apt update
sudo apt install -y python3 python3-venv python3-pip python3-dev build-essential

2. Buat virtual environment

cd ~/Apps/Python
python3 -m venv ml-env
source ~/Apps/Python/ml-env/bin/activate

Upgrade pip:

pip install --upgrade pip setuptools wheel

3. Install paket Python ML

pip install numpy pandas matplotlib scikit-learn scipy seaborn
pip install jupyter notebook ipykernel

4. Install TensorFlow + Keras GPU

Untuk TensorFlow modern, jangan pakai `tensorflow-gpu`; paket itu sudah tidak dipakai. Gunakan:

pip install "tensorflow[and-cuda]"

TensorFlow resmi mendukung GPU NVIDIA di Linux/Ubuntu, dan Python yang didukung saat ini 3.9–3.12. ([TensorFlow][1]) Mulai TensorFlow 2.16, `pip install tensorflow` sudah membawa Keras 3 sebagai `tf.keras`. ([Keras][2])

5. Daftarkan kernel Jupyter

python -m ipykernel install --user --name ml-env --display-name "Python ML GPU"

Jalankan Jupyter:

cd ~/Apps/Python
jupyter notebook

6. Test TensorFlow GPU

python - << 'EOF'
import tensorflow as tf
print("TensorFlow:", tf.__version__)
print("GPU devices:", tf.config.list_physical_devices("GPU"))
EOF

Kalau GPU terbaca, akan muncul kira-kira:

GPU devices: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

7. Test Keras GPU sederhana

python - << 'EOF'
import tensorflow as tf
from tensorflow import keras
import numpy as np

print("GPU:", tf.config.list_physical_devices("GPU"))

x = np.random.random((1000, 20))
y = np.random.randint(2, size=(1000, 1))

model = keras.Sequential([
    keras.layers.Dense(64, activation="relu", input_shape=(20,)),
    keras.layers.Dense(1, activation="sigmoid")
])

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(x, y, epochs=3, batch_size=32)

print("Selesai.")
EOF

8. Cara pakai setiap hari

cd ~/Apps/Python
source ml-env/bin/activate
jupyter notebook

Kalau mau keluar dari virtual environment:

deactivate

Catatan penting: pastikan driver NVIDIA sudah aktif dulu:

nvidia-smi

Kalau `nvidia-smi` belum jalan, TensorFlow tidak akan bisa memakai GPU.

[1]: https://www.tensorflow.org/install/pip?utm_source=chatgpt.com "Install TensorFlow with pip"
[2]: https://keras.io/getting_started/?utm_source=chatgpt.com "Getting started with Keras"