def build_net(x_train, y_train, num_train_steps=10000, x_test=None, y_test=None): # Number of stats currently used to predict outcome- 23 per team + variable for side inputs = 47 outputs = 1 if x_test is None: x_test = x_train if y_test is None: y_test = y_train # widths of fully-connected layers in NN # Input data goes here (via feed_dict or equiv) x = tf.placeholder(tf.float32, shape=[None, inputs]) layer_widths = [16, 16, 16, 16, 16, 16] activations = [Nets.selu for _ in layer_widths] + [tf.identity] layer_widths += [outputs] net = Nets.SuperDenseNet(inputs, layer_widths, activations) # Construct all parameters of NN, set to independant gaussian priors params = [Nets.gauss_prior(shape) for shape in net.param_space()] out = ed.models.Bernoulli(logits=net.apply(x, params)) # Variational 'posterior's for NN params qparams = [Nets.gauss_var_post(w.shape) for w in params] asd = tf.train.AdamOptimizer # Map from random variables to their variational posterior objects params_post = {params[i]: qparams[i] for i in range(len(params))} # evaluate accuracy and likelihood of model over the dataset before training print( 'accuracy, log_likelihood, crossentropy', ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'], data={ out: y_test, x: x_test })) # Run variational inference, minimizing KL(q, p) using stochastic gradient descent over variational params inference = ed.KLqp(params_post, data={out: y_train, x: x_train}) #inference.initialize(optimizer=YFOptimizer()) inference.run(n_samples=32, n_iter=num_train_steps) # Get output object dependant on variational posteriors rather than priors out_post = ed.copy(out, params_post) # Re-evaluate metrics print( 'accuracy, log_likelihood, crossentropy', ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'], data={ out_post: y_test, x: x_test }))
def __init__(self, teams, predictor, features=6, prior=None): self.team_numbers = {} self.team_names = [] self.features = features self.predictor = predictor self.param_space = list(predictor.param_space()) + [[teams, features]] self.var_post = [ Nets.gauss_var_post(shape) for shape in self.param_space ] if prior is None: prior = [Nets.gauss_prior(shape) for shape in self.param_space] self.prior = prior