def fit(<EOL>self,<EOL>img_data,<EOL>save_freq=-<NUM_LIT:1>,<EOL>pic_freq=-<NUM_LIT:1>,<EOL>n_epochs=<NUM_LIT:100>,<EOL>batch_size=<NUM_LIT:50>,<EOL>weight_decay=True,<EOL>model_path='<STR_LIT>',<EOL>img_path='<STR_LIT>',<EOL>img_out_width=<NUM_LIT:10><EOL>):
width = img_out_width<EOL>self.opt.setup(self.model)<EOL>if weight_decay:<EOL><INDENT>self.opt.add_hook(chainer.optimizer.WeightDecay(<NUM_LIT>))<EOL><DEDENT>n_data = img_data.shape[<NUM_LIT:0>]<EOL>batch_iter = list(range(<NUM_LIT:0>, n_data, batch_size))<EOL>n_batches = len(batch_iter)<EOL>save_counter = <NUM_LIT:0><EOL>for epoch in range(<NUM_LIT:1>, n_epochs + <NUM_LIT:1>):<EOL><INDENT>print('<STR_LIT>' % epoch)<EOL>t1 = time.time()<EOL>indexes = np.random.permutation(n_data)<EOL>last_loss_kl = <NUM_LIT:0.><EOL>last_loss_rec = <NUM_LIT:0.><EOL>count = <NUM_LIT:0><EOL>for i in tqdm.tqdm(batch_iter):<EOL><INDENT>x_batch = Variable(img_data[indexes[i: i + batch_size]])<EOL>if self.flag_gpu:<EOL><INDENT>x_batch.to_gpu()<EOL><DEDENT>out, kl_loss, rec_loss = self.model.forward(x_batch)<EOL>total_loss = rec_loss + kl_loss*self.kl_ratio<EOL>self.opt.zero_grads()<EOL>total_loss.backward()<EOL>self.opt.update()<EOL>last_loss_kl += kl_loss.data<EOL>last_loss_rec += rec_loss.data<EOL>plot_pics = Variable(img_data[indexes[:width]])<EOL>count += <NUM_LIT:1><EOL>if pic_freq > <NUM_LIT:0>:<EOL><INDENT>assert type(pic_freq) == int, "<STR_LIT>"<EOL>if count % pic_freq == <NUM_LIT:0>:<EOL><INDENT>fig = self._plot_img(<EOL>plot_pics,<EOL>img_path=img_path,<EOL>epoch=epoch<EOL>)<EOL>display(fig)<EOL><DEDENT><DEDENT><DEDENT>if save_freq > <NUM_LIT:0>:<EOL><INDENT>save_counter += <NUM_LIT:1><EOL>assert type(save_freq) == int, "<STR_LIT>"<EOL>if epoch % save_freq == <NUM_LIT:0>:<EOL><INDENT>name = "<STR_LIT>" % str(epoch)<EOL>if save_counter == <NUM_LIT:1>:<EOL><INDENT>save_meta = True<EOL><DEDENT>else:<EOL><INDENT>save_meta = False<EOL><DEDENT>self.save(model_path, name, save_meta=save_meta)<EOL>fig = self._plot_img(<EOL>plot_pics,<EOL>img_path=img_path,<EOL>epoch=epoch,<EOL>batch=n_batches,<EOL>save=True<EOL>)<EOL><DEDENT><DEDENT>msg = "<STR_LIT>"<EOL>print(msg.format(last_loss_rec/n_batches, last_loss_kl/n_batches))<EOL>t_diff = time.time()-t1<EOL>print("<STR_LIT>" % t_diff)<EOL><DEDENT>
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.