A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
reorg some folders, add an example (#2176)
Akshay Agrawal007931f96479
diff --git a/examples/ai/micrograd_mlp.py b/examples/ai/micrograd_mlp.pynew file mode 100644index 0000000000..2ceec3a0de--- /dev/null+++ b/examples/ai/micrograd_mlp.py@@ -0,0 +1,165 @@+import marimo++__generated_with = "0.2.10"+app = marimo.App()+++def __(mo):+ mo.md(+ r"""+ ### Micrograd demo+ """+ )+ return+++def __():+ import random+ import numpy as np+ import matplotlib.pyplot as plt+ return np, plt, random+++def __():+ from micrograd.engine import Value+ from micrograd.nn import Neuron, Layer, MLP+ return Layer, MLP, Neuron, Value+++def __(np, random):+ np.random.seed(1337)+ random.seed(1337)+ return+++def __(plt):+ # make up a dataset++ from sklearn.datasets import make_moons, make_blobs++ X, y = make_moons(n_samples=100, noise=0.1)++ y = y * 2 - 1 # make y be -1 or 1+ # visualize in 2D+ plt.figure(figsize=(5, 5))+ plt.scatter(X[:, 0], X[:, 1], c=y, s=20, cmap="jet")+ return X, make_blobs, make_moons, y+++def __(MLP):+ # initialize a model+ model = MLP(2, [16, 16, 1]) # 2-layer neural network+ print(model)+ print("number of parameters", len(model.parameters()))+ return model,+++def __(Value, X, model, np, y):+ # loss function+ def loss(batch_size=None):++ # inline DataLoader :)+ if batch_size is None:+ Xb, yb = X, y+ else:+ ri = np.random.permutation(X.shape[0])[:batch_size]+ Xb, yb = X[ri], y[ri]+ inputs = [list(map(Value, xrow)) for xrow in Xb]++ # forward the model to get scores+ scores = list(map(model, inputs))++ # svm "max-margin" loss+ losses = [(1 + -yi * scorei).relu() for yi, scorei in zip(yb, scores)]+ data_loss = sum(losses) * (1.0 / len(losses))+ # L2 regularization+ alpha = 1e-4+ reg_loss = alpha * sum((p * p for p in model.parameters()))+ total_loss = data_loss + reg_loss++ # also get accuracy+ accuracy = [+ (yi > 0) == (scorei.data > 0) for yi, scorei in zip(yb, scores)+ ]+ return total_loss, sum(accuracy) / len(accuracy)++ _total_loss, _acc = loss()+ print(_total_loss, _acc)+ return loss,+++def __(loss, model):+ # optimization+ for k in range(20):++ # forward+ total_loss, acc = loss()++ # backward+ model.zero_grad()+ total_loss.backward()++ # update (sgd)+ learning_rate = 1.0 - 0.9 * k / 100+ for p in model.parameters():+ p.data -= learning_rate * p.grad++ if k % 1 == 0:+ print(f"step {k} loss {total_loss.data}, accuracy {acc*100}%")+ return acc, k, learning_rate, p, total_loss+++def __(Value, X, model, np, plt, y):+ # visualize decision boundary++ h = 0.25+ x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1+ y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1+ xx, yy = np.meshgrid(+ np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)+ )+ Xmesh = np.c_[xx.ravel(), yy.ravel()]+ inputs = [list(map(Value, xrow)) for xrow in Xmesh]+ scores = list(map(model, inputs))+ Z = np.array([s.data > 0 for s in scores])+ Z = Z.reshape(xx.shape)++ fig = plt.figure()+ plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)+ plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)+ plt.xlim(xx.min(), xx.max())+ plt.ylim(yy.min(), yy.max())+ plt.gca()+ return (+ Xmesh,+ Z,+ fig,+ h,+ inputs,+ scores,+ x_max,+ x_min,+ xx,+ y_max,+ y_min,+ yy,+ )+++def __():+ import marimo as mo+ return mo,+++if __name__ == "__main__":+ app.run()diff --git a/examples/google/google_cloud_bigquery.py b/examples/cloud/gcp/google_cloud_bigquery.pysimilarity index 100%rename from examples/google/google_cloud_bigquery.pyrename to examples/cloud/gcp/google_cloud_bigquery.pydiff --git a/examples/google/google_cloud_storage.py b/examples/cloud/gcp/google_cloud_storage.pysimilarity index 100%rename from examples/google/google_cloud_storage.pyrename to examples/cloud/gcp/google_cloud_storage.pydiff --git a/examples/google/google_sheets.py b/examples/cloud/gcp/google_sheets.pysimilarity index 100%rename from examples/google/google_sheets.pyrename to examples/cloud/gcp/google_sheets.pydiff --git a/examples/pokemon_stats/pokemon.py b/examples/dashboards/pokemon_stats/pokemon.pysimilarity index 100%rename from examples/pokemon_stats/pokemon.pyrename to examples/dashboards/pokemon_stats/pokemon.pydiff --git a/examples/embeddings/complete_graph.py b/examples/third_party/pymde/complete_graph.pysimilarity index 100%rename from examples/embeddings/complete_graph.pyrename to examples/third_party/pymde/complete_graph.pydiff --git a/examples/embeddings/debugging_embeddings.py b/examples/third_party/pymde/debugging_embeddings.pysimilarity index 100%rename from examples/embeddings/debugging_embeddings.pyrename to examples/third_party/pymde/debugging_embeddings.pydiff --git a/examples/embeddings/drawing_graphs.py b/examples/third_party/pymde/drawing_graphs.pysimilarity index 100%rename from examples/embeddings/drawing_graphs.pyrename to examples/third_party/pymde/drawing_graphs.pydiff --git a/examples/embeddings/embedding_numbers.py b/examples/third_party/pymde/embedding_numbers.pysimilarity index 100%rename from examples/embeddings/embedding_numbers.pyrename to examples/third_party/pymde/embedding_numbers.pydiff --git a/examples/embeddings/google_scholar.py b/examples/third_party/pymde/google_scholar.pysimilarity index 100%rename from examples/embeddings/google_scholar.pyrename to examples/third_party/pymde/google_scholar.pydiff --git a/examples/embeddings/interactive_cluster_analysis.py b/examples/third_party/pymde/interactive_cluster_analysis.pysimilarity index 100%rename from examples/embeddings/interactive_cluster_analysis.pyrename to examples/third_party/pymde/interactive_cluster_analysis.pydiff --git a/examples/embeddings/rotational_invariance.py b/examples/third_party/pymde/rotational_invariance.pysimilarity index 100%rename from examples/embeddings/rotational_invariance.pyrename to examples/third_party/pymde/rotational_invariance.pydiff --git a/examples/embeddings/tree.py b/examples/third_party/pymde/tree.pysimilarity index 100%rename from examples/embeddings/tree.pyrename to examples/third_party/pymde/tree.pydiff --git a/examples/embeddings/us_counties.py b/examples/third_party/pymde/us_counties.pysimilarity index 100%rename from examples/embeddings/us_counties.pyrename to examples/third_party/pymde/us_counties.py