Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
all_trains = np.vstack( (all_trains, np.array(features[i])) )
train_lengths.append(features[i].shape[0])
scaler = StandardScaler().fit(all_trains)
X_train = [scaler.transform(features[i]) for i in xrange(len(features))]
cl.fit(X_train, labels, train_lengths, workers=self.params['hmm']['n_workers'])
out_filename = self.params['general']['scratch_directory'] + "/" + self.params['model_training']['output_model']
outfile = open(out_filename, "w")
dill.dump(cl, outfile, dill.HIGHEST_PROTOCOL )
outfile.close()
outfile_scaler = open('%s.scaler' % out_filename, "w")
dill.dump(scaler, outfile_scaler, dill.HIGHEST_PROTOCOL)
outfile_scaler.close()
n_jobs=self.params['svm_reg']['num_workers'])
scaler.fit(features)
features = scaler.transform(features)
estimator.fit(features, labels)
T1 = time.time()
print "model training took %f seconds" % (T1-T0)
print "best model score: %f" % (estimator.best_score_)
best_percentile = estimator.best_estimator_.named_steps['anova'].percentile
best_c = estimator.best_estimator_.named_steps['svm'].C
best_gamma = estimator.best_estimator_.named_steps['svm'].gamma
print "best params found for SVM: C = %.2ef, gamma = %.2ef" % (best_c, best_gamma)
print "best params found for ANOVA: percetile = %d" % (best_percentile)
print "saved best model to %s" % (out_filename)
outfile = open(out_filename, "w")
dill.dump(estimator.best_estimator_, outfile, dill.HIGHEST_PROTOCOL )
outfile_scaler = open('%s.scaler' % out_filename, "w")
dill.dump(scaler, outfile_scaler, dill.HIGHEST_PROTOCOL)
estimator.fit(features, labels)
T1 = time.time()
print "model training took %f seconds" % (T1-T0)
print "best model score: %f" % (estimator.best_score_)
best_percentile = estimator.best_estimator_.named_steps['anova'].percentile
best_c = estimator.best_estimator_.named_steps['svm'].C
best_gamma = estimator.best_estimator_.named_steps['svm'].gamma
print "best params found for SVM: C = %.2ef, gamma = %.2ef" % (best_c, best_gamma)
print "best params found for ANOVA: percetile = %d" % (best_percentile)
print "saved best model to %s" % (out_filename)
outfile = open(out_filename, "w")
dill.dump(estimator.best_estimator_, outfile, dill.HIGHEST_PROTOCOL )
outfile_scaler = open('%s.scaler' % out_filename, "w")
dill.dump(scaler, outfile_scaler, dill.HIGHEST_PROTOCOL)
def save_obj(obj, name ):
with open(name, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
n_iter = NN*3
# run the algorithm
constrcons_seq = constrcons.run(iterations=n_iter, verbose=True)
# print results
constrcons_x = constrcons.get_result()
print("Agent {}: {}".format(agent_id, constrcons_x.flatten())) # save information
if agent_id == 0:
with open('info.pkl', 'wb') as output:
pickle.dump({'N': NN, 'size': NN+dim+1, 'iterations': n_iter}, output, pickle.HIGHEST_PROTOCOL)
with open('objective_function.pkl', 'wb') as output:
pickle.dump(obj_func, output, pickle.HIGHEST_PROTOCOL)
with open('agent_{}_constr.pkl'.format(agent_id), 'wb') as output:
pickle.dump(constr, output, pickle.HIGHEST_PROTOCOL)
np.save("agent_{}_seq.npy".format(agent_id), constrcons_seq)
algorithm = ADMM(agent=agent,
initial_lambda=initial_lambda,
initial_z=initial_z,
enable_log=True)
# run the algorithm
x_sequence, lambda_sequence, z_sequence = algorithm.run(iterations=100, penalty=0.1, verbose=True)
x_t, lambda_t, z_t = algorithm.get_result()
print("Agent {}: primal {} dual {} auxiliary primal {}".format(agent.id, x_t.flatten(), lambda_t, z_t.flatten()))
np.save("agents.npy", nproc)
# save agent and sequence
if local_rank == 0:
with open('constraints.pkl', 'wb') as output:
pickle.dump(constr, output, pickle.HIGHEST_PROTOCOL)
with open('agent_{}_function.pkl'.format(agent.id), 'wb') as output:
pickle.dump(agent.problem.objective_function, output, pickle.HIGHEST_PROTOCOL)
with open('agent_{}_dual_sequence.pkl'.format(agent.id), 'wb') as output:
pickle.dump(lambda_sequence, output, pickle.HIGHEST_PROTOCOL)
np.save("agent_{}_primal_sequence.npy".format(agent.id), x_sequence)
np.save("agent_{}_auxiliary_primal_sequence.npy".format(agent.id), z_sequence)
def byte_message(myid, code, message):
return pickle.dumps({
'myid': myid,
'code': code,
'message': message
}, protocol=pickle.HIGHEST_PROTOCOL)
def loadDump(filename):
"""Loads a dump file.
Parameters
----------
filename : String
Dump file to load.
Returns
-------
object
"""
with open(filename, 'rb') as f:
return pickle.load(f, pickle.HIGHEST_PROTOCOL)
self.ONEWAY_THREADED = True # oneway calls run in their own thread
self.DETAILED_TRACEBACK = False
self.THREADPOOL_SIZE = 40
self.THREADPOOL_SIZE_MIN = 4
self.AUTOPROXY = True
self.MAX_MESSAGE_SIZE = 0 # 0 = unlimited
self.BROADCAST_ADDRS = ", 0.0.0.0" # comma separated list of broadcast addresses
self.FLAME_ENABLED = False
self.PREFER_IP_VERSION = 4 # 4, 6 or 0 (let OS choose according to RFC 3484)
self.SERIALIZER = "serpent"
self.SERIALIZERS_ACCEPTED = "serpent,marshal,json" # these are the 'safe' serializers that are always available
self.LOGWIRE = False # log wire-level messages
self.PICKLE_PROTOCOL_VERSION = pickle.HIGHEST_PROTOCOL
try:
import dill
self.DILL_PROTOCOL_VERSION = dill.HIGHEST_PROTOCOL # Highest protocol
except ImportError:
self.DILL_PROTOCOL_VERSION = -1
self.METADATA = True # get metadata from server on proxy connect
self.REQUIRE_EXPOSE = True # require @expose to make members remotely accessible (if False, everything is accessible)
self.USE_MSG_WAITALL = hasattr(socket, "MSG_WAITALL") and platform.system() != "Windows" # waitall is not reliable on windows
self.JSON_MODULE = "json"
self.MAX_RETRIES = 0
self.ITER_STREAMING = True
self.ITER_STREAM_LIFETIME = 0.0
self.ITER_STREAM_LINGER = 30.0
self.SSL = False
self.SSL_SERVERCERT = ""
self.SSL_SERVERKEY = ""
self.SSL_SERVERKEYPASSWD = ""
self.SSL_REQUIRECLIENTCERT = False
self.SSL_CLIENTCERT = ""
def reduce_memmap(a):
"""Pickle the descriptors of a memmap instance to reopen on same file"""
m = _get_backing_memmap(a)
if m is not None:
# m is a real mmap backed memmap instance, reduce a preserving striding
# information
return _reduce_memmap_backed(a, m)
else:
# This memmap instance is actually backed by a regular in-memory
# buffer: this can happen when using binary operators on numpy.memmap
# instances
return (loads, (dumps(np.asarray(a), protocol=HIGHEST_PROTOCOL),))