Weights and Biases
WandB Tutorials
# Install the library
!pip install wandb
# Login to your account
!wandb login
# Initialize wandb
import wandb
wandb.init(project="my-first-project")
# Setting Hyper-parameters
wandb.config.dropout = 0.2
wandb.config.hidden_layer_size = 128
# Log metrics - You can also log images,graphs,histograms
def my_train_loop():
for epoch in range(10):
loss = 0
wandb.log({'epoch':epoch,'loss':loss})
# Logging Images
wandb.log({"examples": [wandb.Image(numpy_array_or_pil, caption="Label")]})
# Saving Files
wandb.save("mymodel.h5")
# Save a model file from the current directory
wandb.save('model.h5')
# Save all files that currently exist containing the substring "ckpt"
wandb.save('../logs/*ckpt*')
# Save any files starting with "checkpoint" as they're written to
wandb.save(os.path.join(wandb.run.dir, "checkpoint*"))
import wandb
from wandb.keras import WandbCallback
wandb.init(config={"hyper": "parameter"})
# Magic
model.fit(X_train, y_train, validation_data=(X_test, y_test),
callbacks=[WandbCallback()])
# Visualize single plot
wandb.sklearn.plot_confusion_matrix(y_true, y_pred, labels)
# Visualize all classifier plots
wandb.sklearn.plot_classifier(clf, X_train, X_test, y_train, y_test, y_pred, y_probas, labels,
model_name='SVC', feature_names=None)
# All regression plots
wandb.sklearn.plot_regressor(reg, X_train, X_test, y_train, y_test, model_name='Ridge')
# All clustering plots
wandb.sklearn.plot_clusterer(kmeans, X_train, cluster_labels, labels=None, model_name='KMeans')
import wandb
from wandb.keras import WandbCallback
from keras.datasets import fashion_mnist
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from keras.utils import np_utils
from keras.optimizers import SGD
from keras.callbacks import TensorBoard
# Default values for hyper-parameters
defaults=dict(
dropout = 0.2,
hidden_layer_size = 32,
layer_1_size = 32,
learn_rate = 0.01,
decay = 1e-6,
momentum = 0.9,
epochs = 5,
)
# Initialize a new wandb run and pass in the config object
# wandb.init(anonymous='allow', project="kaggle", config=defaults)
wandb.init(project="visualize-models", config=defaults, name="neural_network")
config = wandb.config
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
labels=["T-shirt/top","Trouser","Pullover","Dress","Coat",
"Sandal","Shirt","Sneaker","Bag","Ankle boot"]
img_width=28
img_height=28
X_train = X_train.astype('float32')
X_train /= 255.
X_test = X_test.astype('float32')
X_test /= 255.
#reshape input data
X_train = X_train.reshape(X_train.shape[0], img_width, img_height, 1)[:10000]
X_test = X_test.reshape(X_test.shape[0], img_width, img_height, 1)[:10000]
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)[:10000]
y_test = np_utils.to_categorical(y_test)[:10000]
num_classes = y_test.shape[1]
# build model
model = Sequential()
model.add(Conv2D(config.layer_1_size, (5, 5), activation='relu',
input_shape=(img_width, img_height,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(config.dropout))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
sgd = SGD(lr=config.learn_rate, decay=config.decay, momentum=config.momentum, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
%%wandb
# Add WandbCallback() to the fit function
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=config.epochs,
callbacks=[WandbCallback(data_type="image", labels=labels)])
!wandb agent sharan19/visualize-models/q3e69h7k
sweep_config = {
"name": "My Sweep",
"method": "grid",
"parameters": {
"parameter1": {
"values": [1, 2, 3]
}
}
}
sweep_id = wandb.sweep(sweep_config)
wandb.agent(sweep_id, function=train)
For full example of how hyperparameter optimization with sweeps work, visit:
https://github.com/wandb/examples/blob/master/examples/wandb-sweeps/sweeps-python/notebook.ipynb
And
https://colab.research.google.com/drive/181GCGp36_75C2zm7WLxr9U2QjMXXoibt#scrollTo=xftEwKyuaJ5q