Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows port #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions spearmint/ExperimentGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import numpy.random as npr

from spearmint_pb2 import *
from Locker import *
from lockfile import FileLock
from sobol_lib import *

CANDIDATE_STATE = 0
Expand Down Expand Up @@ -57,11 +57,11 @@ def job_broken(expt_dir, id):
def __init__(self, expt_dir, variables=None, grid_size=None, grid_seed=1):
self.expt_dir = expt_dir
self.jobs_pkl = os.path.join(expt_dir, 'expt-grid.pkl')
self.locker = Locker()
self.locker = FileLock(self.jobs_pkl);

# Only one process at a time is allowed to have access to this.
sys.stderr.write("Waiting to lock grid...")
self.locker.lock_wait(self.jobs_pkl)
self.locker.acquire();
sys.stderr.write("...acquired\n")

# Does this exist already?
Expand All @@ -85,10 +85,8 @@ def __init__(self, expt_dir, variables=None, grid_size=None, grid_seed=1):

def __del__(self):
self._save_jobs()
if self.locker.unlock(self.jobs_pkl):
sys.stderr.write("Released lock on job grid.\n")
else:
raise Exception("Could not release lock on job grid.\n")
self.locker.release()
sys.stderr.write("Released lock on job grid.\n")

def get_grid(self):
return self.grid, self.values, self.durs
Expand Down Expand Up @@ -118,7 +116,7 @@ def get_best(self):
return np.nan, -1

def get_sgeid(self, id):
return self.sgeids[id]
return np.asscalar(self.sgeids[id])

def add_to_grid(self, candidate):
# Set up the grid
Expand Down Expand Up @@ -182,7 +180,10 @@ def _save_jobs(self):
fh.close()

# Use an atomic move for better NFS happiness.
cmd = 'mv "%s" "%s"' % (fh.name, self.jobs_pkl)
if os.name =='nt':
cmd = 'move "%s" "%s"' % (fh.name, self.jobs_pkl)
else:
cmd = 'mv "%s" "%s"' % (fh.name, self.jobs_pkl)
os.system(cmd) # TODO: Should check system-dependent return status.

def _hypercube_grid(self, dims, size):
Expand Down
145 changes: 72 additions & 73 deletions spearmint/GPConstrainedEIChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import multiprocessing
import copy

from Locker import *
from lockfile import FileLock

# Wrapper function to pass to parallel ei optimization calls
def optimize_pt(c, b, comp, pend, vals, labels, model):
Expand Down Expand Up @@ -64,8 +64,8 @@ def __init__(self, expt_dir, covar="Matern52", mcmc_iters=20,
grid_subset=20, constraint_violating_value=np.inf,
verbosity=0, visualize2D=False):
self.cov_func = getattr(gp, covar)
self.locker = Locker()
self.state_pkl = os.path.join(expt_dir, self.__module__ + ".pkl")
self.state_lock = FileLock(self.state_pkl)

self.stats_file = os.path.join(expt_dir,
self.__module__ + "_hyperparameters.txt")
Expand Down Expand Up @@ -99,28 +99,29 @@ def __init__(self, expt_dir, covar="Matern52", mcmc_iters=20,
# if the optimization is restarted.
def dump_hypers(self):
sys.stderr.write("Waiting to lock hyperparameter pickle...")
self.locker.lock_wait(self.state_pkl)
sys.stderr.write("...acquired\n")

# Write the hyperparameters out to a Pickle.
fh = tempfile.NamedTemporaryFile(mode='w', delete=False)
cPickle.dump({ 'dims' : self.D,
'ls' : self.ls,
'amp2' : self.amp2,
'noise' : self.noise,
'mean' : self.mean,
'constraint_ls' : self.constraint_ls,
'constraint_amp2' : self.constraint_amp2,
'constraint_noise' : self.constraint_noise,
'constraint_mean' : self.constraint_mean },
fh)
fh.close()

# Use an atomic move for better NFS happiness.
cmd = 'mv "%s" "%s"' % (fh.name, self.state_pkl)
os.system(cmd) # TODO: Should check system-dependent return status.

self.locker.unlock(self.state_pkl)
with self.state_lock:
sys.stderr.write("...acquired\n")

# Write the hyperparameters out to a Pickle.
fh = tempfile.NamedTemporaryFile(mode='w', delete=False)
cPickle.dump({ 'dims' : self.D,
'ls' : self.ls,
'amp2' : self.amp2,
'noise' : self.noise,
'mean' : self.mean,
'constraint_ls' : self.constraint_ls,
'constraint_amp2' : self.constraint_amp2,
'constraint_noise' : self.constraint_noise,
'constraint_mean' : self.constraint_mean },
fh)
fh.close()

# Use an atomic move for better NFS happiness.
if os.name =='nt':
cmd = 'move "%s" "%s"' % (fh.name, self.state_pkl)
else:
cmd = 'mv "%s" "%s"' % (fh.name, self.state_pkl)
os.system(cmd) # TODO: Should check system-dependent return status.

# Write the hyperparameters out to a human readable file as well
fh = open(self.stats_file, 'w')
Expand All @@ -143,55 +144,53 @@ def dump_hypers(self):
def _real_init(self, dims, values, durations):

sys.stderr.write("Waiting to lock hyperparameter pickle...")
self.locker.lock_wait(self.state_pkl)
sys.stderr.write("...acquired\n")

self.randomstate = npr.get_state()
if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'r')
state = cPickle.load(fh)
fh.close()

self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
self.constraint_ls = state['constraint_ls']
self.constraint_amp2 = state['constraint_amp2']
self.constraint_noise = state['constraint_noise']
self.constraint_mean = state['constraint_mean']
self.constraint_gain = state['constraint_mean']
self.needs_burnin = False
else:

# Identify constraint violations
# Note that we'll treat NaNs and Infs as these values as well
# as an optional user defined value
goodvals = np.nonzero(np.logical_and(values != self.bad_value,
np.isfinite(values)))[0]

# Input dimensionality.
self.D = dims

# Initial length scales.
self.ls = np.ones(self.D)
self.constraint_ls = np.ones(self.D)

# Initial amplitude.
self.amp2 = np.std(values[goodvals])
self.constraint_amp2 = 1#np.std(durations)

# Initial observation noise.
self.noise = 1e-3
self.constraint_noise = 1e-3
self.constraint_gain = 1

# Initial mean.
self.mean = np.mean(values[goodvals])
self.constraint_mean = 0.5

self.locker.unlock(self.state_pkl)
with self.state_lock:
sys.stderr.write("...acquired\n")

self.randomstate = npr.get_state()
if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'r')
state = cPickle.load(fh)
fh.close()

self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
self.constraint_ls = state['constraint_ls']
self.constraint_amp2 = state['constraint_amp2']
self.constraint_noise = state['constraint_noise']
self.constraint_mean = state['constraint_mean']
self.constraint_gain = state['constraint_mean']
self.needs_burnin = False
else:

# Identify constraint violations
# Note that we'll treat NaNs and Infs as these values as well
# as an optional user defined value
goodvals = np.nonzero(np.logical_and(values != self.bad_value,
np.isfinite(values)))[0]

# Input dimensionality.
self.D = dims

# Initial length scales.
self.ls = np.ones(self.D)
self.constraint_ls = np.ones(self.D)

# Initial amplitude.
self.amp2 = np.std(values[goodvals])
self.constraint_amp2 = 1#np.std(durations)

# Initial observation noise.
self.noise = 1e-3
self.constraint_noise = 1e-3
self.constraint_gain = 1

# Initial mean.
self.mean = np.mean(values[goodvals])
self.constraint_mean = 0.5

def cov(self, amp2, ls, x1, x2=None):
if x2 is None:
Expand Down
101 changes: 50 additions & 51 deletions spearmint/GPEIChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import scipy.optimize as spo
import cPickle

from Locker import *
from lockfile import FileLock

def init(expt_dir, arg_string):
args = util.unpack_args(arg_string)
Expand All @@ -48,8 +48,8 @@ class GPEIChooser:
def __init__(self, expt_dir, covar="Matern52", mcmc_iters=10,
pending_samples=100, noiseless=False):
self.cov_func = getattr(gp, covar)
self.locker = Locker()
self.state_pkl = os.path.join(expt_dir, self.__module__ + ".pkl")
self.state_lock = FileLock(self.state_pkl)

self.mcmc_iters = int(mcmc_iters)
self.pending_samples = pending_samples
Expand All @@ -63,59 +63,58 @@ def __init__(self, expt_dir, covar="Matern52", mcmc_iters=10,

def __del__(self):
sys.stderr.write("Waiting to lock hyperparameter pickle...")
self.locker.lock_wait(self.state_pkl)
sys.stderr.write("...acquired\n")

# Write the hyperparameters out to a Pickle.
fh = tempfile.NamedTemporaryFile(mode='w', delete=False)
cPickle.dump({ 'dims' : self.D,
'ls' : self.ls,
'amp2' : self.amp2,
'noise' : self.noise,
'mean' : self.mean },
fh)
fh.close()

# Use an atomic move for better NFS happiness.
cmd = 'mv "%s" "%s"' % (fh.name, self.state_pkl)
os.system(cmd) # TODO: Should check system-dependent return status.

self.locker.unlock(self.state_pkl)
with self.state_lock:
sys.stderr.write("...acquired\n")

# Write the hyperparameters out to a Pickle.
fh = tempfile.NamedTemporaryFile(mode='w', delete=False)
cPickle.dump({ 'dims' : self.D,
'ls' : self.ls,
'amp2' : self.amp2,
'noise' : self.noise,
'mean' : self.mean },
fh)
fh.close()

# Use an atomic move for better NFS happiness.
if os.name =='nt':
cmd = 'move "%s" "%s"' % (fh.name, self.state_pkl)
else:
cmd = 'mv "%s" "%s"' % (fh.name, self.state_pkl)
os.system(cmd) # TODO: Should check system-dependent return status.

def _real_init(self, dims, values):

sys.stderr.write("Waiting to lock hyperparameter pickle...")
self.locker.lock_wait(self.state_pkl)
sys.stderr.write("...acquired\n")

if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'r')
state = cPickle.load(fh)
fh.close()

self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
else:

# Input dimensionality.
self.D = dims

# Initial length scales.
self.ls = np.ones(self.D)

# Initial amplitude.
self.amp2 = np.std(values)

# Initial observation noise.
self.noise = 1e-3

# Initial mean.
self.mean = np.mean(values)

self.locker.unlock(self.state_pkl)
with self.state_lock:
sys.stderr.write("...acquired\n")

if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'r')
state = cPickle.load(fh)
fh.close()

self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
else:

# Input dimensionality.
self.D = dims

# Initial length scales.
self.ls = np.ones(self.D)

# Initial amplitude.
self.amp2 = np.std(values)

# Initial observation noise.
self.noise = 1e-3

# Initial mean.
self.mean = np.mean(values)

def cov(self, x1, x2=None):
if x2 is None:
Expand Down
Loading