Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_tornasole_hook(out_dir, train_data=None, validation_data=None, frequency=1):
save_config = SaveConfig(save_interval=frequency)
hook = Hook(
out_dir=out_dir,
save_config=save_config,
train_data=train_data,
validation_data=validation_data,
)
return hook
def helper_test_modes(hook=None, out_dir="/tmp/test_output/test_hook_modes/"):
prefix = str(uuid.uuid4())
device = torch.device("cpu")
save_steps = [i for i in range(5)]
model = Net(to_save=save_steps).to(device)
json = hook is not None
if hook is None:
out_dir = str(Path(out_dir, prefix))
hook = Hook(
out_dir=out_dir,
save_config=SaveConfig({modes.TRAIN: SaveConfigMode(save_steps=save_steps)}),
include_collections=[
CollectionKeys.WEIGHTS,
CollectionKeys.BIASES,
CollectionKeys.GRADIENTS,
CollectionKeys.DEFAULT,
CollectionKeys.LOSSES,
],
)
hook.register_module(model)
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
hook.set_mode(mode=modes.TRAIN)
train(model, device, optimizer, num_steps=10, save_steps=save_steps)
trial = create_trial(path=out_dir, name="test output")
assert module is not None
# Create a hook that logs weights, biases, gradients and inputs/outputs of model
hook = Hook(
out_dir=output_dir,
save_config=SaveConfig(save_steps=save_steps),
include_collections=[
CollectionKeys.WEIGHTS,
CollectionKeys.GRADIENTS,
CollectionKeys.BIASES,
"l_mod",
],
)
hook.get_collection("l_mod").add_module_tensors(module, inputs=True, outputs=True)
elif hook_type == "weights-bias-gradients":
save_config = SaveConfig(save_steps=save_steps)
# Create a hook that logs ONLY weights, biases, and gradients
hook = Hook(
out_dir=output_dir,
save_config=save_config,
include_collections=[
CollectionKeys.WEIGHTS,
CollectionKeys.BIASES,
CollectionKeys.GRADIENTS,
CollectionKeys.DEFAULT,
CollectionKeys.LOSSES,
],
)
return hook
def test_loss_collection_default():
save_config = SaveConfig(save_steps=[0, 1, 2, 3])
run_id = "trial_" + datetime.now().strftime("%Y%m%d-%H%M%S%f")
out_dir = "newlogsRunTest/" + run_id
hook = t_hook(out_dir=out_dir, save_config=save_config)
assert has_training_ended(out_dir) == False
run_mnist_gluon_model(
hook=hook, num_steps_train=10, num_steps_eval=10, register_to_loss_block=True
)
print("Created the trial with out_dir {0}".format(out_dir))
tr = create_trial(out_dir)
assert tr
assert len(tr.steps()) == 4
print(tr.tensor_names())
tname = tr.tensor_names(regex=".*loss")[0]
loss_tensor = tr.tensor(tname)
def test_lstm_and_generator(out_dir):
# init hook
hook = KerasHook(
out_dir,
include_collections=[
CollectionKeys.WEIGHTS,
CollectionKeys.LOSSES,
CollectionKeys.GRADIENTS,
],
save_config=SaveConfig(save_steps=[0, 1, 2, 3]),
)
# init model
num_steps = 100
hidden_size = 100
vocabulary = 1000
model = Sequential()
model.add(Embedding(vocabulary, hidden_size, input_length=num_steps))
model.add(LSTM(hidden_size, return_sequences=True))
model.add(LSTM(hidden_size, return_sequences=True))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(vocabulary)))
model.add(Activation("softmax"))
model.compile(
loss="categorical_crossentropy",
def create_hook(output_dir, module=None, hook_type="saveall", save_steps=None):
# Create a hook that logs weights, biases, gradients and inputs/ouputs of model
if hook_type == "saveall":
hook = Hook(
out_dir=output_dir, save_config=SaveConfig(save_steps=save_steps), save_all=True
)
elif hook_type == "module-input-output":
# The names of input and output tensors of a module are in following format
# Inputs : _input_, and
# Output : _output
# In order to log the inputs and output of a module, we will create a collection as follows:
assert module is not None
# Create a hook that logs weights, biases, gradients and inputs/outputs of model
hook = Hook(
out_dir=output_dir,
save_config=SaveConfig(save_steps=save_steps),
include_collections=[
CollectionKeys.WEIGHTS,
CollectionKeys.GRADIENTS,
CollectionKeys.BIASES,
def create_hook(output_uri):
# With the following SaveConfig, we will save tensors for steps 1, 2 and 3
# (indexing starts with 0).
save_config = SaveConfig(save_interval=1)
# Create a hook that logs weights, biases and gradients while training the model.
hook = Hook(
out_dir=output_uri,
save_config=save_config,
include_collections=["weights", "gradients", "biases"],
)
return hook
def create_hook(out_dir, train_data=None, validation_data=None, frequency=1):
save_config = SaveConfig(save_interval=frequency)
hook = Hook(
out_dir=out_dir,
save_config=save_config,
train_data=train_data,
validation_data=validation_data,
)
return hook