This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tour-with-TensorFlow-and-Keras.py
191 lines (136 loc) · 5.5 KB
/
Tour-with-TensorFlow-and-Keras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Tour with TensorFlow and Keras
# Install dependencies
# Basic Tour
# Step 1: Import Neptune and TensorFlow
import neptune
import tensorflow as tf
# Step 2: Select Neptune project
neptune.init('shared/tour-with-tf-keras',
api_token='ANONYMOUS')
# Step 3: Create Neptune experiment
neptune.create_experiment(name='tf-keras-training-basic')
# Step 4: Prepare dataset and model
# dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(10, activation='softmax')
])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.05)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Step 5: Use NeptuneMonitor callback to log metrics during training
from neptunecontrib.monitoring.keras import NeptuneMonitor
model.fit(x_train, y_train,
epochs=10,
validation_split=0.2,
callbacks=[NeptuneMonitor()])
# Step 6: Log model evaluation metrics
eval_metrics = model.evaluate(x_test, y_test, verbose=0)
for j, metric in enumerate(eval_metrics):
neptune.log_metric('test_{}'.format(model.metrics_names[j]), metric)
# Step 7: Stop experiment at the end
neptune.stop()
# More logging options
# Install additional dependencies
# Select Neptune project
neptune.init('shared/tour-with-tf-keras',
api_token='ANONYMOUS')
# Prepare params
parameters = {'dense_units': 32,
'activation': 'relu',
'dropout': 0.3,
'learning_rate': 0.05,
'batch_size': 32,
'n_epochs': 10}
# Create Neptune experiment and log parameters
neptune.create_experiment(name='tf-keras-training-advanced',
tags=['keras', 'fashion-mnist'],
params=parameters)
# Prepare dataset and log data version
import hashlib
# prepare dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# log data version
neptune.set_property('x_train_version', hashlib.md5(x_train).hexdigest())
neptune.set_property('y_train_version', hashlib.md5(y_train).hexdigest())
neptune.set_property('x_test_version', hashlib.md5(x_test).hexdigest())
neptune.set_property('y_test_version', hashlib.md5(y_test).hexdigest())
neptune.set_property('class_names', class_names)
# Prepare model and log model architecture summary
# prepare model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(parameters['dense_units'], activation=parameters['activation']),
tf.keras.layers.Dropout(parameters['dropout']),
tf.keras.layers.Dense(parameters['dense_units'], activation=parameters['activation']),
tf.keras.layers.Dropout(parameters['dropout']),
tf.keras.layers.Dense(10, activation='softmax')
])
optimizer = tf.keras.optimizers.SGD(learning_rate=parameters['learning_rate'])
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# log model summary
model.summary(print_fn=lambda x: neptune.log_text('model_summary', x))
# Use NeptuneMonitor callback to log metrics during training
model.fit(x_train, y_train,
batch_size=parameters['batch_size'],
epochs=parameters['n_epochs'],
validation_split=0.2,
callbacks=[NeptuneMonitor()])
# Log model evaluation metrics
eval_metrics = model.evaluate(x_test, y_test, verbose=0)
for j, metric in enumerate(eval_metrics):
neptune.log_metric('test_{}'.format(model.metrics_names[j]), metric)
# Log model weights after training
model.save('model')
neptune.log_artifact('model')
# Log predictions as table
import numpy as np
import pandas as pd
from neptunecontrib.api import log_table
y_pred_proba = model.predict(x_test)
y_pred = np.argmax(y_pred_proba, axis=1)
y_pred = y_pred
df = pd.DataFrame(data={'y_test': y_test, 'y_pred': y_pred, 'y_pred_probability': y_pred_proba.max(axis=1)})
log_table('predictions', df)
# Log model performance visualizations
import matplotlib.pyplot as plt
from scikitplot.metrics import plot_roc, plot_precision_recall
fig, ax = plt.subplots()
plot_roc(y_test, y_pred_proba, ax=ax)
neptune.log_image('model-performance-visualizations', fig, image_name='ROC')
fig, ax = plt.subplots()
plot_precision_recall(y_test, y_pred_proba, ax=ax)
neptune.log_image('model-performance-visualizations', fig, image_name='precision recall')
plt.close('all')
# Log train data sample (images per class)
for j, class_name in enumerate(class_names):
plt.figure(figsize=(10, 10))
label_ = np.where(y_train == j)
for i in range(9):
plt.subplot(3, 3, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[label_[0][i]], cmap=plt.cm.binary)
plt.xlabel(class_names[j])
neptune.log_image('train data sample', plt.gcf())
plt.close('all')
# Stop experiment at the end
neptune.stop()