Skip to content

Instantly share code, notes, and snippets.

def get_cost(target,Q,action_indices):
"""
Cost-function of the Q-matrix attempting to approximate the reward-function
:param tf.placeholder target: placeholder for the values of the registered rewards
:param tf.placeholder Q: output of the Q-matrix the registered state
:param tf.placeholder action_indices: placeholder for the indices of the registered actions
:return: tf.tensor mean_squared error the reward vs Q-matrix
"""
row_indices = tf.range(tf.shape(action_indices)[0])
full_indices = tf.stack([row_indices, action_indices], axis=1)
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
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
# 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):
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
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 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])
cluster_spec = tf.train.ClusterSpec({'worker' : ['localhost:2222']})
server = tf.train.Server(cluster_spec)
def map_fun(args, ctx):
try:
import tensorflow as tf
#utils
from datetime import datetime
import time
import logging
import numpy as np