示例#1
0
Note that the same result can also be achieved via a Lambda layer.

Because our custom layer is written with primitives from the Keras
backend (`K`), our code can run both on TensorFlow and Theano.
'''

from __future__ import print_function
import keras
from keras.models import Sequential
from keras import layers
from keras.datasets import mnist
from keras import backend as K

import missinglink

missinglink_callback = missinglink.KerasCallback(
    owner_id="your-owner-id", project_token="your-project-token")
missinglink_callback.set_properties("Antirectifier")


class Antirectifier(layers.Layer):
    '''This is the combination of a sample-wise
    L2 normalization with the concatenation of the
    positive part of the input with the negative part
    of the input. The result is a tensor of samples that are
    twice as large as the input samples.

    It can be used in place of a ReLU.

    # Input shape
        2D tensor of shape (samples, n)
示例#2
0
              metrics=[
                  'accuracy', 'categorical_accuracy', 'mean_squared_error',
                  'hinge'
              ])

# Provide an alternative to provide MissingLinkAI credential
parser = argparse.ArgumentParser()
parser.add_argument('--owner-id')
parser.add_argument('--project-token')

# Override credential values if provided as arguments
args = parser.parse_args()
OWNER_ID = args.owner_id or OWNER_ID
PROJECT_TOKEN = args.project_token or PROJECT_TOKEN

missinglink_callback = missinglink.KerasCallback(owner_id=OWNER_ID,
                                                 project_token=PROJECT_TOKEN)

missinglink_callback.set_properties(
    display_name='Keras convolutional neural network',
    description='Two dimensional convolutional neural network')

model.fit(x_train,
          y_train,
          batch_size=BATCH_SIZE,
          nb_epoch=EPOCHS,
          validation_split=VALIDATION_SPLIT,
          callbacks=[missinglink_callback])

with missinglink_callback.test(model):
    score = model.evaluate(x_test, y_test)
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import missinglink

missinglink_callback = missinglink.KerasCallback()

batch_size = 12
num_classes = 10
epochs = 100

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
示例#4
0
    # if using a 5 layer net of pool_size = 2
    x_train = np.pad(x_train, [[0, 0], [0, 0], [2, 2], [2, 2]],
                     mode='constant')
    x_test = np.pad(x_test, [[0, 0], [0, 0], [2, 2], [2, 2]], mode='constant')
    nlayers = 5
elif pool_size == 3:
    # if using a 3 layer net of pool_size = 3
    x_train = x_train[:, :, :-1, :-1]
    x_test = x_test[:, :, :-1, :-1]
    nlayers = 3
else:
    import sys
    sys.exit('Script supports pool_size of 2 and 3.')

missinglink_callback = missinglink.KerasCallback(
    owner_id="485aee1a-7f13-0dab-c470-0be21d273407",
    project_token="AojfcjXvLGftJDdc")
missinglink_callback.set_properties("mnist_swwae")

# Shape of input to train on (note that model is fully convolutional however)
input_shape = x_train.shape[1:]
# The final list of the size of axis=1 for all layers, including input
nfeats_all = [input_shape[0]] + nfeats

# First build the encoder, all the while keeping track of the 'where' masks
img_input = Input(shape=input_shape)

# We push the 'where' masks to the following list
wheres = [None] * nlayers
y = img_input
for i in range(nlayers):
示例#5
0
import missinglink
from keras.applications import vgg16

model = vgg16.VGG16()

missinglink_callback = missinglink.KerasCallback(
    owner_id='replace with owner id',
    project_token='replace with project token')

path = 'http://l7.alamy.com/zooms' + \
    '/b76d255dd51e493e8c0fd5d5aa85f96f/lumbermill-cp93p7.jpg'

missinglink_callback.visual_back_prop(path, model)
示例#6
0
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=[
                  'accuracy', 'categorical_accuracy', 'mean_squared_error',
                  'hinge', precision, recall, f1
              ])

callback = missinglink.KerasCallback(owner_id=args.owner_id,
                                     project_token=args.project_token,
                                     host=args.host)

callback.set_properties(display_name='KerasMnistTest',
                        description='cool kerassing around')

if args.is_sampling:
    callback.set_hyperparams(
        sampling_factor=1 - random_sampling_factor
    )  # we log how many samples in % we have from the total samples

callback.set_hyperparams(train_sample_count=X_train.shape[0])
callback.set_hyperparams(test_sample_count=X_test.shape[0])
callback.set_hyperparams(total_epochs=epochs)

model.fit(X_train,
示例#7
0
parser = argparse.ArgumentParser()
parser.add_argument('--owner-id')
parser.add_argument('--project-token')

# Override credential values if provided as arguments
args = parser.parse_args()
OWNER_ID = args.owner_id or OWNER_ID
PROJECT_TOKEN = args.project_token or PROJECT_TOKEN


def stopped_callback():
    print('Experiment stopped from the web')


missinglink_callback = missinglink.KerasCallback(
    owner_id=OWNER_ID,
    project_token=PROJECT_TOKEN,
    host='https://missinglink-staging.appspot.com')

missinglink_callback.set_properties(
    display_name='Keras convolutional neural network',
    description='Two dimensional convolutional neural network')

model.fit(x_train,
          y_train,
          batch_size=BATCH_SIZE,
          nb_epoch=EPOCHS,
          validation_split=VALIDATION_SPLIT,
          callbacks=[missinglink_callback])

with missinglink_callback.test(model):
    score = model.evaluate(x_test, y_test)
示例#8
0
# Horovod: add Horovod Distributed Optimizer.
opt = hvd.DistributedOptimizer(opt)

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=opt,
              metrics=['accuracy'])

callbacks = [
    # Horovod: broadcast initial variable states from rank 0 to all other processes.
    # This is necessary to ensure consistent initialization of all workers when
    # training is started with random weights or restored from a checkpoint.
    hvd.callbacks.BroadcastGlobalVariablesCallback(0),
]

# Horovod: save checkpoints only on worker 0 to prevent other workers from corrupting them.
if hvd.rank() == 0:
    callbacks.append(keras.callbacks.ModelCheckpoint('./checkpoint-{epoch}.h5'))
    missinglink_callback = missinglink.KerasCallback(project='5740395606573056')
    callbacks.append(missinglink_callback)

model.fit(x_train, y_train,
          batch_size=batch_size,
          callbacks=callbacks,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# Hyper paramaters
EPOCHS = int(os.environ.get("EPOCHS", "4"))
MODEL = os.environ.get("MODEL", "simple")
SIMPLE_LAYER_DIMENSIONALITY = int(
    os.environ.get("SIMPLE_LAYER_DIMENSIONALITY", "64"))
SIMPLE_LAYER_COUNT = int(os.environ.get("SIMPLE_LAYER_COUNT", "0"))
BATCH_SIZE = int(os.environ.get("BATCH_SIZE", "10"))
OPTIMIZER = os.environ.get("OPTIMIZER", "sgd")
LEARNING_RATE = float(os.environ.get("LEARNING_RATE", "0.01"))
CLASS_COUNT = int(os.environ.get("CLASS_COUNT", "11"))
QUERY = os.environ.get("QUERY",
                       '@seed:1337 @split:0.1:0.2:0.7 @sample:0.2 yummy:True')
DATA_VOLUME_ID = int(os.environ.get("DATA_VOLUME_ID", "5652757822308352"))

missinglink_callback = missinglink.KerasCallback(project='5715594536026112')

missinglink_callback.set_properties(display_name=EXPERIMENT_NAME,
                                    description=EXPERIMENT_NOTE)

missinglink_callback.set_hyperparams(
    MODEL=MODEL,
    SIMPLE_LAYER_DIMENSIONALITY=SIMPLE_LAYER_DIMENSIONALITY,
    SIMPLE_LAYER_COUNT=SIMPLE_LAYER_COUNT,
    BATCH_SIZE=BATCH_SIZE,
    CLASS_COUNT=CLASS_COUNT,
)
callbacks = [missinglink_callback]

# Note Adam defaults lr to 0.001
#       SGD defaults lr to 0.01
示例#10
0
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import missinglink
import logging

missinglink_callback = missinglink.KerasCallback(
    owner_id='95444180-d97b-4d3d-83b1-a5ca63d426a1',
    project_token='EDeqiZjcSfNeKuxa',
    host='https://missinglink-staging.appspot.com')

batch_size = 128
num_classes = 10
epochs = 12

missinglink_callback.set_hyperparams(epochs=epochs)

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':