In [40]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import pymongo
import os
# import branin
import sys,os
sys.path.append(os.path.join(os.getcwd() ,"test_py2"))
from branin import branin
# awesome plot options
plt.style.use('fivethirtyeight')
sns.set(style="white",font_scale=1.5,)
plt.rcParams['figure.figsize'] = (12.0, 6.0)
plt.rcParams['savefig.dpi'] = 100
from matplotlib.colors import LogNorm
%config InlineBackend.figure_format='retina'
%matplotlib inline
In [2]:
!head -n 50 test_py2/branin.py
In [57]:
locmin_z = [3.14159213 , 2.27500065]
min_z = branin(locmin_z[0],locmin_z[1])
print("minima %f at %s "%(min_z,str(locmin_z)))
In [58]:
x = np.linspace(-5, 10, 500 )
y = np.linspace(0, 15, 500 )
Z = np.zeros((x.shape[0],y.shape[0]))
X, Y = np.meshgrid(x, y)
Z = np.array([[ branin(xx,yy) for yy in y] for xx in x])
plt.figure(figsize=(8,6))
plt.pcolor(X,Y,Z.T,
norm=LogNorm(vmin=np.min(Z)*0.9,vmax=np.max(Z)),cmap='bone')
plt.colorbar(label='$f(x,y)$')
levels = np.logspace(np.log10(np.min(Z)*0.9), np.log10(np.max(Z)), 20)
plt.contour(Z.T, levels,linewidths=2,alpha=0.5,extent=(-5,10,0,15))
plt.scatter(min_x,min_y,s=100,c='g',label='global_minima')
# style
plt.legend(loc='best')
plt.xlabel('$x$')
plt.ylabel('$y$')
sns.despine(trim=True)
plt.show()
In [75]:
from scipy.optimize import minimize
guess = (np.mean(x),np.mean(y))
methods = {'BFGS':[],'CG':[],'Nelder-Mead':[]}
def info(x):
pos.append(x)
branin_vector = lambda x: branin(x[0],x[1])
for key,val in methods.items():
pos =[]
print('== %s ==='%key)
res = minimize( branin_vector, guess, jac=False,method=key,callback=info,options={'disp': True})
methods[key]=np.array(pos)
print("success? %s "%(res['success']))
if res['success']:
print("Global minima %f at %s "%(min_z,str(locmin_z)))
print("Found minima %f at %s "%(branin(res['x'][0],res['x'][1]),str(res['x'])))
In [76]:
plt.figure(figsize=(8,6))
plt.pcolor(X,Y,Z.T,
norm=LogNorm(vmin=np.min(Z)*0.9,vmax=np.max(Z)),cmap='bone')
plt.colorbar(label='$f(x,y)$')
levels = np.logspace(np.log10(np.min(Z)*0.9), np.log10(np.max(Z)), 20)
plt.contour(Z.T, levels,linewidths=2,alpha=0.5,extent=(-5,10,0,15))
plt.scatter(min_x,min_y,s=100,c='g',label='global_minima')
for key,val in methods.items():
plt.plot(val[:,0],val[:,1],'-o',label=key)
#plt.scatter(val['x'][0],val['x'][1],s=500,c='r',label='Grid')
# style
plt.legend(loc='best')
plt.xlabel('$x$')
plt.ylabel('$y$')
sns.despine(trim=True)
plt.show()
In [77]:
!mongod --fork --logpath test_py2/db/mongodb.log --dbpath test_py2/db
In [78]:
from pymongo import MongoClient
client = MongoClient()
print("Avail DB's: {}".format(client.database_names()))
db = client['spearmint']
print('DB fields: {}'.format(db.collection_names()))
coll= db['simple-braninhoo-example.jobs']
In [83]:
import zlib
def decompress(values):
val = np.fromstring(zlib.decompress(values['value'].decode('base64'))).reshape(values['shape'])
if values['shape'] == [1]:
val = val[0]
return val
rows = []
for rec in coll.find():
delta_t = rec['end time']-rec['start time']
a_dict={'id':rec['id'],'objective':rec['values']['Objective'],'time':delta_t}
for key,val in rec['params'].items():
a_dict.update({key:decompress(val['values'])})
rows.append(a_dict)
df = pd.DataFrame(rows)
print(df.shape)
df.head()
Out[83]:
In [ ]: