Пример #1
0
    def __init__(self, l2_reg=1, step_size=.005, max_num_epochs=5000):
        self.max_num_epochs = max_num_epochs
        self.step_size = step_size
        # Build computation graph
        self.x = nodes.ValueNode(node_name="x")  # to hold a vector input
        self.y = nodes.ValueNode(node_name="y")  # to hold a scalar response
        self.w = nodes.ValueNode(node_name="w")  # to hold the parameter vector
        self.b = nodes.ValueNode(
            node_name="b")  # to hold the bias parameter (scalar)
        self.l2_reg = nodes.ValueNode(
            node_name="l2_reg")  # to hold the reg parameter (scalar)
        self.prediction = nodes.VectorScalarAffineNode(x=self.x,
                                                       w=self.w,
                                                       b=self.b,
                                                       node_name="prediction")
        self.squareloss = nodes.SquaredL2DistanceNode(a=self.prediction,
                                                      b=self.y,
                                                      node_name="square loss")
        self.l2penalty = nodes.L2NormPenaltyNode(l2_reg=l2_reg,
                                                 w=self.w,
                                                 node_name="l2 penalty")
        self.objective = nodes.SumNode(a=self.squareloss,
                                       b=self.l2penalty,
                                       node_name="objective")

        # Group nodes into types to construct computation graph function
        self.inputs = [self.x]
        self.outcomes = [self.y]
        self.parameters = [self.w, self.b]

        self.graph = graph.ComputationGraphFunction(self.inputs, self.outcomes,
                                                    self.parameters,
                                                    self.prediction,
                                                    self.objective)
Пример #2
0
    def __init__(self, l2_reg=1, step_size=.005,  max_num_epochs = 5000):
        self.max_num_epochs = max_num_epochs
        self.step_size = step_size

        # Build computation graph
        self.x = nodes.ValueNode(node_name="x") # to hold a vector input
        self.y = nodes.ValueNode(node_name="y") # to hold a scalar response
        self.w = nodes.ValueNode(node_name="w") # to hold the parameter vector
        self.b = nodes.ValueNode(node_name="b") # to hold the bias parameter (scalar)
        self.prediction = nodes.VectorScalarAffineNode(x=self.x, 
                                                       w=self.w, 
                                                       b=self.b,
                                                       node_name="prediction")
        
        # Square Loss
        self.square_loss = nodes.SquaredL2DistanceNode(a = self.prediction,
                                                       b = self.y,
                                                       node_name = "square_loss")
        # L2 Regularization Loss
        self.l2_reg_loss = nodes.L2NormPenaltyNode(l2_reg = l2_reg,
                                                   w = self.w,
                                                   node_name = "l2_reg_loss")

        # Total Loss
        self.total_loss = nodes.SumNode(a=self.square_loss,
                                        b=self.l2_reg_loss,
                                        node_name="total_loss")

        # Computational Graph
        self.graph = graph.ComputationGraphFunction(inputs=[self.x],
                                                    outcomes=[self.y],
                                                    parameters=[self.w,self.b],
                                                    prediction=self.y,
                                                    objective=self.total_loss)       
    def __init__(self, l2_reg=1, step_size=.005,\
                 max_num_epochs = 5000):
        self.max_num_epochs = max_num_epochs
        self.step_size = step_size

        # Build computation graph
        self.x = nodes.ValueNode(node_name="x") 
        self.y = nodes.ValueNode(node_name="y") 
        self.w = nodes.ValueNode(node_name="w") 
        self.b = nodes.ValueNode(node_name="b") 
        self.prediction = nodes.VectorScalarAffineNode(x=self.x, 
                                                       w=self.w, 
                                                       b=self.b,
                                                       node_name="prediction")

        self.square_loss = nodes.SquaredL2DistanceNode(a = self.prediction,
                                                       b = self.y,
                                                       node_name = "square_loss")

        self.l2_reg_loss = nodes.L2NormPenaltyNode(l2_reg = l2_reg,
                                                   w = self.w,
                                                   node_name = "l2_reg_loss")

        self.total_loss = nodes.SumNode(a=self.square_loss,
                                        b=self.l2_reg_loss,
                                        node_name="total_loss")

        self.graph = graph.ComputationGraphFunction(inputs=[self.x],
                                                    outcomes=[self.y],
                                                    parameters=[self.w,self.b],
                                                    prediction=self.prediction,
                                                    objective=self.total_loss)       
 def test_L2NormPenaltyNode(self):
     max_allowed_rel_err = 1e-5
     l2_reg = np.array(4.0)
     w = nodes.ValueNode("w")
     l2_norm_node = nodes.L2NormPenaltyNode(l2_reg, w, "l2 norm node")
     d = (5)
     init_vals = {"w":np.array(np.random.standard_normal(d))}
     max_rel_err = test_utils.test_node_backward(l2_norm_node, init_vals, delta=1e-7)
     self.assertTrue(max_rel_err < max_allowed_rel_err)
    def __init__(self, l2_reg=1, step_size=.005,  max_num_epochs = 5000):
        self.max_num_epochs = max_num_epochs
        self.step_size = step_size

        # Build computation graph
        self.x = nodes.ValueNode(node_name="x") # to hold a vector input
        self.y = nodes.ValueNode(node_name="y") # to hold a scalar response
        self.w = nodes.ValueNode(node_name="w") # to hold the parameter vector
        self.b = nodes.ValueNode(node_name="b") # to hold the bias parameter (scalar)
        self.prediction = nodes.VectorScalarAffineNode(x=self.x, w=self.w, b=self.b,
                                                 node_name="prediction")
        
        self.objective = nodes.SquaredL2DistanceNode(a=self.prediction, b=self.y,
                        node_name="square loss") + 
                        nodes.L2NormPenaltyNode(l2_reg=self.l2_reg, w=self.w,
                                                node_name="l2 penalty")
Пример #6
0
    def __init__(
        self,
        n_hidden_units=10,
        l2_reg=0,
        step_size=0.005,
        init_param_scale=0.01,
        max_num_epochs=5000,
    ):
        self.n_hidden_units = n_hidden_units
        self.init_param_scale = 0.01
        self.max_num_epochs = max_num_epochs
        self.step_size = step_size
        self.l2_reg = l2_reg

        # Build computation graph
        self.x = nodes.ValueNode(node_name="x")  # to hold a vector input
        self.y = nodes.ValueNode(node_name="y")  # to hold a scalar response
        self.W1 = nodes.ValueNode(
            node_name="W1")  # to hold the parameter matrix
        self.W2 = nodes.ValueNode(
            node_name="W2")  # to hold the parameter vector
        self.b1 = nodes.ValueNode(
            node_name="b1")  # to hold the bias parameter (vector)
        self.b2 = nodes.ValueNode(
            node_name="b2")  # to hold the bias parameter (scalar)

        f1 = nodes.AffineNode(x=self.x,
                              W=self.W1,
                              b=self.b1,
                              node_name="Hidden Layer")
        a1 = nodes.TanhNode(a=f1, node_name="Hidden Activation")
        self.prediction = nodes.VectorScalarAffineNode(x=a1,
                                                       w=self.W2,
                                                       b=self.b2,
                                                       node_name="Output")

        data_loss = nodes.SquaredL2DistanceNode(a=self.prediction,
                                                b=self.y,
                                                node_name="Data Loss")
        reg_loss1 = nodes.L2NormPenaltyNode(l2_reg=self.l2_reg,
                                            w=self.W1,
                                            node_name="W1 Decay")
        reg_loss2 = nodes.L2NormPenaltyNode(l2_reg=self.l2_reg,
                                            w=self.W2,
                                            node_name="W2 Decay")
        total_reg_loss = nodes.SumNode(a=reg_loss1,
                                       b=reg_loss2,
                                       node_name="Regularization Loss")

        self.objective = nodes.SumNode(a=data_loss,
                                       b=total_reg_loss,
                                       node_name="Total Loss")

        self.inputs = [self.x]
        self.outcomes = [self.y]
        self.parameters = [self.W1, self.W2, self.b1, self.b2]

        self.graph = graph.ComputationGraphFunction(self.inputs, self.outcomes,
                                                    self.parameters,
                                                    self.prediction,
                                                    self.objective)