Skip to content
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
10 changes: 6 additions & 4 deletions azure_tools/azurepool.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

try:
Expand All @@ -12,7 +14,7 @@
import azure.batch.batch_auth as batchauth
import azure.batch.models as batchmodels

import common.helpers
from . import common.helpers
import time

try:
Expand Down Expand Up @@ -168,7 +170,7 @@ def map(self, parameters, job_id):
job_id = common.helpers.generate_unique_resource_name(
job_id)
common.helpers.delete_blobs_from_container(block_blob_client, self.out)
input_file_names = [os.path.join(self.data_dir, str(i) + '.pk') for i in xrange(len(parameters))]
input_file_names = [os.path.join(self.data_dir, str(i) + '.pk') for i in range(len(parameters))]
for i, p in enumerate(parameters):
pickle.dump(p, open(input_file_names[i], 'wb'))
# input_file_names = [os.path.realpath(fn) for fn in input_file_names]
Expand All @@ -194,7 +196,7 @@ def map(self, parameters, job_id):

# print(os.path.join(self.data_dir, str(0) + '_out.pk'))
ret = []
for i in xrange(len(parameters)):
for i in range(len(parameters)):
fnm = os.path.join(self.data_dir, str(i) + '_out.pk')
if os.path.isfile(fnm):
ret.append(pickle.load(open(fnm)))
Expand Down Expand Up @@ -312,7 +314,7 @@ def run_commands(batch_client, block_blob_client, job_id, pool_id):
id="EBOTask-{}".format(i),
command_line=common.helpers.wrap_commands_in_shell('linux', task_commands),
user_identity=batchmodels.UserIdentity(auto_user=user)) \
for i in xrange(len(nodes))]
for i in range(len(nodes))]

batch_client.task.add_collection(job.id, tasks)
logging.info('task created in seconds {}'.format(time.time() - start))
Expand Down
7 changes: 5 additions & 2 deletions azure_tools/bo_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os

import azure.storage.blob as azureblob
try:
import cPickle as pickle
import pickle as pickle
except:
import pickle

Expand Down Expand Up @@ -40,7 +43,7 @@

#print(res)
pickle.dump(res, open(output_file, 'wb'))

print("bo_wrapper.py listing files:")
for item in os.listdir('.'):
print(item)
Expand Down
3 changes: 3 additions & 0 deletions azure_tools/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import datetime
import io
import os
Expand Down
8 changes: 6 additions & 2 deletions ebo_core/bo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from gibbs import GibbsSampler
from .gibbs import GibbsSampler
from scipy.optimize import minimize


Expand Down Expand Up @@ -63,7 +67,7 @@ def global_minimize(f, x_range, n, n_bo=1, n_bo_top_percent=1.0):
ty = np.hstack((ty, res.fun))
inds = ty.argsort()
thres = np.ceil(n_bo * n_bo_top_percent).astype(int)
inds_of_inds = np.hstack((range(thres), np.random.permutation(range(thres, len(inds)))))
inds_of_inds = np.hstack((list(range(thres)), np.random.permutation(list(range(thres, len(inds))))))
inds = inds[inds_of_inds[:n_bo]]
return tx[inds, :]

Expand Down
32 changes: 18 additions & 14 deletions ebo_core/ebo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time

import helper
from . import helper
import numpy as np
from mondrian import MondrianTree
from mypool import MyPool
from .mondrian import MondrianTree
from .mypool import MyPool

try:
import cPickle as pickle
import pickle as pickle
except:
import pickle
from gp_tools.representation import DenseL1Kernel
Expand Down Expand Up @@ -50,8 +54,8 @@ def get_params(self):

def run(self):
x_range, T, B, dim_limit, min_leaf_size, max_n_leaves, n_bo, n_top = self.get_params()
tstart = self.X.shape[0] / B
for t in xrange(tstart, T):
tstart = self.X.shape[0] // B
for t in range(tstart, T):
# search space partition
ref = self.y.min() if self.y.shape[0] > 0 else None
self.tree = MondrianTree(self.X, self.y, x_range, max_n_leaves, reference=ref)
Expand All @@ -71,7 +75,7 @@ def run(self):

self.timing.append((self.X.shape[0], elapsed))
# allocate worker budget
newX, newacf, z_all, k_all = zip(*res)
newX, newacf, z_all, k_all = list(zip(*res))
# sync hyper parameters

if self.options['gibbs_iter'] != 0:
Expand Down Expand Up @@ -99,13 +103,13 @@ def run(self):
self.pause()

def choose_newX(self, newX, newacf, n_top, B):
print
print()
'start choosing newX'
start = time.time()
inds = newacf.argsort()
if 'heuristic' in self.options and self.options['heuristic']:
n_top = np.ceil(B / 2.).astype(int)
inds_of_inds = np.hstack((range(n_top), np.random.permutation(range(n_top, len(inds)))))
inds_of_inds = np.hstack((list(range(n_top)), np.random.permutation(list(range(n_top, len(inds))))))
newX = newX[inds[inds_of_inds[:B]]]
return newX

Expand Down Expand Up @@ -143,7 +147,7 @@ def choose_newX(self, newX, newacf, n_top, B):
all_candidates = all_candidates[all_candidates != jbest]
next_ind += 1
rec.append(marginal)
print
print()
'finished choosing newX, eplased time = ', time.time() - start

return newX[good_inds]
Expand All @@ -163,17 +167,17 @@ def print_step(self, newX, t):
if self.options['isplot']:
plot_ebo(self.tree, newX, t)
_, besty, cur = self.get_best()
print
print()
't=', t, ', bestid=', cur, ', besty=', besty
print
print()
'final z=', self.z, ' final k=', self.k

def reload(self):
fnm = self.options['save_file_name']
if not os.path.isfile(fnm):
return False
self.X, self.y, self.z, self.k, self.timing = pickle.load(open(fnm))
print
print()
'Successfully reloaded file.'

# This will save the pool workers
Expand All @@ -191,7 +195,7 @@ def save(self):
if not os.path.exists(dirnm):
os.makedirs(dirnm)
pickle.dump([self.X, self.y, self.z, self.k, self.timing], open(fnm, 'wb'))
print
print()
'saving file... ', time.time() - start, ' seconds'


Expand Down
14 changes: 9 additions & 5 deletions ebo_core/gibbs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import helper
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from . import helper
import numpy as np
import sklearn.random_projection as rp
from gp_tools.gp import SparseFeatureGP, DenseFeatureGP, DenseKernelGP, SparseKernelGP
Expand Down Expand Up @@ -71,7 +75,7 @@ def get_tilegp(self):
all_cat = np.unique(self.z)
if self.tilecap:
hashing_mem = self.tilecap / len(all_cat) / nlayers
hashing = [rp.UNH(hashing_mem) for _ in xrange(len(all_cat))]
hashing = [rp.UNH(hashing_mem) for _ in range(len(all_cat))]
for a in all_cat:
inds = helper.find(self.z == a)
indices.append(inds)
Expand Down Expand Up @@ -109,10 +113,10 @@ def get_tilegp(self):

# idea: can instead get log likelihood on different subset of data for gibbs
def run(self, niter):
for i in xrange(niter):
for i in range(niter):
# sample z w/ limit on size
# random permute dimensions
for d in np.random.permutation(range(self.xdim)):
for d in np.random.permutation(list(range(self.xdim))):
# sample z_d
# initialize
final_z = self.z.copy()
Expand All @@ -130,7 +134,7 @@ def run(self, niter):
other_cat = other_cat[np.logical_and(other_cat != zd_old, other_cat != -1)]
# otherwise, need to remove z[d] and add one additional category
if a_size > 0 and other_cat.size + 1 < self.n_add:
for a in xrange(self.n_add):
for a in range(self.n_add):
if (a not in other_cat) and (a != zd_old):
other_cat = np.append(other_cat, [a])
break
Expand Down
4 changes: 4 additions & 0 deletions ebo_core/helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import scipy.cluster.hierarchy as hi

Expand Down
12 changes: 8 additions & 4 deletions ebo_core/mondrian.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import helper
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from . import helper
import numpy as np


Expand Down Expand Up @@ -63,7 +67,7 @@ def grow_tree(self, min_leaf_size):
for node in leaves])
mask = np.maximum(prob[:, 1] - min_leaf_size, 0)
if mask.sum() == 0:
print
print()
'Mondrian stopped at ', str(len(leaves)), ' number of leaves.'
break

Expand All @@ -90,7 +94,7 @@ def update_leaf_data(self, X, y, ref=None):

def visualize(self):
if self.leaves is None or self.X.shape[1] != 2:
print
print()
'error: x shape is wrong or leaves is none.'

# visaulize 2d mondrians
Expand All @@ -104,7 +108,7 @@ def visualize(self):
mondrian_colors = mondrian_colors / 255.0
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
print('number of leaves = {}'.format(len(self.leaves)))
print(('number of leaves = {}'.format(len(self.leaves))))
for node in self.leaves:
xy = node.x_range[0]
xylen = node.x_range[1] - node.x_range[0]
Expand Down
6 changes: 5 additions & 1 deletion ebo_core/mypool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from bo import bo
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from .bo import bo


class MyPool(object):
Expand Down
9 changes: 8 additions & 1 deletion gp_tools/gp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import scipy.linalg
import scipy.sparse

try:
import sksparse.cholmod as spch
except ImportError:
pass

# import sksparse.cholmod as spch
class SparseFeatureGP:
def __init__(self, X, y, sigma, phi):
self.X = X
Expand Down
14 changes: 9 additions & 5 deletions gp_tools/representation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from itertools import chain, izip
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from itertools import chain

import numpy as np
from scipy.sparse import csr_matrix
Expand Down Expand Up @@ -463,7 +467,7 @@ def __init__(self,
self.offset = offset
if offset is None:
self.offset = np.empty((ntiles.shape[1], ntilings))
for i in xrange(ntiles.shape[0]):
for i in range(ntiles.shape[0]):
self.offset[i, :] = -rnd_stream.random_sample(ntilings) / ntiles[0, i]

if self.hashing == None:
Expand Down Expand Up @@ -578,10 +582,10 @@ def __init__(self,
self.tilings = [Tiling(in_index, nt, t, self.state_range, rnd_stream, offset=o, hashing=h)
for in_index, nt, t, h, o
in zip(input_indices, ntiles, ntilings, hashing, offsets)]
self.__size = sum(map(lambda x: x.size, self.tilings))
self.__size = sum([x.size for x in self.tilings])
self.bias_term = bias_term
self.index_offset = np.zeros(len(ntilings), dtype='int')
self.index_offset[1:] = np.cumsum(map(lambda x: x.size, self.tilings[:-1]))
self.index_offset[1:] = np.cumsum([x.size for x in self.tilings[:-1]])
self.index_offset = np.hstack([np.array([off] * t, dtype='int')
for off, t in zip(self.index_offset, ntilings)])

Expand Down Expand Up @@ -759,7 +763,7 @@ def grid_of_points(state_range, num_centers):
if isinstance(num_centers, int):
num_centers = [num_centers] * state_range[0].shape[0]
points = [np.linspace(start, stop, num, endpoint=True)
for start, stop, num in izip(state_range[0],
for start, stop, num in zip(state_range[0],
state_range[1],
num_centers)]

Expand Down
Loading