def fit(
        self,
        img_data,
        save_freq=-1,
        pic_freq=-1,

        n_epochs=100,
        batch_size=50,
        weight_decay=True,
        model_path='./VAE_training_model/',
        img_path='./VAE_training_images/',
        img_out_width=10
    ):

        '''Fit the VAE model to the image data.

        Parameters
        ----------

        img_data : array-like shape (n_images, n_colors, image_width, image_height)
            Images used to fit VAE model.
        save_freq [optional] : int
            Sets the number of epochs to wait before saving the model and optimizer states.
            Also saves image files of randomly generated images using those states in a
            separate directory. Does not save if negative valued.
        pic_freq [optional] : int
            Sets the number of batches to wait before displaying a picture or randomly
            generated images using the current model state.
            Does not display if negative valued.
        n_epochs [optional] : int
            Gives the number of training epochs to run through for the fitting
            process.
        batch_size [optional] : int
            The size of the batch to use when training. Note: generally larger
            batch sizes will result in fater epoch iteration, but at the const
            of lower granulatity when updating the layer weights.
        weight_decay [optional] : bool
            Flag that controls adding weight decay hooks to the optimizer.
        model_path [optional] : str
            Directory where the model and optimizer state files will be saved.
        img_path [optional] : str
            Directory where the end of epoch training image files will be saved.
        img_out_width : int
            Controls the number of randomly genreated images per row in the output
            saved imags.
        '''
        width = img_out_width
        self.opt.setup(self.model)

        if weight_decay:
            self.opt.add_hook(chainer.optimizer.WeightDecay(0.00001))

        n_data = img_data.shape[0]

        batch_iter = list(range(0, n_data, batch_size))
        n_batches = len(batch_iter)
        save_counter = 0
        for epoch in range(1, n_epochs + 1):
            print('epoch: %i' % epoch)
            t1 = time.time()
            indexes = np.random.permutation(n_data)
            last_loss_kl = 0.
            last_loss_rec = 0.
            count = 0
            for i in tqdm.tqdm(batch_iter):

                x_batch = Variable(img_data[indexes[i: i + batch_size]])

                if self.flag_gpu:
                    x_batch.to_gpu()

                out, kl_loss, rec_loss = self.model.forward(x_batch)
                total_loss = rec_loss + kl_loss*self.kl_ratio

                self.opt.zero_grads()
                total_loss.backward()
                self.opt.update()

                last_loss_kl += kl_loss.data
                last_loss_rec += rec_loss.data
                plot_pics = Variable(img_data[indexes[:width]])
                count += 1
                if pic_freq > 0:
                    assert type(pic_freq) == int, "pic_freq must be an integer."
                    if count % pic_freq == 0:
                        fig = self._plot_img(
                            plot_pics,
                            img_path=img_path,
                            epoch=epoch
                        )
                        display(fig)

            if save_freq > 0:
                save_counter += 1
                assert type(save_freq) == int, "save_freq must be an integer."
                if epoch % save_freq == 0:
                    name = "vae_epoch%s" % str(epoch)
                    if save_counter == 1:
                        save_meta = True
                    else:
                        save_meta = False
                    self.save(model_path, name, save_meta=save_meta)
                    fig = self._plot_img(
                        plot_pics,
                        img_path=img_path,
                        epoch=epoch,
                        batch=n_batches,
                        save=True
                        )

            msg = "rec_loss = {0} , kl_loss = {1}"
            print(msg.format(last_loss_rec/n_batches, last_loss_kl/n_batches))
            t_diff = time.time()-t1
            print("time: %f\n\n" % t_diff)
def fit ( self , img_data , save_freq = - 1 , pic_freq = - 1 , n_epochs = 100 , batch_size = 50 , weight_decay = True , model_path = './VAE_training_model/' , img_path = './VAE_training_images/' , img_out_width = 10 ) : width = img_out_width self . opt . setup ( self . model ) if weight_decay : self . opt . add_hook ( chainer . optimizer . WeightDecay ( 0.00001 ) ) n_data = img_data . shape [ 0 ] batch_iter = list ( range ( 0 , n_data , batch_size ) ) n_batches = len ( batch_iter ) save_counter = 0 for epoch in range ( 1 , n_epochs + 1 ) : print ( 'epoch: %i' % epoch ) t1 = time . time ( ) indexes = np . random . permutation ( n_data ) last_loss_kl = 0. last_loss_rec = 0. count = 0 for i in tqdm . tqdm ( batch_iter ) : x_batch = Variable ( img_data [ indexes [ i : i + batch_size ] ] ) if self . flag_gpu : x_batch . to_gpu ( ) out , kl_loss , rec_loss = self . model . forward ( x_batch ) total_loss = rec_loss + kl_loss * self . kl_ratio self . opt . zero_grads ( ) total_loss . backward ( ) self . opt . update ( ) last_loss_kl += kl_loss . data last_loss_rec += rec_loss . data plot_pics = Variable ( img_data [ indexes [ : width ] ] ) count += 1 if pic_freq > 0 : assert type ( pic_freq ) == int , "pic_freq must be an integer." if count % pic_freq == 0 : fig = self . _plot_img ( plot_pics , img_path = img_path , epoch = epoch ) display ( fig ) if save_freq > 0 : save_counter += 1 assert type ( save_freq ) == int , "save_freq must be an integer." if epoch % save_freq == 0 : name = "vae_epoch%s" % str ( epoch ) if save_counter == 1 : save_meta = True else : save_meta = False self . save ( model_path , name , save_meta = save_meta ) fig = self . _plot_img ( plot_pics , img_path = img_path , epoch = epoch , batch = n_batches , save = True ) msg = "rec_loss = {0} , kl_loss = {1}" print ( msg . format ( last_loss_rec / n_batches , last_loss_kl / n_batches ) ) t_diff = time . time ( ) - t1 print ( "time: %f\n\n" % t_diff )
Fit the VAE model to the image data.

        Parameters
        ----------

        img_data : array-like shape (n_images, n_colors, image_width, image_height)
            Images used to fit VAE model.
        save_freq [optional] : int
            Sets the number of epochs to wait before saving the model and optimizer states.
            Also saves image files of randomly generated images using those states in a
            separate directory. Does not save if negative valued.
        pic_freq [optional] : int
            Sets the number of batches to wait before displaying a picture or randomly
            generated images using the current model state.
            Does not display if negative valued.
        n_epochs [optional] : int
            Gives the number of training epochs to run through for the fitting
            process.
        batch_size [optional] : int
            The size of the batch to use when training. Note: generally larger
            batch sizes will result in fater epoch iteration, but at the const
            of lower granulatity when updating the layer weights.
        weight_decay [optional] : bool
            Flag that controls adding weight decay hooks to the optimizer.
        model_path [optional] : str
            Directory where the model and optimizer state files will be saved.
        img_path [optional] : str
            Directory where the end of epoch training image files will be saved.
        img_out_width : int
            Controls the number of randomly genreated images per row in the output
            saved imags.