Skip to content

Commit c7cbeae

Browse files
notebooks for oreilly class
1 parent 5d8c387 commit c7cbeae

File tree

4 files changed

+325
-0
lines changed

4 files changed

+325
-0
lines changed

examples/keras-cifar/cifar-cnn.ipynb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import tensorflow as tf\n",
10+
"import wandb\n",
11+
"\n",
12+
"# Set Hyper-parameters\n",
13+
"wandb.init()\n",
14+
"config = wandb.config\n",
15+
"config.batch_size = 128\n",
16+
"config.epochs = 10\n",
17+
"config.learn_rate = 0.001\n",
18+
"config.dropout = 0.3\n",
19+
"config.dense_layer_nodes = 128\n",
20+
"\n",
21+
"# Load data\n",
22+
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n",
23+
" 'dog', 'frog', 'horse', 'ship', 'truck']\n",
24+
"num_classes = len(class_names)\n",
25+
"\n",
26+
"(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()\n",
27+
"X_train = X_train.astype('float32') / 255.\n",
28+
"X_test = X_test.astype('float32') / 255.\n",
29+
"\n",
30+
"# Convert class vectors to binary class matrices.\n",
31+
"y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n",
32+
"y_test = tf.keras.utils.to_categorical(y_test, num_classes)\n",
33+
"\n",
34+
"# Define model\n",
35+
"model = tf.keras.models.Sequential()\n",
36+
"model.add(tf.keras.layers.Conv2D(32, (3, 3), padding='same',\n",
37+
" input_shape=X_train.shape[1:], activation='relu'))\n",
38+
"model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n",
39+
"model.add(tf.keras.layers.Dropout(config.dropout))\n",
40+
"\n",
41+
"model.add(tf.keras.layers.Flatten())\n",
42+
"model.add(tf.keras.layers.Dense(config.dense_layer_nodes, activation='relu'))\n",
43+
"model.add(tf.keras.layers.Dropout(config.dropout))\n",
44+
"model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))\n",
45+
"model.compile(loss='categorical_crossentropy',\n",
46+
" optimizer=tf.keras.optimizers.Adam(config.learn_rate),\n",
47+
" metrics=['accuracy'])\n",
48+
"# log the number of total parameters\n",
49+
"config.total_params = model.count_params()\n",
50+
"print(\"Total params: \", config.total_params)\n",
51+
"\n",
52+
"model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test),\n",
53+
" callbacks=[wandb.keras.WandbCallback(data_type=\"image\", labels=class_names, save_model=False)])"
54+
]
55+
}
56+
],
57+
"metadata": {
58+
"kernelspec": {
59+
"display_name": "Python 3",
60+
"language": "python",
61+
"name": "python3"
62+
},
63+
"language_info": {
64+
"codemirror_mode": {
65+
"name": "ipython",
66+
"version": 3
67+
},
68+
"file_extension": ".py",
69+
"mimetype": "text/x-python",
70+
"name": "python",
71+
"nbconvert_exporter": "python",
72+
"pygments_lexer": "ipython3",
73+
"version": "3.7.3"
74+
}
75+
},
76+
"nbformat": 4,
77+
"nbformat_minor": 2
78+
}

examples/keras-cifar/cifar-gen.ipynb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"scrolled": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"from tensorflow.keras.callbacks import TensorBoard\n",
12+
"from tensorflow.keras.datasets import cifar10\n",
13+
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
14+
"\n",
15+
"import numpy as np\n",
16+
"import os\n",
17+
"import wandb\n",
18+
"from wandb.keras import WandbCallback\n",
19+
"import tensorflow as tf\n",
20+
"\n",
21+
"run = wandb.init()\n",
22+
"config = run.config\n",
23+
"config.dropout = 0.25\n",
24+
"config.dense_layer_nodes = 100\n",
25+
"config.learn_rate = 0.08\n",
26+
"config.batch_size = 128\n",
27+
"config.epochs = 10\n",
28+
"\n",
29+
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n",
30+
" 'dog', 'frog', 'horse', 'ship', 'truck']\n",
31+
"num_classes = len(class_names)\n",
32+
"\n",
33+
"(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n",
34+
"\n",
35+
"# Convert class vectors to binary class matrices.\n",
36+
"y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n",
37+
"y_test = tf.keras.utils.to_categorical(y_test, num_classes)\n",
38+
"\n",
39+
"model = tf.keras.models.Sequential()\n",
40+
"model.add(tf.keras.layers.Conv2D(32, (3, 3), padding='same',\n",
41+
" input_shape=X_train.shape[1:], activation='relu'))\n",
42+
"model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n",
43+
"model.add(tf.keras.layers.Dropout(config.dropout))\n",
44+
"\n",
45+
"model.add(tf.keras.layers.Flatten())\n",
46+
"model.add(tf.keras.layers.Dense(config.dense_layer_nodes, activation='relu'))\n",
47+
"model.add(tf.keras.layers.Dropout(config.dropout))\n",
48+
"model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))\n",
49+
"\n",
50+
"model.compile(loss='categorical_crossentropy',\n",
51+
" optimizer=\"adam\",\n",
52+
" metrics=['accuracy'])\n",
53+
"# log the number of total parameters\n",
54+
"config.total_params = model.count_params()\n",
55+
"print(\"Total params: \", config.total_params)\n",
56+
"X_train = X_train.astype('float32') / 255.\n",
57+
"X_test = X_test.astype('float32') / 255.\n",
58+
"\n",
59+
"datagen = ImageDataGenerator(width_shift_range=0.1)\n",
60+
"datagen.fit(X_train)\n",
61+
"\n",
62+
"\n",
63+
"# Fit the model on the batches generated by datagen.flow().\n",
64+
"model.fit_generator(datagen.flow(X_train, y_train,\n",
65+
" batch_size=config.batch_size),\n",
66+
" steps_per_epoch=X_train.shape[0] // config.batch_size,\n",
67+
" epochs=config.epochs,\n",
68+
" validation_data=(X_test, y_test),\n",
69+
" callbacks=[WandbCallback(data_type=\"image\", labels=class_names)])"
70+
]
71+
}
72+
],
73+
"metadata": {
74+
"kernelspec": {
75+
"display_name": "Python 3",
76+
"language": "python",
77+
"name": "python3"
78+
},
79+
"language_info": {
80+
"codemirror_mode": {
81+
"name": "ipython",
82+
"version": 3
83+
},
84+
"file_extension": ".py",
85+
"mimetype": "text/x-python",
86+
"name": "python",
87+
"nbconvert_exporter": "python",
88+
"pygments_lexer": "ipython3",
89+
"version": "3.7.3"
90+
}
91+
},
92+
"nbformat": 4,
93+
"nbformat_minor": 2
94+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"scrolled": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import tensorflow as tf\n",
12+
"from tensorflow.keras.optimizers import Adam\n",
13+
"# Importing the ResNet50 model\n",
14+
"from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\n",
15+
"import numpy as np\n",
16+
"import os\n",
17+
"import wandb\n",
18+
"from wandb.keras import WandbCallback\n",
19+
"\n",
20+
"# Set hyper parameters\n",
21+
"wandb.init()\n",
22+
"config = wandb.config\n",
23+
"config.batch_size = 128\n",
24+
"config.epochs = 10\n",
25+
"config.learn_rate = 0.001\n",
26+
"config.dropout = 0.3\n",
27+
"config.dense_layer_nodes = 128\n",
28+
"\n",
29+
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n",
30+
" 'dog', 'frog', 'horse', 'ship', 'truck']\n",
31+
"num_classes = len(class_names)\n",
32+
"\n",
33+
"# Load and normalize data\n",
34+
"(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()\n",
35+
"X_train = X_train.astype('float32') / 255.\n",
36+
"X_test = X_test.astype('float32') / 255.\n",
37+
"\n",
38+
"# Convert class vectors to binary class matrices.\n",
39+
"y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n",
40+
"y_test = tf.keras.utils.to_categorical(y_test, num_classes)\n",
41+
"\n",
42+
"# Loading the ResNet50 model with pre-trained ImageNet weights\n",
43+
"big_model = ResNet50(weights='imagenet', include_top=False,\n",
44+
" input_shape=(X_train.shape[1], X_train.shape[2], 3))\n",
45+
"\n",
46+
"# Add new last layer\n",
47+
"model = tf.keras.models.Sequential()\n",
48+
"model.add(big_model)\n",
49+
"model.add(tf.keras.layers.Flatten())\n",
50+
"model.add(tf.keras.layers.Dense(10, activation='softmax'))\n",
51+
"\n",
52+
"# Make the big first layer frozen for speed\n",
53+
"model.layers[0].trainable = False\n",
54+
"\n",
55+
"model.compile(loss='categorical_crossentropy',\n",
56+
" optimizer=Adam(config.learn_rate),\n",
57+
" metrics=['accuracy'])\n",
58+
"# log the number of total parameters\n",
59+
"config.total_params = model.count_params()\n",
60+
"print(\"Total params: \", config.total_params)\n",
61+
"model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test),\n",
62+
" callbacks=[WandbCallback(data_type=\"image\", labels=class_names, save_model=False)])"
63+
]
64+
}
65+
],
66+
"metadata": {
67+
"kernelspec": {
68+
"display_name": "Python 3",
69+
"language": "python",
70+
"name": "python3"
71+
},
72+
"language_info": {
73+
"codemirror_mode": {
74+
"name": "ipython",
75+
"version": 3
76+
},
77+
"file_extension": ".py",
78+
"mimetype": "text/x-python",
79+
"name": "python",
80+
"nbconvert_exporter": "python",
81+
"pygments_lexer": "ipython3",
82+
"version": "3.7.3"
83+
}
84+
},
85+
"nbformat": 4,
86+
"nbformat_minor": 2
87+
}

examples/keras-cifar/cifar.ipynb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"scrolled": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import tensorflow as tf\n",
12+
"import wandb\n",
13+
"\n",
14+
"wandb.init()\n",
15+
"\n",
16+
"config = wandb.config\n",
17+
"config.batch_size = 128\n",
18+
"config.epochs = 10\n",
19+
"config.learn_rate = 1000\n",
20+
"\n",
21+
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n",
22+
" 'dog', 'frog', 'horse', 'ship', 'truck']\n",
23+
"num_classes = len(class_names)\n",
24+
"\n",
25+
"(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()\n",
26+
"\n",
27+
"# Convert class vectors to binary class matrices.\n",
28+
"y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n",
29+
"y_test = tf.keras.utils.to_categorical(y_test, num_classes)\n",
30+
"\n",
31+
"model = tf.keras.models.Sequential()\n",
32+
"model.add(tf.keras.layers.Flatten(input_shape=X_train.shape[1:]))\n",
33+
"model.add(tf.keras.layers.Dense(num_classes))\n",
34+
"model.compile(loss='mse',\n",
35+
" optimizer=tf.keras.optimizers.Adam(config.learn_rate),\n",
36+
" metrics=['accuracy'])\n",
37+
"# log the number of total parameters\n",
38+
"config.total_params = model.count_params()\n",
39+
"print(\"Total params: \", config.total_params)\n",
40+
"model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test),\n",
41+
" callbacks=[wandb.keras.WandbCallback(data_type=\"image\", labels=class_names, save_model=False)])"
42+
]
43+
}
44+
],
45+
"metadata": {
46+
"kernelspec": {
47+
"display_name": "Python 3",
48+
"language": "python",
49+
"name": "python3"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 3
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython3",
61+
"version": "3.7.3"
62+
}
63+
},
64+
"nbformat": 4,
65+
"nbformat_minor": 2
66+
}

0 commit comments

Comments
 (0)