def fit(self, X, y, sample_weight=None): global g_seed global g_loop if sample_weight is None: sample_weight = np.ones(X.shape[0], np.float32) print("Calling fit with sample_weight None") else: sample_weight *= X.shape[0] print("Calling fit with sample_weight sum", sample_weight.sum()) # sample_weight = np.ones(X.shape[0], np.float32) # II = sample_weight > 0 # X = X[II] # y = y[II] # sample_weight = sample_weight[II] # sample_weight = np.ones(X.shape[0]) w = sample_weight # sample_weight[:10] = 0.0 # w[:1000] = 0.0 # w = sample_weight # w0 = w / w.sum() # print('Weight entropy:', -np.sum(w0 * np.log2(w0))) print("Weight max:", w.max()) print("Weight min:", w.min()) # import sys; sys.exit(0) self.classes_ = np.unique(y) self.n_classes_ = len(self.classes_) # Set up weighted database create_weighted_db(X, y, sample_weight) # steps = [(0.001, 2000, 2000)] steps = [(0.001, 0.004, 60000), (0.0001, 0.004, 5000), (0.00001, 0.004, 5000)] # steps = [(0.00001, 10000, 10000), (0.000001, 5000, 15000), (0.0000001, 5000, 20000)] # steps = [(0.001, 10000, 10000)] # steps = [(0.001, 200, 1000)] name = os.path.join(CONF_DIR, "adaboost_{}_loop{}".format(g_rnd, g_loop)) bare_conf_fn = os.path.join(CONF_DIR, "boost_bare.prototxt") conf_fn = os.path.join(CONF_DIR, "solver.prototxt.template") # bare_conf_fn = 'regaug_bare.prototxt' # conf_fn = 'regaug_solver.prototxt.template' net, info = train_model(name, conf_fn, bare_conf_fn, steps, seed=g_seed, device_id=DEVICE_ID) loss_fn = "info/info_{}_loop{}.h5".format(g_rnd, g_loop) dd.io.save(loss_fn, info) print("Saved to", loss_fn) g_loop += 1 print("Classifier set up") self.net_ = net
if 0: # Just load a pre-calculated version instead model_fn = base + '.caffemodel' net = caffe.Classifier(bare_conf_fn, model_fn, image_dims=(32, 32)) net.set_phase_test() net.set_mode_gpu() net.set_device(DEVICE_ID) all_fmj = dd.io.load('all_fmj0_eps_inf.h5') all_te_fmj = dd.io.load('all_te_fmj0_eps_inf.h5') else: #warmstart_fn = base + '.solverstate' #warmstart_fn = 'models/regression100_6916_loop0_iter_70000.solverstate' #warmstart_fn = 'models/adaboost100_35934_loop0_iter_70000.solverstate' warmstart_fn = None net, info = train_model(name, solver_conf_fn, conf_fn, bare_conf_fn, steps, seed=g_seed, logfile=logfile, device_id=DEVICE_ID, warmstart=warmstart_fn) all_fmj = net.forward_all(data=X).values()[0].squeeze(axis=(2,3)) all_te_fmj = net.forward_all(data=te_X).values()[0].squeeze(axis=(2,3)) all_fmj *= zstd all_te_fmj *= zstd g_seed += 1 #if loop == 0: #dd.io.save('all_fmj0_eps_inf.h5', all_fmj) #dd.io.save('all_te_fmj0_eps_inf.h5', all_te_fmj) fs = (K - 1) / K * (all_fmj - ag.apply_once(np.mean, all_fmj, [1])) te_fs = (K - 1) / K * (all_te_fmj - ag.apply_once(np.mean, all_te_fmj, [1]))
def fit(self, X, y, sample_weight=None): global g_seed global g_loop if sample_weight is None: sample_weight = np.ones(X.shape[0], np.float32) print('Calling fit with sample_weight None') else: sample_weight *= X.shape[0] print('Calling fit with sample_weight sum', sample_weight.sum()) #sample_weight = np.ones(X.shape[0], np.float32) #II = sample_weight > 0 #X = X[II] #y = y[II] #sample_weight = sample_weight[II] #sample_weight = np.ones(X.shape[0]) w = sample_weight #sample_weight[:10] = 0.0 #w[:1000] = 0.0 #w = sample_weight #w0 = w / w.sum() #print('Weight entropy:', -np.sum(w0 * np.log2(w0))) print('Weight max:', w.max()) print('Weight min:', w.min()) #import sys; sys.exit(0) self.classes_ = np.unique(y) self.n_classes_ = len(self.classes_) # Set up weighted database create_weighted_db(X, y, sample_weight) #steps = [(0.001, 2000, 2000)] steps = [(0.001, 0.004, 60000), (0.0001, 0.004, 5000), (0.00001, 0.004, 5000)] #steps = [(0.00001, 10000, 10000), (0.000001, 5000, 15000), (0.0000001, 5000, 20000)] #steps = [(0.001, 10000, 10000)] #steps = [(0.001, 200, 1000)] name = os.path.join(CONF_DIR, 'adaboost_{}_loop{}'.format(g_rnd, g_loop)) bare_conf_fn = os.path.join(CONF_DIR, 'boost_bare.prototxt') conf_fn = os.path.join(CONF_DIR, 'solver.prototxt.template') #bare_conf_fn = 'regaug_bare.prototxt' #conf_fn = 'regaug_solver.prototxt.template' net, info = train_model(name, conf_fn, bare_conf_fn, steps, seed=g_seed, device_id=DEVICE_ID) loss_fn = 'info/info_{}_loop{}.h5'.format(g_rnd, g_loop) dd.io.save(loss_fn, info) print('Saved to', loss_fn) g_loop += 1 print('Classifier set up') self.net_ = net