def test_update_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *' schedule_json = schedule() schedule_json['cron'] = '10 0 * * *' client.update_schedule('schedule', 'nightly', schedule_json) assert client.get_schedule('schedule', 'nightly')['cron'] == '10 0 * * *'
def test_update_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *' schedule_json = schedule() schedule_json['cron'] = '10 0 * * *' client.update_schedule('schedule', 'nightly', schedule_json) assert client.get_schedule('schedule', 'nightly')['cron'] == '10 0 * * *'
def test_disable_schedule(): """ Confirms that a schedule runs when enabled but then stops firing when the schedule is disabled. """ client = metronome.create_client() job_id = 'schedule-disabled-{}'.format(uuid.uuid4().hex) job_json = job_no_schedule(job_id) with job(job_json): # indent job_schedule = schedule() job_schedule['cron'] = '* * * * *' # every minute client.add_schedule(job_id, job_schedule) # sleep until we run time.sleep(timedelta(minutes=1.1).total_seconds()) runs = client.get_runs(job_id) run_count = len(runs) # there is a race condition where this could be 1 or 2 # both are ok... what matters is that after disabled, that there are # no more assert run_count > 0 # update enabled = False job_schedule['enabled'] = False client.update_schedule(job_id, 'nightly', job_schedule) # wait for the next run time.sleep(timedelta(minutes=1.5).total_seconds()) runs = client.get_runs(job_id) # make sure there are no more than the original count assert len(runs) == run_count
def test_disable_schedule(): """ Confirms that a schedule runs when enabled but then stops firing when the schedule is disabled. """ client = metronome.create_client() job_id = 'schedule-disabled-{}'.format(uuid.uuid4().hex) job_json = job_no_schedule(job_id) with job(job_json): # indent job_schedule = schedule() job_schedule['cron'] = '* * * * *' # every minute client.add_schedule(job_id, job_schedule) # sleep until we run time.sleep(timedelta(minutes=1.1).total_seconds()) runs = client.get_runs(job_id) run_count = len(runs) # there is a race condition where this could be 1 or 2 # both are ok... what matters is that after disabled, that there are # no more assert run_count > 0 # update enabled = False job_schedule['enabled'] = False client.update_schedule(job_id, 'nightly', job_schedule) # wait for the next run time.sleep(timedelta(minutes=1.5).total_seconds()) runs = client.get_runs(job_id) # make sure there are no more than the original count assert len(runs) == run_count
def actor_loss(self, seq, target): # Actions: 0 [a1] [a2] a3 # ^ | ^ | ^ | # / v / v / v # States: [z0]->[z1]-> z2 -> z3 # Targets: t0 [t1] [t2] # Baselines: [v0] [v1] v2 v3 # Entropies: [e1] [e2] # Weights: [ 1] [w1] w2 w3 # Loss: l1 l2 metrics = {} # Two states are lost at the end of the trajectory, one for the boostrap # value prediction and one because the corresponding action does not lead # anywhere anymore. One target is lost at the start of the trajectory # because the initial state comes from the replay buffer. policy = self.actor(tf.stop_gradient(seq['feat'][:-2])) if self.config.actor_grad == 'dynamics': objective = target[1:] elif self.config.actor_grad == 'reinforce': baseline = self._target_critic(seq['feat'][:-2]).mode() advantage = tf.stop_gradient(target[1:] - baseline) action = tf.stop_gradient(seq['action'][1:-1]) objective = policy.log_prob(action) * advantage elif self.config.actor_grad == 'both': baseline = self._target_critic(seq['feat'][:-2]).mode() advantage = tf.stop_gradient(target[1:] - baseline) objective = policy.log_prob(seq['action'][1:-1]) * advantage mix = common.schedule(self.config.actor_grad_mix, self.tfstep) objective = mix * target[1:] + (1 - mix) * objective metrics['actor_grad_mix'] = mix else: raise NotImplementedError(self.config.actor_grad) ent = policy.entropy() ent_scale = common.schedule(self.config.actor_ent, self.tfstep) objective += ent_scale * ent weight = tf.stop_gradient(seq['weight']) actor_loss = -(weight[:-2] * objective).mean() metrics['actor_ent'] = ent.mean() metrics['actor_ent_scale'] = ent_scale return actor_loss, metrics
def test_remove_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *' client.remove_schedule('schedule', 'nightly') schedule_exists = False try: client.get_schedule('schedule', 'nightly') schedule_exists = True except: pass assert not schedule_exists, "Schedule exists"
def test_remove_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *' client.remove_schedule('schedule', 'nightly') schedule_exists = False try: client.get_schedule('schedule', 'nightly') schedule_exists = True except Exception: pass assert not schedule_exists, "Schedule exists"
def test_add_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *'
def test_add_schedule(): client = metronome.create_client() with job(job_no_schedule('schedule')): client.add_schedule('schedule', schedule()) assert client.get_schedule('schedule', 'nightly')['cron'] == '20 0 * * *'
import malwaresnet import math from keras.callbacks import LearningRateScheduler model_malwaresnet = malwaresnet.create_model(input_shape=(file_chunks, file_chunk_size), byte_embedding_size=2) train_generator = common.generator(list(zip(sha256_train, y_train)), batch_size, file_chunks, file_chunk_size) test_generator = common.generator(list(zip(sha256_test, y_test)), 1, file_chunks, file_chunk_size) model_malwaresnet.fit_generator( train_generator, steps_per_epoch=math.ceil(len(sha256_train) / batch_size), epochs=20, callbacks=[ LearningRateScheduler(lambda epoch: common.schedule( epoch, start=0.1, decay=0.5, every=1)) ], validation_data=test_generator, validation_steps=len(sha256_test)) y_pred = [] for sha256, lab in zip(sha256_test, y_test): y_pred.append( model_malwaresnet.predict_on_batch( np.asarray([get_file_data(sha256, lab)]).reshape( (-1, file_chunks, file_chunk_size)))) common.summarize_performance( np.asarray(y_pred).flatten(), y_test, "MalwaResnet") model_malwaresnet.save('malwaresnet.h5')
file_chunk_size = max_file_length // file_chunks batch_size = 8 import endtoend import math from keras.callbacks import LearningRateScheduler # create_model(input_shape, byte_embedding_size=2, input_dropout=0.2, kernel_size=16, n_filters_per_layer=[64,256,1024], n_mlp_layers=2 ) model_e2e = endtoend.create_model(input_shape=(file_chunks, file_chunk_size)) train_generator = common.generator(list(zip(sha256_train, y_train)), batch_size, file_chunks, file_chunk_size) test_generator = common.generator(list(zip(sha256_test, y_test)), 1, file_chunks, file_chunk_size) model_e2e.fit_generator(train_generator, steps_per_epoch=math.ceil(len(sha256_train) / batch_size), epochs=20, callbacks=[LearningRateScheduler( lambda epoch: common.schedule(epoch, start=0.1, decay=0.5, every=1) ) ], validation_data=test_generator, validation_steps=len(sha256_test)) y_pred = [] for sha256, lab in zip(sha256_test, y_test): y_pred.append( model_e2e.predict_on_batch( np.asarray([get_file_data(sha256, lab)]).reshape( (-1, file_chunks, file_chunk_size)) ) ) common.summarize_performance(np.asarray( y_pred).flatten(), y_test, "End-to-end convnet") model_e2e.save('endtoend.h5')
scaler = StandardScaler().fit(X_train) ########### # sanity check: random forest classifier from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier rf = RandomForestClassifier( n_estimators=40, n_jobs=-1, max_depth=30).fit(X_train, y_train) y_pred = rf.predict_proba(X_test)[:,-1] # get probabiltiy of malicious (last class == last column ) common.summarize_performance(y_pred, y_test, "RF Classifier") from sklearn.externals import joblib joblib.dump(rf, 'random_forest.pkl') # simple multilayer perceptron X_train = scaler.transform(X_train) # scale for multilayer perceptron X_test = scaler.transform(X_test) import simple_multilayer from keras.callbacks import LearningRateScheduler model = simple_multilayer.create_model(input_shape=( X_train.shape[1], ), input_dropout=0.1, hidden_dropout=0.1, hidden_layers=[4096, 2048, 1024, 512]) model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, callbacks=[LearningRateScheduler( lambda epoch: common.schedule(0.2, 0.5, 5))], validation_data=(X_test, y_test)) y_pred = model.predict(X_test) common.summarize_performance(y_pred, y_test, "Multilayer perceptron") model.save('multilayer.h5')
y_pred = rf.predict_proba( X_test)[:, -1] # get probabiltiy of malicious (last class == last column ) common.summarize_performance(y_pred, y_test, "RF Classifier") from sklearn.externals import joblib joblib.dump(rf, 'random_forest.pkl') # simple multilayer perceptron X_train = scaler.transform(X_train) # scale for multilayer perceptron X_test = scaler.transform(X_test) import simple_multilayer from keras.callbacks import LearningRateScheduler model = simple_multilayer.create_model(input_shape=(X_train.shape[1], ), input_dropout=0.1, hidden_dropout=0.1, hidden_layers=[4096, 2048, 1024, 512]) model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, callbacks=[ LearningRateScheduler(lambda epoch: common.schedule(0.2, 0.5, 5)) ], validation_data=(X_test, y_test)) y_pred = model.predict(X_test) common.summarize_performance(y_pred, y_test, "Multilayer perceptron") model.save('multilayer.h5')