Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Callbacks neptune #491

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/neptunelogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Example application with a dummy model using callback-based Neptune integration.
"""
import numpy as np

from keras_contrib import callbacks
from keras.models import Sequential
from keras.layers import Dense


# Replace below with your own credentials
# If NEPTUNE_API_TOKEN is None, read from environment variable
# otherwise you should provide it as a str
PROJECT_QUALIFIED_NAME = "your_username/your_project"
EXPERIMENT_NAME = "test-neptunelogger"
NEPTUNE_API_TOKEN = None


def build_model():
"""Build a dummy binary classification model model.

Returns:
Keras.models.Model: The dummy model.
"""
model = Sequential([
Dense(2, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])
return model


def test_NeptuneLogger():
"""Test the NeptuneLogger callback with a dummy model and dataset.
"""
X = np.random.rand(100, 2)
y = np.random.rand(100).reshape(-1, 1)

model = build_model()
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.fit(
X, y,
batch_size=1,
epochs=1,
verbose=0,
callbacks=[
callbacks.NeptuneLogger(
project_qualified_name=PROJECT_QUALIFIED_NAME,
experiment_name=EXPERIMENT_NAME,
api_token=NEPTUNE_API_TOKEN
)
])

if __name__ == '__main__':
test_NeptuneLogger()
1 change: 1 addition & 0 deletions keras_contrib/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .dead_relu_detector import DeadReluDetector
from .cyclical_learning_rate import CyclicLR
from .tensorboard import TensorBoardGrouped
from .neptune import NeptuneLogger
64 changes: 64 additions & 0 deletions keras_contrib/callbacks/neptune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Neptune integration for Keras via callback.
"""
from __future__ import absolute_import
from __future__ import print_function

from keras.callbacks import Callback

import neptune


class NeptuneLogger(Callback):
"""
Neptune integration with keras callbacks.
"""

def __init__(
self,
project_qualified_name, experiment_name, api_token=None, **kwargs):
"""
Construct the NeptuneLogger callback and log in to neptune.
Neptune experiment creation is delayed until the beginning of training.

Args:
project_qualified_name (str): The username and project name with
which to log in.
experiment_name (str): The name to give to the new experiment.
api_token (str, optional): The Neptune API token to use as
credentials. By default this is read from the environment
variable NEPTUNE_API_TOKEN.
**kwargs:
For full list see neptune.projects.Project.create_experiment.
Some useful keyword names include: description (str),
params (dict), properties (dict), tags (list of str),
upload_source_files (list of str paths).
Note that the value of this can change between instatiation
and on_train_begin.

"""
self.experiment_name = experiment_name
self.experiment_kwargs = kwargs
self.project_qualified_name = project_qualified_name
self.experiment = None

self.session = neptune.sessions.Session(api_token=api_token)
self.project = self.session.get_project(project_qualified_name)

def __del__(self):
if self.experiment:
self.experiment.stop()

def on_train_begin(self, logs):
if self.experiment:
return
self.experiment = self.project.create_experiment(
self.experiment_name, **self.experiment_kwargs)

def on_epoch_end(self, epoch, logs=None):
if not logs:
return
for key, value in logs.items():
try:
self.experiment.send_metric(key, epoch, float(value))
except ValueError:
pass # Ignore non numeric values