Skip to content

Instantly share code, notes, and snippets.

class AddSign(optimizer.Optimizer):
"""Implementation of AddSign.
See [Bello et. al., 2017](https://arxiv.org/abs/1709.07417)
@@__init__
"""
def __init__(self, learning_rate=1.001,alpha=0.01,beta=0.5, use_locking=False, name="AddSign"):
super(AddSign, self).__init__(use_locking, name)
self._lr = learning_rate
class PowerSign(optimizer.Optimizer):
"""Implementation of PowerSign.
See [Bello et. al., 2017](https://arxiv.org/abs/1709.07417)
@@__init__
"""
def __init__(self, learning_rate=0.001,alpha=0.01,beta=0.5, use_locking=False, name="PowerSign"):
super(PowerSign, self).__init__(use_locking, name)
self._lr = learning_rate
self._alpha = alpha
self._beta = beta
# This class defines the API to add Ops to train a model.
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.framework import ops
from tensorflow.python.training import optimizer
import tensorflow as tf
 def _create_slots(self, var_list):
# Create slots for allocation and later management of additional
# variables associated with the variables to train.
# for example: the first and second moments.
'''
for v in var_list:
self._zeros_slot(v, "m", self._name)
self._zeros_slot(v, "v", self._name)
'''
def _apply_dense(self, grad, var):
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
class SoftDecisionTree(object):
def __init__(self, *args,**kwargs):
self.params = TreeProperties(*args,**kwargs)
self.loss = 0.0
self.output = list()
self.leafs_distribution = list()
def build_tree(self):
self.tf_X = tf.placeholder(tf.float32, [None, self.params.n_features])
class Node(object):
def __init__(self,id,depth,pathprob,tree):
self.id = id
self.depth = depth
self.prune(tree)
if self.isLeaf:
self.W = tf.get_variable(...)
self.b = tf.get_variable(...)
else:
self.W = tf.get_variable(...)
class TreeProperties(object):
'''
:param max_leafs: maximum number of leafs
:param n_features: maximum number of feature available within the data
:param n_classes: number of classes
'''
def __init__(self,max_depth,max_leafs,n_features,n_classes,regularisation_penality=10.,decay_penality=0.9):
self.max_depth = max_depth
self.max_leafs = max_leafs
self.n_features = n_features
import numpy as np
np.random.seed(1)
def f(x):
"""The function to predict."""
return x *np.sin(x)
#----------------------------------------------------------------------
# First the noiseless case
X = np.atleast_2d(np.random.uniform(0, 10.0, size=100)).T
class XGBQuantile(XGBRegressor):
def __init__(self,quant_alpha=0.95,quant_delta = 1.0,quant_thres=1.0,quant_var =1.0,base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=1, gamma=0, learning_rate=0.1, max_delta_step=0,max_depth=3, min_child_weight=1, missing=None, n_estimators=100,
n_jobs=1, nthread=None, objective='reg:linear', random_state=0,reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,silent=True, subsample=1):
self.quant_alpha = quant_alpha
self.quant_delta = quant_delta
self.quant_thres = quant_thres
self.quant_var = quant_var
super().__init__(base_score=base_score, booster=booster, colsample_bylevel=colsample_bylevel,