Пример #1
0
    def __init__(self, features_count, initializer_name,
                 hidden_layers_sizes=[10, 5]):
        self.weight_initializer = self.initializers.get(initializer_name)
        # Number of examples
        self.m = Dim.unknown()
        # Number of features
        self.n = features_count
        # Number of hidden units
        self.L = len(hidden_layers_sizes)

        # Placeholders and Vars
        self.X = f.placeholder(shape=(self.n, self.m))
        self.Y = f.placeholder(shape=(1, self.m))

        # Hidden
        a = self.X
        a_size = self.n
        for l, h in enumerate(hidden_layers_sizes):
            linear = self.fully_connected_layer(a, a_size, h, l)
            a = f.relu(linear)
            a_size = h

        # Output layer
        linear = self.fully_connected_layer(a, a_size, 1, self.L)
        loss = f.sigmoid_cross_entropy(linear, self.Y)
        predictions = f.is_greater_than(f.sigmoid(linear), 0.5)

        # Computation graphs
        self.cost_graph = g.Graph(loss)
        self.prediction_graph = g.Graph(predictions)
Пример #2
0
    def __init__(self, features_count, hidden_layers_size):
        # Number of examples
        self.m = Dim.unknown()
        # Number of features
        self.n = features_count
        # Number of hidden units
        self.h = hidden_layers_size
        weight_initializer = init.RandomNormalInitializer()
        bias_initializer = init.ZeroInitializer()

        # Placeholders and Vars
        self.W1 = f.var("W1", weight_initializer, shape=(self.h, self.n))
        self.b1 = f.var("b1", bias_initializer, shape=(self.h, 1))
        self.W2 = f.var("W2", weight_initializer, shape=(1, self.h))
        self.b2 = f.var("b2", bias_initializer, shape=(1, 1))
        self.X = f.placeholder(shape=(self.n, self.m))
        self.Y = f.placeholder(shape=(1, self.m))

        # Nodes
        lin_1 = chains.core.node_factory.fully_connected(self.X,
                                                         self.W1,
                                                         self.b1,
                                                         first_layer=True)
        act_1 = f.tanh(lin_1)
        lin_2 = chains.core.node_factory.fully_connected(
            act_1, self.W2, self.b2)
        loss = f.sigmoid_cross_entropy(lin_2, self.Y)
        predictions = f.is_greater_than(f.sigmoid(lin_2), 0.5)

        # Computation graphs
        self.cost_graph = g.Graph(loss)
        self.prediction_graph = g.Graph(predictions)
Пример #3
0
def test_quadratic():
    x = nf.initialized_var('x', np.array([[0.07], [0.4], [0.1]]))
    A = nf.constant(np.array([[2, -1, -2], [-1, 1, 0], [-2, 0, 4]]))
    B = nf.constant(np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]))
    b = nf.placeholder(shape=(3, 1))

    quadratic = x.T() @ A @ x
    linear = B @ x - b
    squared_linear = nf.mat_sum(linear)**2

    expr = quadratic + squared_linear

    cost_function = nf.as_scalar(expr) + 7

    cost = Graph(cost_function)
    cost.placeholders = {
        b: np.array([1, 0, 0]).reshape(3, 1).astype('float32')
    }
    cost.initialize_variables()
    optimizer = gd.GradientDescentOptimizer(0.1)
    optimizer.prepare_and_check(cost)

    for i in range(500):
        optimizer.run()

    np.testing.assert_allclose(cost.evaluate(), 7.)
    np.testing.assert_allclose(x.value, np.array([[1.], [1.], [.5]]))
Пример #4
0
    def __init__(self, features_count):

        self.m = Dim.unknown()  # Number of examples
        self.n = features_count  # Number of features

        self.W = f.var('W', init.ZeroInitializer(), shape=(1, self.n))
        self.b = f.var('b', init.ZeroInitializer(), shape=(1, 1))
        self.X = f.placeholder(shape=(self.n, self.m))
        self.Y = f.placeholder(shape=(1, self.m))

        self.logits = nf.fully_connected(self.X,
                                         self.W,
                                         self.b,
                                         first_layer=True)
        self.loss = f.sigmoid_cross_entropy(self.logits, self.Y)
        self.predictions = f.is_greater_than(f.sigmoid(self.logits), 0.5)

        self.cost_graph = g.Graph(self.loss)
        self.prediction_graph = g.Graph(self.predictions)
Пример #5
0
    def __init__(self, features_count, *, lambd=0, keep_prob=1):
        hidden_layers_sizes = [20, 3]

        self.m = Dim.unknown()
        self.n = features_count
        self.L = len(hidden_layers_sizes)

        self.X = f.placeholder(shape=(self.n, self.m), dtype=np.float64)
        self.Y = f.placeholder(shape=(1, self.m))
        weights, biases = self.build_variables(self.n, hidden_layers_sizes)

        # Cost graph
        network = self.layers(self.X, weights, biases, self.L, keep_prob)
        loss = f.sigmoid_cross_entropy(network, self.Y)
        if lambd > 0:
            loss += f.l2_norm_regularizer(lambd, f.dim(self.X), weights)
        self.cost_graph = g.Graph(loss)

        # Prediction graph
        network = self.layers(self.X, weights, biases, self.L, 1)
        predictions = f.is_greater_than(f.sigmoid(network), 0.5)
        self.prediction_graph = g.Graph(predictions)
Пример #6
0
    def __init__(self, features_count, hidden_layers_sizes=[]):
        self.m = Dim.unknown()
        self.n = features_count
        self.L = len(hidden_layers_sizes)
        self.X = f.placeholder(shape=(self.n, self.m))
        self.Y = f.placeholder(shape=(1, self.m))

        # Hidden
        a = self.X
        a_size = self.n
        for l, h in enumerate(hidden_layers_sizes):
            linear = self.fully_connected_layer(a, a_size, h, l)
            a = f.relu(linear)
            a_size = h

        # Output layer
        linear = self.fully_connected_layer(a, a_size, 1, self.L)
        loss = f.sigmoid_cross_entropy(linear, self.Y)
        predictions = f.is_greater_than(f.sigmoid(linear), 0.5)

        # Computation graphs
        self.cost_graph = Graph(loss)
        self.prediction_graph = Graph(predictions)