Exemplo n.º 1
0
 def step(hiddens, x):
     gsn = GSN(inputs_hook=(28 * 28, x),
               hiddens_hook=(gsn_hiddens, hiddens),
               params_hook=(gsn_params),
               **gsn_args)
     # return the reconstruction and cost!
     return gsn.get_outputs(), gsn.get_train_cost()
Exemplo n.º 2
0
 def step(hiddens, x):
     gsn = GSN(
         inputs_hook=(28*28, x),
         hiddens_hook=(gsn_hiddens, hiddens),
         params_hook=(gsn_params),
         **gsn_args
     )
     # return the reconstruction and cost!
     return gsn.get_outputs(), gsn.get_train_cost()
Exemplo n.º 3
0
    def recurrent_step(self, x_t, u_tm1):
        """
        Performs one timestep for recurrence.

        Parameters
        ----------
        x_t : tensor
            The input at time t.
        u_tm1 : tensor
            The previous timestep (t-1) recurrent hiddens.

        Returns
        -------
        tuple
            Current generated visible x_t and recurrent u_t if generating (no x_t given as parameter),
            otherwise current recurrent u_t and hiddens h_t.
        """
        # If `x_t` is given, deterministic recurrence to compute the u_t. Otherwise, first generate.
        # Make current guess for hiddens based on U
        h_list = []
        for i in range(self.layers):
            if i % 2 == 0:
                log.debug("Using {0!s} and {1!s}".format(
                    self.recurrent_to_gsn_weights_list[(i+1) / 2], self.bias_list[i+1]))
                h = T.dot(u_tm1, self.recurrent_to_gsn_weights_list[(i+1) / 2]) + self.bias_list[i+1]
                h = self.hidden_activation_func(h)
                h_list.append(h)
        h_t = T.concatenate(h_list, axis=0)

        generate = x_t is None
        if generate:
            h_list_generate = [T.shape_padleft(h) for h in h_list]
            # create a GSN to generate x_t
            assert self.input_size, self
            gsn = GSN(
                inputs_hook        = (self.input_size, self.input),
                input_size         = self.input_size,
                hiddens_hook       = (self.hidden_size, T.concatenate(h_list_generate, axis=1)),
                params_hook        = self.gsn_params,
                outdir             = os.path.join(self.outdir, 'gsn_generate/'),
                layers             = self.layers,
                walkbacks          = self.walkbacks,
                visible_activation = self.visible_activation_func,
                hidden_activation  = self.hidden_activation_func,
                input_sampling     = self.input_sampling,
                mrg                = self.mrg,
                tied_weights       = self.tied_weights,
                cost_function      = self.cost_function,
                cost_args          = self.cost_args,
                add_noise          = self.add_noise,
                noiseless_h1       = self.noiseless_h1,
                hidden_noise       = self.hidden_noise,
                hidden_noise_level = self.hidden_noise_level,
                input_noise        = self.input_noise,
                input_noise_level  = self.input_noise_level,
                noise_decay        = self.noise_decay,
                noise_annealing    = self.noise_annealing,
                image_width        = self.image_width,
                image_height       = self.image_height
            )

            x_t = gsn.get_outputs().flatten()

        ua_t = T.dot(x_t, self.W_x_u) + T.dot(u_tm1, self.W_u_u) + self.recurrent_bias
        u_t = self.rnn_hidden_activation_func(ua_t)
        return [x_t, u_t] if generate else [u_t, h_t]
Exemplo n.º 4
0
    def _build_rnngsn(self):
        """
        Creates the updates and other return variables for the computation graph.

        Returns
        -------
        List
            the sample at the end of the computation graph, the train cost function, the train monitors,
            the computation updates, the generated visible list, the generated computation updates, the ending
            recurrent states
        """
        # For training, the deterministic recurrence is used to compute all the
        # {h_t, 1 <= t <= T} given Xs. Conditional GSNs can then be trained
        # in batches using those parameters.
        (u, h_ts), updates_train = theano.scan(fn=lambda x_t, u_tm1: self.recurrent_step(x_t, u_tm1),
                                               sequences=self.input,
                                               outputs_info=[self.u0, None],
                                               name="rnngsn_computation_scan")

        h_list = [T.zeros_like(self.input)]
        for layer, w in enumerate(self.weights_list[:self.layers]):
            if layer % 2 != 0:
                h_list.append(T.zeros_like(T.dot(h_list[-1], w)))
            else:
                h_list.append((h_ts.T[(layer/2) * self.hidden_size:(layer/2 + 1) * self.hidden_size]).T)

        gsn = GSN(
            inputs_hook        = (self.input_size, self.input),
            input_size         = self.input_size,
            hiddens_hook       = (self.hidden_size, GSN.pack_hiddens(h_list)),
            params_hook        = self.gsn_params,
            outdir             = os.path.join(self.outdir, 'gsn_noisy/'),
            layers             = self.layers,
            walkbacks          = self.walkbacks,
            visible_activation = self.visible_activation_func,
            hidden_activation  = self.hidden_activation_func,
            input_sampling     = self.input_sampling,
            mrg                = self.mrg,
            tied_weights       = self.tied_weights,
            cost_function      = self.cost_function,
            cost_args          = self.cost_args,
            add_noise          = self.add_noise,
            noiseless_h1       = self.noiseless_h1,
            hidden_noise       = self.hidden_noise,
            hidden_noise_level = self.hidden_noise_level,
            input_noise        = self.input_noise,
            input_noise_level  = self.input_noise_level,
            noise_decay        = self.noise_decay,
            noise_annealing    = self.noise_annealing,
            image_width        = self.image_width,
            image_height       = self.image_height
        )

        cost = gsn.get_train_cost()
        monitors = gsn.get_monitors()  # frame-level error would be the 'mse' monitor from GSN
        x_sample_recon = gsn.get_outputs()

        # symbolic loop for sequence generation
        (x_ts, u_ts), updates_generate = theano.scan(lambda u_tm1: self.recurrent_step(None, u_tm1),
                                                     outputs_info=[None, self.generate_u0],
                                                     n_steps=self.n_steps,
                                                     name="rnngsn_generate_scan")

        return x_sample_recon, cost, monitors, updates_train, x_ts, updates_generate, u_ts[-1]
Exemplo n.º 5
0
    def recurrent_step(self, x_t, u_tm1):
        """
        Performs one timestep for recurrence.

        Parameters
        ----------
        x_t : tensor
            The input at time t.
        u_tm1 : tensor
            The previous timestep (t-1) recurrent hiddens.

        Returns
        -------
        tuple
            Current generated visible x_t and recurrent u_t if generating (no x_t given as parameter),
            otherwise current recurrent u_t and hiddens h_t.
        """
        # If `x_t` is given, deterministic recurrence to compute the u_t. Otherwise, first generate.
        # Make current guess for hiddens based on U
        h_list = []
        for i in range(self.layers):
            if i % 2 == 0:
                log.debug("Using {0!s} and {1!s}".format(
                    self.recurrent_to_gsn_weights_list[(i+1) / 2], self.bias_list[i+1]))
                h = T.dot(u_tm1, self.recurrent_to_gsn_weights_list[(i+1) / 2]) + self.bias_list[i+1]
                h = self.hidden_activation_func(h)
                h_list.append(h)
        h_t = T.concatenate(h_list, axis=0)

        generate = x_t is None
        if generate:
            h_list_generate = [T.shape_padleft(h) for h in h_list]
            # create a GSN to generate x_t
            gsn = GSN(
                inputs_hook        = (self.input_size, self.input),
                hiddens_hook       = (self.hidden_size, T.concatenate(h_list_generate, axis=1)),
                params_hook        = self.gsn_params,
                outdir             = os.path.join(self.outdir, 'gsn_generate/'),
                layers             = self.layers,
                walkbacks          = self.walkbacks,
                visible_activation = self.visible_activation_func,
                hidden_activation  = self.hidden_activation_func,
                input_sampling     = self.input_sampling,
                mrg                = self.mrg,
                tied_weights       = self.tied_weights,
                cost_function      = self.cost_function,
                cost_args          = self.cost_args,
                add_noise          = self.add_noise,
                noiseless_h1       = self.noiseless_h1,
                hidden_noise       = self.hidden_noise,
                hidden_noise_level = self.hidden_noise_level,
                input_noise        = self.input_noise,
                input_noise_level  = self.input_noise_level,
                noise_decay        = self.noise_decay,
                noise_annealing    = self.noise_annealing,
                image_width        = self.image_width,
                image_height       = self.image_height
            )

            x_t = gsn.get_outputs().flatten()

        ua_t = T.dot(x_t, self.W_x_u) + T.dot(u_tm1, self.W_u_u) + self.recurrent_bias
        u_t = self.rnn_hidden_activation_func(ua_t)
        return [x_t, u_t] if generate else [u_t, h_t]
Exemplo n.º 6
0
    def _build_rnngsn(self):
        """
        Creates the updates and other return variables for the computation graph.

        Returns
        -------
        List
            the sample at the end of the computation graph, the train cost function, the train monitors,
            the computation updates, the generated visible list, the generated computation updates, the ending
            recurrent states
        """
        # For training, the deterministic recurrence is used to compute all the
        # {h_t, 1 <= t <= T} given Xs. Conditional GSNs can then be trained
        # in batches using those parameters.
        (u, h_ts), updates_train = theano.scan(fn=lambda x_t, u_tm1: self.recurrent_step(x_t, u_tm1),
                                               sequences=self.input,
                                               outputs_info=[self.u0, None],
                                               name="rnngsn_computation_scan")

        h_list = [T.zeros_like(self.input)]
        for layer, w in enumerate(self.weights_list[:self.layers]):
            if layer % 2 != 0:
                h_list.append(T.zeros_like(T.dot(h_list[-1], w)))
            else:
                h_list.append((h_ts.T[(layer/2) * self.hidden_size:(layer/2 + 1) * self.hidden_size]).T)

        gsn = GSN(
            inputs_hook        = (self.input_size, self.input),
            hiddens_hook       = (self.hidden_size, GSN.pack_hiddens(h_list)),
            params_hook        = self.gsn_params,
            outdir             = os.path.join(self.outdir, 'gsn_noisy/'),
            layers             = self.layers,
            walkbacks          = self.walkbacks,
            visible_activation = self.visible_activation_func,
            hidden_activation  = self.hidden_activation_func,
            input_sampling     = self.input_sampling,
            mrg                = self.mrg,
            tied_weights       = self.tied_weights,
            cost_function      = self.cost_function,
            cost_args          = self.cost_args,
            add_noise          = self.add_noise,
            noiseless_h1       = self.noiseless_h1,
            hidden_noise       = self.hidden_noise,
            hidden_noise_level = self.hidden_noise_level,
            input_noise        = self.input_noise,
            input_noise_level  = self.input_noise_level,
            noise_decay        = self.noise_decay,
            noise_annealing    = self.noise_annealing,
            image_width        = self.image_width,
            image_height       = self.image_height
        )

        cost = gsn.get_train_cost()
        monitors = gsn.get_monitors()  # frame-level error would be the 'mse' monitor from GSN
        x_sample_recon = gsn.get_outputs()

        # symbolic loop for sequence generation
        (x_ts, u_ts), updates_generate = theano.scan(lambda u_tm1: self.recurrent_step(None, u_tm1),
                                                     outputs_info=[None, self.generate_u0],
                                                     n_steps=self.n_steps,
                                                     name="rnngsn_generate_scan")

        return x_sample_recon, cost, monitors, updates_train, x_ts, updates_generate, u_ts[-1]