예제 #1
0
    def train(self, sequences):
        """Implements optimize(..) from assignment. This trains a hid_to_class.

        Notes:
        * This uses mini-batches of size 100, momentum of 0.9, no weight decay, and no early stopping.

        Args:
            model_shape (tuple) : is the shape of the array of weights.
            gradient_function   : a function that takes parameters <model> and <data> and returns the gradient
                (or approximate gradient in the case of CD-1) of the function that we're maximizing.
                Note the contrast with the loss function that we saw in PA3, which we were minimizing.
                The returned gradient is an array of the same shape as the provided <model> parameter.

        Returns:
            (numpy.array) : matrix of weights of the trained model (hid_to_class)
        """
        self.reset_classifier()
        # calculate the hidden layer representation of the labeled data, rbm_w is input_to_hid
        hidden_representation = logistic(
            np.dot(self.rbm_w, sequences['inputs']))
        momentum_speed = np.zeros(self.model_shape)
        for i, (mini_batch_x, mini_batch_y) in enumerate(
                zip(batches(hidden_representation, self.mini_batch_size),
                    batches(sequences['targets'], self.mini_batch_size))):
            if i >= self.n_iterations:
                break
            self.fit(mini_batch_x, mini_batch_y)
            momentum_speed = self.train_momentum * momentum_speed + self.d_phi_by_d_input_to_class
            self.model += momentum_speed * self.lr_net
예제 #2
0
    def train(self, sequences):
        """Implements optimize(..) from assignment. This trains a hid_to_class.

        Notes:
        * This uses mini-batches of size 100, momentum of 0.9, no weight decay, and no early stopping.

        Args:
            model_shape (tuple) : is the shape of the array of weights.
            gradient_function   : a function that takes parameters <model> and <data> and returns the gradient
                (or approximate gradient in the case of CD-1) of the function that we're maximizing.
                Note the contrast with the loss function that we saw in PA3, which we were minimizing.
                The returned gradient is an array of the same shape as the provided <model> parameter.

        Returns:
            (numpy.array) : matrix of weights of the trained model (hid_to_class)
        """
        self.reset_classifier()
        # calculate the hidden layer representation of the labeled data, rbm_w is input_to_hid
        hidden_representation = logistic(np.dot(self.rbm_w, sequences['inputs']))
        momentum_speed = np.zeros(self.model_shape)
        for i, (mini_batch_x, mini_batch_y) in enumerate(zip(batches(hidden_representation, self.mini_batch_size),
                                                             batches(sequences['targets'], self.mini_batch_size))):
            if i >= self.n_iterations:
                break
            self.fit(mini_batch_x, mini_batch_y)
            momentum_speed = self.train_momentum * momentum_speed + self.d_phi_by_d_input_to_class
            self.model += momentum_speed * self.lr_net
예제 #3
0
    def train(self, sequences):
        """Implements optimize(..) from assignment. This trains a model that's defined by a single matrix of weights.

        Notes:
        * This uses mini-batches of size 100, momentum of 0.9, no weight decay, and no early stopping.

        Args:
            model_shape (tuple) : is the shape of the array of weights.
            gradient_function   : a function that takes parameters <model> and <data> and returns the gradient
                (or approximate gradient in the case of CD-1) of the function that we're maximizing.
                Note the contrast with the loss function that we saw in PA3, which we were minimizing.
                The returned gradient is an array of the same shape as the provided <model> parameter.

        Returns:
            numpy.array : matrix of weights of the trained model
        """
        self.reset_classifier()
        momentum_speed = np.zeros(self.model_shape)
        for i, mini_batch_x in enumerate(
                batches(sequences['inputs'], self.mini_batch_size)):
            if i >= self.n_iterations:
                break
            self.fit(mini_batch_x)
            momentum_speed = self.train_momentum * momentum_speed + self.gradient
            self.rbm_w += momentum_speed * self.lr_rbm
예제 #4
0
    def train(self, sequences):
        """Implements optimize(..) from assignment. This trains a model that's defined by a single matrix of weights.

        Notes:
        * This uses mini-batches of size 100, momentum of 0.9, no weight decay, and no early stopping.

        Args:
            model_shape (tuple) : is the shape of the array of weights.
            gradient_function   : a function that takes parameters <model> and <data> and returns the gradient
                (or approximate gradient in the case of CD-1) of the function that we're maximizing.
                Note the contrast with the loss function that we saw in PA3, which we were minimizing.
                The returned gradient is an array of the same shape as the provided <model> parameter.

        Returns:
            numpy.array : matrix of weights of the trained model
        """
        self.reset_classifier()
        momentum_speed = np.zeros(self.model_shape)
        for i, mini_batch_x in enumerate(batches(sequences['inputs'], self.mini_batch_size)):
            if i >= self.n_iterations:
                break
            self.fit(mini_batch_x)
            momentum_speed = self.train_momentum * momentum_speed + self.gradient
            self.rbm_w += momentum_speed * self.lr_rbm