Ejemplo n.º 1
0
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
                    }))
Ejemplo n.º 2
0
def train_model(x_train, y_train):
    # x = tf.placeholder(tf.float32, shape=[None, 2])
    # widths of fully-connected layers in NN
    inputs = 2
    outputs = 1
    layer_widths = [8, 8, 8, 8, 8]
    activations = [tf.nn.elu for _ in layer_widths] + [tf.identity]
    layer_widths += [outputs]
    # Input data goes here (via feed_dict or equiv)
    x = tf.placeholder(tf.float32, shape=[len(x_train), inputs])
    net = Nets.SuperDenseNet(inputs, layer_widths, activations)
    # Construct all parameters of NN, set to independant gaussian priors
    weights = [gauss_prior(shape) for shape in net.weight_shapes()]
    biases = [gauss_prior(shape) for shape in net.bias_shapes()]
    out = ed.models.Bernoulli(logits=net.apply(x, weights, biases))

    # Variational 'posterior's for NN params
    qweights = [gauss_var_post(w.shape) for w in weights]
    qbiases = [gauss_var_post(b.shape) for b in biases]

    # Map from random variables to their variational posterior objects
    weights_post = {weights[i]: qweights[i] for i in range(len(weights))}
    biases_post = {biases[i]: qbiases[i] for i in range(len(weights))}
    var_post = {**weights_post, **biases_post}

    # evaluate 'accuracy' (what even is this??) and likelihood of model over the dataset before training
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out: y_train,
                        x: x_train
                    }))
    # Run variational inference, minimizing KL(q, p) using stochastic gradient descent over variational params
    inference = ed.KLqp(var_post, data={out: y_train, x: x_train})
    inference.run(n_samples=16, n_iter=10000)

    # Get output object dependant on variational posteriors rather than priors
    out_post = ed.copy(out, var_post)
    # Re-evaluate metrics
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out_post: y_train,
                        x: x_train
                    }))
Ejemplo n.º 3
0
                                  [-1, 2 * self.features])
        return self.predictor.apply(
            tf.concat(
                [team_vectors, tf.cast(x[:, -1:], tf.float32)], 1),
            self.prior[:-1])


def read_csv(league):
    return pd.read_csv(_constants.data_location +
                       'simple_game_data_leagueId={}.csv'.format(league))


if __name__ == '__main__':
    games, results, teams = read_data(read_csv(2))
    print(games[0])
    outputs = 32
    print(results)
    team_vector_length = 1
    layer_widths = []
    activations = [Nets.selu for _ in layer_widths] + [tf.identity]
    layer_widths += [outputs]

    net = Nets.SuperDenseNet(2 * team_vector_length + 1, layer_widths,
                             activations)
    predictor = FactoredPredictor(net, len(results[0]))
    myModel = PairModel(teams, predictor, team_vector_length)

    # tf.global_variables_initializer()

    myModel.train_model(games, results)