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
/
Neptune-TensorFlow-Keras.py
103 lines (66 loc) · 2.39 KB
/
Neptune-TensorFlow-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
# Neptune + TensorFlow / Keras
# Before we start
## Import libraries
import tensorflow as tf
## Define your model, data loaders and optimizer
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256, activation=tf.keras.activations.relu),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax)
])
optimizer = tf.keras.optimizers.SGD(lr=0.005, momentum=0.4,)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
## Initialize Neptune
import neptune
neptune.init(api_token='ANONYMOUS', project_qualified_name='shared/tensorflow-keras-integration')
# Quickstart
## Step 1: Create an Experiment
neptune.create_experiment('tensorflow-keras-quickstart')
## Step 2: Add NeptuneMonitor Callback to model.fit()
from neptunecontrib.monitoring.keras import NeptuneMonitor
model.fit(x_train, y_train,
epochs=5,
batch_size=64,
callbacks=[NeptuneMonitor()])
## Step 3: Explore results in the Neptune UI
## Step 4: Stop logging
neptune.stop()
# More Options
## Log hardware consumption
## Log hyperparameters
PARAMS = {'lr':0.005,
'momentum':0.9,
'epochs':10,
'batch_size':32}
optimizer = tf.keras.optimizers.SGD(lr=PARAMS['lr'], momentum=PARAMS['momentum'])
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# log params
neptune.create_experiment('tensorflow-keras-advanced', params=PARAMS)
model.fit(x_train, y_train,
epochs=PARAMS['epochs'],
batch_size=PARAMS['batch_size'],
callbacks=[NeptuneMonitor()])
## Log image predictions
x_test_sample = x_test[:100]
y_test_sample_pred = model.predict(x_test_sample)
for image, y_pred in zip(x_test_sample, y_test_sample_pred):
description = '\n'.join(['class {}: {}'.format(i, pred)
for i, pred in enumerate(y_pred)])
neptune.log_image('predictions',
image,
description=description)
## Log model weights
model.save('my_model')
# log model
neptune.log_artifact('my_model')
# Explore results in the Neptune UI
## Stop logging
neptune.stop()