Skip to main content

Code Playground

Reference templates for common AI/ML patterns. Copy and run in your local environment.

How to Use

  1. 1. Copy the code
  2. 2. Open a terminal or Jupyter notebook
  3. 3. Paste and run
  4. 4. Experiment by changing parameters

Linear Regression

python
1import numpy as np
2import tensorflow as tf
3
4# Generate synthetic data
5np.random.seed(42)
6X = np.random.randn(100, 1).astype(np.float32)
7y = 3 * X + 2 + np.random.randn(100, 1).astype(np.float32) * 0.5
8
9# Build model
10model = tf.keras.Sequential([
11    tf.keras.layers.Dense(1, input_shape=(1,))
12])
13
14model.compile(optimizer='sgd', loss='mse')
15history = model.fit(X, y, epochs=100, verbose=0)
16
17# Check learned parameters
18w, b = model.layers[0].get_weights()
19print(f"Learned weight: {w[0][0]:.2f} (true: 3.0)")
20print(f"Learned bias: {b[0]:.2f} (true: 2.0)")
21print(f"Final loss: {history.history['loss'][-1]:.4f}")