{ "info": { "author": "chimera0", "author_email": "ai-brain-lab@accel-brain.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Framework :: Robot Framework", "Intended Audience :: Information Technology", "Intended Audience :: Science/Research", "License :: OSI Approved :: GNU General Public License v2 (GPLv2)", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering :: Artificial Intelligence" ], "description": "# Generative Adversarial Networks Library: pygan\n\n`pygan` is Python library to implement Generative Adversarial Networks(GANs), *Conditional* GANs, Adversarial Auto-Encoders(AAEs), and Energy-based Generative Adversarial Network(EBGAN).\n\nThis library makes it possible to design the Generative models based on the Statistical machine learning problems in relation to Generative Adversarial Networks(GANs), *Conditional* GANs, Adversarial Auto-Encoders(AAEs), and Energy-based Generative Adversarial Network(EBGAN) to practice algorithm design for semi-supervised learning. But this library provides components for designers, not for end-users of state-of-the-art black boxes. Briefly speaking the philosophy of this library, *give user hype-driven blackboxes and you feed him for a day; show him how to design algorithms and you feed him for a lifetime.* So algorithm is power.\n\nSee also ...\n\n- [Algorithmic Composition or Automatic Composition Library: pycomposer](https://github.com/chimera0/accel-brain-code/tree/master/Algorithmic-Composition)\n * If you want to implement the Algorithmic Composer based on Generative Adversarial Networks(GANs) and the *Conditional* GANs by using `pygan` as components for Generative models based on the Statistical machine learning problems.\n\n## Installation\n\nInstall using pip:\n\n```sh\npip install pygan\n```\n\n### Source code\n\nThe source code is currently hosted on GitHub.\n\n- [accel-brain-code/Generative-Adversarial-Networks](https://github.com/chimera0/accel-brain-code/tree/master/Generative-Adversarial-Networks)\n\n### Python package index(PyPI)\n\nInstallers for the latest released version are available at the Python package index.\n\n- [pygan : Python Package Index](https://pypi.python.org/pypi/pygan/)\n\n### Dependencies\n\n- numpy: v1.13.3 or higher.\n\n#### Option\n\n- [pydbm](https://github.com/chimera0/accel-brain-code/tree/master/Deep-Learning-by-means-of-Design-Pattern): v1.4.9 or higher.\n * Only if you want to implement the components based on this library.\n\n## Documentation\n\nFull documentation is available on [https://code.accel-brain.com/Generative-Adversarial-Networks/](https://code.accel-brain.com/Generative-Adversarial-Networks/) . This document contains information on functionally reusability, functional scalability and functional extensibility.\n\n## Description\n\n`pygan` is Python library to implement Generative Adversarial Networks(GANs), *Conditional* GANs, Adversarial Auto-Encoders(AAEs), and Energy-based Generative Adversarial Network(EBGAN).\n\nThe Generative Adversarial Networks(GANs) (Goodfellow et al., 2014) framework establishes a\nmin-max adversarial game between two neural networks \u2013 a generative model, `G`, and a discriminative\nmodel, `D`. The discriminator model, `D(x)`, is a neural network that computes the probability that\na observed data point `x` in data space is a sample from the data distribution (positive samples) that we are trying to model, rather than a sample from our generative model (negative samples). Concurrently, the generator uses a function `G(z)` that maps samples `z` from the prior `p(z)` to the data space. `G(z)` is trained to maximally confuse the discriminator into believing that samples it generates come from the data distribution. The generator is trained by leveraging the gradient of `D(x)` w.r.t. `x`, and using that to modify its parameters.\n\n### Structural extension for *Conditional* GANs (or cGANs).\n\nThe *Conditional* GANs (or cGANs) is a simple extension of the basic GAN model which allows the model to condition on external information. This makes it possible to engage the learned generative model in different \"modes\" by providing it with different contextual information (Gauthier, J. 2014).\n\nThis model can be constructed by simply feeding the data, `y`, to condition on to both the generator and discriminator. In an unconditioned generative model, because the maps samples `z` from the prior `p(z)` are drawn from uniform or normal distribution, there is no control on modes of the data being generated. On the other hand, it is possible to direct the data generation process by conditioning the model on additional information (Mirza, M., & Osindero, S. 2014).\n\n### Structural extension for Adversarial Auto-Encoders(AAEs).\n\nThis library also provides the Adversarial Auto-Encoders(AAEs), which is a probabilistic Auto-Encoder that uses GANs to perform variational inference by matching the aggregated posterior of the feature points in hidden layer of the Auto-Encoder with an arbitrary prior distribution(Makhzani, A., et al., 2015). Matching the aggregated posterior to the prior ensures that generating from any part of prior space results in meaningful samples. As a result, the decoder of the Adversarial Auto-Encoder learns a deep generative model that maps the imposed prior to the data distribution.\n\n### Structural extension for Energy-based Generative Adversarial Network(EBGAN).\n\nReusing the Auto-Encoders, this library introduces the Energy-based Generative Adversarial Network (EBGAN) model(Zhao, J., et al., 2016) which views the discriminator as an energy function that attributes low energies to the regions near the data manifold and higher energies to other regions. THe Auto-Encoders have traditionally been used to represent energy-based models. When trained with some regularization terms, the Auto-Encoders have the ability to learn an energy manifold without supervision or negative examples. This means that even when an energy-based Auto-Encoding model is trained to reconstruct a real sample, the model contributes to discovering the data manifold by itself.\n\n### The Commonality/Variability Analysis in order to practice object-oriented design.\n\nFrom perspective of *commonality/variability* analysis in order to practice object-oriented design, the concepts of GANs and AAEs can be organized as follows:\n\n
\n\n
\n\nThe configuration is based on the `Strategy Pattern`, which provides a way to define a family of algorithms implemented by inheriting the interface or abstract class such as `TrueSampler`, `NoiseSampler`, `GenerativeModel`, and `DiscriminativeModel`, where ...\n - `TrueSampler` is the interface to provide `x` by drawing from the `true` distributions.\n - `GenerativeModel` is the abstract class to generate `G(z)` by drawing from the `fake` distributions with `NoiseSampler` which provides `z`.\n - `DiscriminativeModel` is the interface to inference that observed data points are `true` or `fake` as a `D(x)`.\n\nThis pattern is encapsulate each one as an object, and make them interchangeable from the point of view of functionally equivalent. This library provides sub classes such as Neural Networks, Convolutional Neural Networks, and LSTM networks. Althogh those models are variable from the view points of learning algorithms, but as a `GenerativeModel` or a `DiscriminativeModel` those models have common function.\n\n`GenerativeAdversarialNetworks` is a *Context* in the `Strategy Pattern`, controlling the objects of `TrueSampler`, `GenerativeModel`, and `DiscriminativeModel` in order to train `G(z)` and `D(x)`. This *context* class also calls the object of `GANsValueFunction`, whose function is computing the rewards or gradients in GANs framework.\n\nThe structural extension from GANs to AAEs is achieved by the inheritance of two classes: `GenerativeModel` and `GenerativeAdversarialNetworks`. One of the main concepts of AAEs, which is worthy of special mention, can be considered that *the Auto-Encoders can be transformed into the generative Models*. Therefore this library firstly implements a `AutoEncoderModel` by inheriting `GenerativeModel`. Next, this library watches closely that the difference between GANs and AAEs brings us different *context* in the `Strategy Pattern` in relation to the learning algorithm of Auto-Encoders. By the addition of the `AutoEncoderModel`'s learning method, this library provieds `AdversarialAutoEncoders` which is-a `GenerativeAdversarialNetworks` and makes it possible to train not only `GenerativeModel` and `DiscriminativeModel` but also `AutoEncoderModel`.\n\nFurthermore, `FeatureMatching` is a value function with so-called Feature matching technic, which addresses the instability of GANs by specifying a new objective for the generator that prevents it from overtraining on the current discriminator(Salimans, T., et al., 2016).\n\n
\n\n
\n\nLike Yang, L. C., et al. (2017), this library implements the `Conditioner` to conditon on external information. As class configuration in this library, the `Conditioner` is divided into two, `ConditionalGenerativeModel` and `ConditionalTrueSampler`. This library consider that the `ConditionalGenerativeModel` and `ConditionalTrueSampler` contain `Conditioner` of the *Conditional* GANs to reduce the burden of architectural design. The controller `GenerativeAdversarialNetworks` functionally uses the conditions in a black boxed state.\n\n## Usecase: Generating Sine Waves by GANs.\n\nSet hyperparameters.\n\n```python\n# Batch size\nbatch_size = 20\n# The length of sequences.\nseq_len = 30\n# The dimension of observed or feature points.\ndim = 5\n```\n\nImport Python modules.\n\n```python\n# is-a `TrueSampler`.\nfrom pygan.truesampler.sine_wave_true_sampler import SineWaveTrueSampler\n# is-a `NoiseSampler`.\nfrom pygan.noisesampler.uniform_noise_sampler import UniformNoiseSampler\n# is-a `GenerativeModel`.\nfrom pygan.generativemodel.lstm_model import LSTMModel\n# is-a `DiscriminativeModel`.\nfrom pygan.discriminativemodel.nn_model import NNModel\n# is-a `GANsValueFunction`.\nfrom pygan.gansvaluefunction.mini_max import MiniMax\n# GANs framework.\nfrom pygan.generative_adversarial_networks import GenerativeAdversarialNetworks\n```\n\nSetup `TrueSampler`.\n\n```python\ntrue_sampler = SineWaveTrueSampler(\n batch_size=batch_size,\n seq_len=seq_len,\n dim=dim\n)\n```\n\nSetup `NoiseSampler` and `GenerativeModel`.\n\n```python\nnoise_sampler = UniformNoiseSampler(\n # Lower boundary of the output interval.\n low=-1, \n # Upper boundary of the output interval.\n high=1, \n # Output shape.\n output_shape=(batch_size, 1, dim)\n)\n\ngenerative_model = LSTMModel(\n batch_size=batch_size,\n seq_len=seq_len,\n input_neuron_count=dim,\n hidden_neuron_count=dim\n)\ngenerative_model.noise_sampler = noise_sampler\n```\n\nSetup `DiscriminativeModel` with [pydbm](https://github.com/chimera0/accel-brain-code/tree/master/Deep-Learning-by-means-of-Design-Pattern) library.\n\n```python\n# Computation graph for Neural network.\nfrom pydbm.synapse.nn_graph import NNGraph\n# Layer object of Neural network.\nfrom pydbm.nn.nn_layer import NNLayer\n#$ Logistic function or Sigmoid function which is-a `ActivatingFunctionInterface`.\nfrom pydbm.activation.logistic_function import LogisticFunction\n\nnn_layer = NNLayer(\n graph=NNGraph(\n activation_function=LogisticFunction(),\n # The number of units in hidden layer.\n hidden_neuron_count=seq_len * dim,\n # The number of units in output layer.\n output_neuron_count=1\n )\n)\n\ndiscriminative_model = NNModel(\n # `list` of `NNLayer`.\n nn_layer_list=[nn_layer],\n batch_size=batch_size\n)\n```\n\nSetup the value function.\n\n```python\ngans_value_function = MiniMax()\n```\n\nSetup GANs framework.\n\n```python\nGAN = GenerativeAdversarialNetworks(\n gans_value_function=gans_value_function\n)\n```\n\nIf you want to setup GNAs framework with so-called feature matching technic, which is effective in situations where regular GAN becomes unstable(Salimans, T., et al., 2016), setup GANs framework as follows:\n\n```python\nGAN = GenerativeAdversarialNetworks(\n gans_value_function=gans_value_function,\n feature_matching=FeatureMatching(\n # Weight for results of standard feature matching.\n lambda1=0.01, \n # Weight for results of difference between generated data points and true samples.\n lambda2=0.99\n )\n)\n```\n\nwhere `lambda1` and `lambda2` are trade-off parameters. `lambda1` means a weight for results of standard feature matching and `lambda2` means a weight for results of difference between generated data points and true samples(Yang, L. C., et al., 2017).\n\nStart training.\n\n```python\ngenerative_model, discriminative_model = GAN.train(\n true_sampler,\n generative_model,\n discriminative_model,\n # The number of training iterations.\n iter_n=100,\n # The number of learning of the discriminative_model.\n k_step=10\n)\n```\n\n#### Visualization.\n\nCheck the rewards or losses.\n\n```python\nd_logs_list, g_logs_list = GAN.extract_logs_tuple()\n```\n\n`d_logs_list` is a `list` of probabilities inferenced by the `discriminator` (mean) in the `discriminator`'s update turn and `g_logs_list` is a `list` of probabilities inferenced by the `discriminator` (mean) in the `generator`'s update turn.\n\nVisualize the values of `d_logs_list`.\n\n```python\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%config InlineBackend.figure_format = \"retina\"\nplt.style.use(\"fivethirtyeight\")\nplt.figure(figsize=(20, 10))\nplt.plot(d_logs_list)\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.show()\n```\n\n
\n\n
\n\nSimilarly, visualize the values of `g_logs_list`.\n\n```python\nplt.figure(figsize=(20, 10))\nplt.plot(g_logs_list)\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.show()\n```\n\n
\n\n
\n\nAs the training progresses, the values are close to `0.5`.\n\n#### Generation.\n\nPlot a true distribution and generated data points to check how the `discriminator` was *confused* by the `generator`.\n\n```python\ntrue_arr = true_sampler.draw()\n\nplt.style.use(\"fivethirtyeight\")\nplt.figure(figsize=(20, 10))\nplt.plot(true_arr[0])\nplt.show()\n```\n\n
\n\n
\n\n```python\ngenerated_arr = generative_model.draw()\n\nplt.style.use(\"fivethirtyeight\")\nplt.figure(figsize=(20, 10))\nplt.plot(generated_arr[0])\nplt.show()\n```\n\n
\n\n
\n\n### Usecase: Generating images by AAEs.\n\nIn this demonstration, we use image dataset in [the Weizmann horse dataset](https://avaminzhang.wordpress.com/2012/12/07/%E3%80%90dataset%E3%80%91weizmann-horses/). [pydbm](https://github.com/chimera0/accel-brain-code/tree/master/Deep-Learning-by-means-of-Design-Pattern) library used this dataset to demonstrate for [observing reconstruction images by Convolutional Auto-Encoder.](https://github.com/chimera0/accel-brain-code/blob/master/Deep-Learning-by-means-of-Design-Pattern/demo/demo_convolutional_auto_encoder.ipynb) and Shape boltzmann machines as follows.\n\n\n \n \n \n \n \n
\n \n

Image in the Weizmann horse dataset.

\n
\n \n

Reconstructed image by Shape-BM.

\n
\n \n

Reconstructed image by Convolutional Auto-Encoder.

\n
\n\nThis library also provides the Convolutional Auto-Encoder, which can be functionally re-used as `AutoEncoderModel`, loosely coupling with `AdversarialAutoEncoders`.\n\nSet hyperparameters and directory path that stores your image files.\n\n```python\nbatch_size = 20\n# Width of images.\nwidth = 100\n# height of images.\nheight = 100\n# Channel of images.\nchannel = 1\n# Path to your image files.\nimage_dir = \"your/path/to/images/\"\n# The length of sequneces. If `None`, the objects will ignore sequneces.\nseq_len = None\n# Gray scale or not.\ngray_scale_flag = True\n# The tuple of width and height.\nwh_size_tuple = (width, height)\n# How to normalize pixel values of images.\n# - `z_score`: Z-Score normalization.\n# - `min_max`: Min-max normalization.\n# - `tanh`: Normalization by tanh function.\nnorm_mode = \"z_score\"\n```\n\nImport Python modules.\n\n```python\n# is-a `TrueSampler`.\nfrom pygan.truesampler.image_true_sampler import ImageTrueSampler\n# is-a `NoiseSampler`.\nfrom pygan.noisesampler.image_noise_sampler import ImageNoiseSampler\n# is-a `AutoencoderModel`.\nfrom pygan.generativemodel.autoencodermodel.convolutional_auto_encoder import ConvolutionalAutoEncoder as Generator\n# is-a `DiscriminativeModel`.\nfrom pygan.discriminativemodel.cnn_model import CNNModel as Discriminator\n# `AdversarialAutoEncoders` which is-a `GenerativeAdversarialNetworks`.\nfrom pygan.generativeadversarialnetworks.adversarial_auto_encoders import AdversarialAutoEncoders\n# Value function.\nfrom pygan.gansvaluefunction.mini_max import MiniMax\n# Feature Matching.\nfrom pygan.feature_matching import FeatureMatching\n```\n\nImport [pydbm](https://github.com/chimera0/accel-brain-code/tree/master/Deep-Learning-by-means-of-Design-Pattern) modules.\n\n```python\n# Convolution layer.\nfrom pydbm.cnn.layerablecnn.convolution_layer import ConvolutionLayer\n# Computation graph in output layer.\nfrom pydbm.synapse.cnn_output_graph import CNNOutputGraph\n# Computation graph for convolution layer.\nfrom pydbm.synapse.cnn_graph import CNNGraph\n# Logistic Function as activation function.\nfrom pydbm.activation.logistic_function import LogisticFunction\n# Tanh Function as activation function.\nfrom pydbm.activation.tanh_function import TanhFunction\n# ReLu Function as activation function.\nfrom pydbm.activation.relu_function import ReLuFunction\n# SGD optimizer.\nfrom pydbm.optimization.optparams.sgd import SGD\n# Adam optimizer.\nfrom pydbm.optimization.optparams.adam import Adam\n# MSE.\nfrom pydbm.loss.mean_squared_error import MeanSquaredError\n# Convolutional Auto-Encoder.\nfrom pydbm.cnn.convolutionalneuralnetwork.convolutional_auto_encoder import ConvolutionalAutoEncoder as CAE\n# Deconvolution layer.\nfrom pydbm.cnn.layerablecnn.convolutionlayer.deconvolution_layer import DeconvolutionLayer\n# Verification object.\nfrom pydbm.verification.verificate_function_approximation import VerificateFunctionApproximation\n```\n\nSetup `TrueSampler`.\n\n```python\ntrue_sampler = ImageTrueSampler(\n batch_size=batch_size,\n image_dir=image_dir,\n seq_len=seq_len,\n gray_scale_flag=gray_scale_flag,\n wh_size_tuple=wh_size_tuple,\n norm_mode=norm_mode\n)\n```\n\nSetup `NoiseSampler` and `AutoEncoderModel`.\n\n```python\nnoise_sampler = ImageNoiseSampler(\n batch_size,\n image_dir,\n seq_len=seq_len,\n gray_scale_flag=gray_scale_flag,\n wh_size_tuple=wh_size_tuple,\n norm_mode=norm_mode\n)\n\nif gray_scale_flag is True:\n channel = 1\nelse:\n channel = 3\nscale = 0.1\n\nconv1 = ConvolutionLayer(\n CNNGraph(\n activation_function=TanhFunction(),\n # The number of filters.\n filter_num=batch_size,\n channel=channel,\n # Kernel size.\n kernel_size=3,\n scale=scale,\n # The number of strides.\n stride=1,\n # The number of zero-padding.\n pad=1\n )\n)\n\nconv2 = ConvolutionLayer(\n CNNGraph(\n activation_function=TanhFunction(),\n filter_num=batch_size,\n channel=batch_size,\n kernel_size=3,\n scale=scale,\n stride=1,\n pad=1\n )\n)\n\ndeconvolution_layer_list = [\n DeconvolutionLayer(\n CNNGraph(\n activation_function=TanhFunction(),\n filter_num=batch_size,\n channel=channel,\n kernel_size=5,\n scale=scale,\n stride=1,\n pad=1\n )\n )\n]\n\nopt_params = Adam()\n# The probability of dropout.\nopt_params.dropout_rate = 0.0\n\nconvolutional_auto_encoder = CAE(\n layerable_cnn_list=[\n conv1, \n conv2\n ],\n epochs=100,\n batch_size=batch_size,\n learning_rate=1e-05,\n # # Attenuate the `learning_rate` by a factor of this value every `attenuate_epoch`.\n learning_attenuate_rate=0.1,\n # # Attenuate the `learning_rate` by a factor of `learning_attenuate_rate` every `attenuate_epoch`.\n attenuate_epoch=25,\n computable_loss=MeanSquaredError(),\n opt_params=opt_params,\n verificatable_result=VerificateFunctionApproximation(),\n # # Size of Test data set. If this value is `0`, the validation will not be executed.\n test_size_rate=0.3,\n # Tolerance for the optimization.\n # When the loss or score is not improving by at least tol \n # for two consecutive iterations, convergence is considered \n # to be reached and training stops.\n tol=1e-15\n)\n\ngenerator = Generator(\n batch_size=batch_size,\n learning_rate=1e-05,\n convolutional_auto_encoder=convolutional_auto_encoder,\n deconvolution_layer_list=deconvolution_layer_list,\n gray_scale_flag=gray_scale_flag\n)\ngenerator.noise_sampler = noise_sampler\n```\n\nSetup `DiscriminativeModel`.\n\n```python\nconvD = ConvolutionLayer(\n CNNGraph(\n activation_function=TanhFunction(),\n filter_num=batch_size,\n channel=channel,\n kernel_size=3,\n scale=0.001,\n stride=3,\n pad=1\n )\n)\n\nlayerable_cnn_list=[\n convD\n]\n\nopt_params = Adam()\nopt_params.dropout_rate = 0.0\n\ncnn_output_graph = CNNOutputGraph(\n # The number of units in hidden layer.\n hidden_dim=23120, \n # The number of units in output layer.\n output_dim=1, \n activating_function=LogisticFunction(), \n scale=0.01\n)\n\ndiscriminator = Discriminator(\n batch_size=batch_size,\n layerable_cnn_list=layerable_cnn_list,\n cnn_output_graph=cnn_output_graph,\n learning_rate=1e-05,\n opt_params=opt_params\n)\n```\n\nSetup AAEs framework.\n\n```python\nAAE = AdversarialAutoEncoders(\n gans_value_function=MiniMax(),\n feature_matching=FeatureMatching(\n # Weight for results of standard feature matching.\n lambda1=0.01, \n # Weight for results of difference between generated data points and true samples.\n lambda2=0.99\n )\n)\n```\n\nStart pre-training.\n\n```python\ngenerator.pre_learn(true_sampler=true_sampler, epochs=1000)\n```\n\nStart training.\n\n```python\ngenerator, discriminator = AAE.train(\n true_sampler=true_sampler,\n generative_model=generator,\n discriminative_model=discriminator,\n iter_n=1000,\n k_step=5\n)\n```\n\n#### Visualization.\n\nCheck the rewards or losses.\n\n##### Result of pre-training.\n\n```python\nplt.figure(figsize=(20, 10))\nplt.title(\"The reconstruction errors.\")\nplt.plot(generator.pre_loss_arr)\nplt.show()\nplt.close()\n```\n\n
\n\n##### Result of training.\n\n```python\na_logs_list, d_logs_list, g_logs_list = AAE.extract_logs_tuple()\n```\n\n`a_logs_list` is a `list` of the reconstruction errors.\n\nVisualize the values of `a_logs_list`.\n\n```python\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%config InlineBackend.figure_format = \"retina\"\nplt.figure(figsize=(20, 10))\nplt.title(\"The reconstruction errors.\")\nplt.plot(a_logs_list)\nplt.show()\nplt.close()\n```\n\n
\n\nThe error is not decreasing in steps toward the lower side. Initially, the error is monotonically increased probably due to the side effects of `GeneratorModel` and `DiscriminativeModel` learning in GANs framework. However, as learning as an Auto-Encoder progresses gradually in AAEs framework, it converges after showing the tendency of the monotonous phenomenon.\n\nVisualize the values of `d_logs_list`.\n\n```python\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%config InlineBackend.figure_format = \"retina\"\nplt.style.use(\"fivethirtyeight\")\nplt.figure(figsize=(20, 10))\nplt.plot(d_logs_list)\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.show()\n```\n\n
\n\n
\n\nSimilarly, visualize the values of `g_logs_list`.\n\n```python\nplt.figure(figsize=(20, 10))\nplt.plot(g_logs_list)\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.show()\n```\n\n
\n\n
\n\nAs the training progresses, the values are close to not `0.5` but about `0.55`.\n\nApparently it was not perfect. But we can take heart from the generated images.\n\n#### Generation.\n\nLet's define a helper function for plotting.\n\n```python\ndef plot(arr):\n '''\n Plot three gray scaled images.\n\n Args:\n arr: mini-batch data.\n\n '''\n for i in range(3):\n plt.imshow(arr[i, 0], cmap=\"gray\");\n plt.show()\n plt.close()\n```\n\nDraw from a true distribution of images and input it to `plot` function.\n\n```python\narr = true_sampler.draw()\nplot(arr)\n```\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n\nInput the generated `np.ndarray` to `plot` function.\n\n```python\narr = generator.draw()\nplot(arr)\n```\n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\nNext, observe the true images and reconstructed images.\n\n```python\nobserved_arr = generator.noise_sampler.generate()\ndecoded_arr = generator.inference(observed_arr)\nplot(observed_arr)\nplot(decoded_arr)\n```\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n#### About the progress of learning\n\nJust observing the results of learning does not tell how learning of each model is progressing. In the following, the progress of each learning step is confirmed from the generated images and the reconstructed images.\n\n##### Generated images in 500 step.\n\n```python\narr = generator.draw()\nplot(arr)\n```\n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n##### Generated images in 1000 step.\n\n```python\narr = generator.draw()\nplot(arr)\n```\n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n##### Generated images in 1500 step.\n\n```python\narr = generator.draw()\nplot(arr)\n```\n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n##### Reconstructed images in 500 step.\n\n```python\nobserved_arr = generator.noise_sampler.generate()\ndecoded_arr = generator.inference(observed_arr)\nplot(observed_arr)\nplot(decoded_arr)\n```\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n##### Reconstructed images in 1000 step.\n\n```python\nobserved_arr = generator.noise_sampler.generate()\ndecoded_arr = generator.inference(observed_arr)\nplot(observed_arr)\nplot(decoded_arr)\n```\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n##### Reconstructed images in 1500 step.\n\n```python\nobserved_arr = generator.noise_sampler.generate()\ndecoded_arr = generator.inference(observed_arr)\nplot(observed_arr)\nplot(decoded_arr)\n```\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n\n\n\n\n
\n\n## References\n\n- Fang, W., Zhang, F., Sheng, V. S., & Ding, Y. (2018). A method for improving CNN-based image recognition using DCGAN. Comput. Mater. Contin, 57, 167-178.\n- Gauthier, J. (2014). Conditional generative adversarial nets for convolutional face generation. Class Project for Stanford CS231N: Convolutional Neural Networks for Visual Recognition, Winter semester, 2014(5), 2.\n- Goodfellow, I., Pouget-Abadie, J., Mirza, M., Xu, B., Warde-Farley, D., Ozair, S., ... & Bengio, Y. (2014). Generative adversarial nets. In Advances in neural information processing systems (pp. 2672-2680).\n- Long, J., Shelhamer, E., & Darrell, T. (2015). Fully convolutional networks for semantic segmentation. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 3431-3440).\n- Makhzani, A., Shlens, J., Jaitly, N., Goodfellow, I., & Frey, B. (2015). Adversarial autoencoders. arXiv preprint arXiv:1511.05644.\n- Mirza, M., & Osindero, S. (2014). Conditional generative adversarial nets. arXiv preprint arXiv:1411.1784.\n- Mogren, O. (2016). C-RNN-GAN: Continuous recurrent neural networks with adversarial training. arXiv preprint arXiv:1611.09904.\n- Rifai, S., Vincent, P., Muller, X., Glorot, X., & Bengio, Y. (2011, June). Contractive auto-encoders: Explicit invariance during feature extraction. In Proceedings of the 28th International Conference on International Conference on Machine Learning (pp. 833-840). Omnipress.\n- Rifai, S., Mesnil, G., Vincent, P., Muller, X., Bengio, Y., Dauphin, Y., & Glorot, X. (2011, September). Higher order contractive auto-encoder. In Joint European Conference on Machine Learning and Knowledge Discovery in Databases (pp. 645-660). Springer, Berlin, Heidelberg.\n- Salimans, T., Goodfellow, I., Zaremba, W., Cheung, V., Radford, A., & Chen, X. (2016). Improved techniques for training gans. In Advances in neural information processing systems (pp. 2234-2242).\n- Yang, L. C., Chou, S. Y., & Yang, Y. H. (2017). MidiNet: A convolutional generative adversarial network for symbolic-domain music generation. arXiv preprint arXiv:1703.10847.\n- Zhao, J., Mathieu, M., & LeCun, Y. (2016). Energy-based generative adversarial network. arXiv preprint arXiv:1609.03126.\n- Warde-Farley, D., & Bengio, Y. (2016). Improving generative adversarial networks with denoising feature matching.\n\n### Related PoC\n\n- [\u6df1\u5c64\u5f37\u5316\u5b66\u7fd2\u306e\u30d9\u30a4\u30ba\u4e3b\u7fa9\u7684\u306a\u60c5\u5831\u63a2\u7d22\u306b\u99c6\u52d5\u3055\u308c\u305f\u81ea\u7136\u8a00\u8a9e\u51e6\u7406\u306e\u610f\u5473\u8ad6](https://accel-brain.com/semantics-of-natural-language-processing-driven-by-bayesian-information-search-by-deep-reinforcement-learning/) (Japanese)\n - [\u6b63\u5247\u5316\u554f\u984c\u306b\u304a\u3051\u308b\u6575\u5bfe\u7684\u751f\u6210\u30cd\u30c3\u30c8\u30ef\u30fc\u30af(GANs)\u3068\u6575\u5bfe\u7684\u81ea\u5df1\u7b26\u53f7\u5316\u5668(AAEs)\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u9020](https://accel-brain.com/semantics-of-natural-language-processing-driven-by-bayesian-information-search-by-deep-reinforcement-learning/regularisierungsproblem-und-gan/)\n - [\u968e\u5c64\u7684\u6f5c\u5728\u5909\u6570\u30e2\u30c7\u30eb\u3092\u30e1\u30c7\u30a3\u30a2\u3068\u3057\u305f\u30e9\u30c0\u30fc\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u534a\u6559\u5e2b\u3042\u308a\u5b66\u7fd2\u5f62\u5f0f\u3001\u30ce\u30a4\u30ba\u9664\u53bb\u578b\u81ea\u5df1\u7b26\u53f7\u5316\u5668\u306e\u6a5f\u80fd](https://accel-brain.com/semantics-of-natural-language-processing-driven-by-bayesian-information-search-by-deep-reinforcement-learning/hierarchical-latent-variable-model-as-media-and-semi-supervised-learning-of-ladder-network-as-a-form/)\n - [\u30a8\u30cd\u30eb\u30ae\u30fc\u30d9\u30fc\u30b9\u30e2\u30c7\u30eb\u3068\u3057\u3066\u306e\u6575\u5bfe\u7684\u751f\u6210\u30cd\u30c3\u30c8\u30ef\u30fc\u30af(GAN)\u3068\u81ea\u5df1\u7b26\u53f7\u5316\u5668\u306b\u304a\u3051\u308b\u30ea\u30a2\u30d7\u30ce\u30d5\u5b89\u5b9a](https://accel-brain.com/semantics-of-natural-language-processing-driven-by-bayesian-information-search-by-deep-reinforcement-learning/lyaponov-stability-optimization-in-gan-and-auto-encoder-in-energy-based-models/)\n- [\u300c\u4eba\u5de5\u306e\u7406\u60f3\u300d\u3092\u80cc\u666f\u3068\u3057\u305f\u300c\u4e07\u7269\u7167\u5fdc\u300d\u306e\u30c7\u30fc\u30bf\u30e2\u30c7\u30ea\u30f3\u30b0](https://accel-brain.com/data-modeling-von-korrespondenz-in-artificial-paradise/) (Japanese)\n - [\u30e9\u30f3\u30c0\u30e0\u30a6\u30a9\u30fc\u30af\u306e\u793e\u4f1a\u69cb\u9020\u3068\u30c0\u30a6\u7406\u8ad6\u306e\u610f\u5473\u8ad6\u3001\u518d\u5e30\u7684\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u4fa1\u683c\u5909\u52d5\u30e2\u30c7\u30eb\u304b\u3089\u6575\u5bfe\u7684\u751f\u6210\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\uff08GAN\uff09\u3078](https://accel-brain.com/data-modeling-von-korrespondenz-in-artificial-paradise/sozialstruktur-von-random-walk-und-semantik-der-dow-theorie/)\n\n## Author\n\n- chimera0(RUM)\n\n## Author URI\n\n- http://accel-brain.com/\n\n## License\n\n- GNU General Public License v2.0", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/chimera0/accel-brain-code/tree/master/Generative-Adversarial-Networks/", "keywords": "Generative Adversarial Networks Adversarial Auto-Encoders autoencoder auto-encoder convolution deconvolution encoder decoder LSTM", "license": "GPL2", "maintainer": "", "maintainer_email": "", "name": "pygan", "package_url": "https://pypi.org/project/pygan/", "platform": "", "project_url": "https://pypi.org/project/pygan/", "project_urls": { "Homepage": "https://github.com/chimera0/accel-brain-code/tree/master/Generative-Adversarial-Networks/" }, "release_url": "https://pypi.org/project/pygan/1.0.8/", "requires_dist": null, "requires_python": "", "summary": "pygan is Python library to implement Generative Adversarial Networks(GANs), Conditional GANs, Adversarial Auto-Encoders(AAEs), and Energy-based Generative Adversarial Network(EBGAN).", "version": "1.0.8" }, "last_serial": 5902487, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "b7a4cbde9ceb3626015373e3542b1f05", "sha256": "08529584be51cf440837574362a4f40d49e22a8afcd95e1a4efeb492e8ab738b" }, "downloads": -1, "filename": "pygan-0.0.1.linux-x86_64.tar.gz", "has_sig": false, "md5_digest": "b7a4cbde9ceb3626015373e3542b1f05", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 30914, "upload_time": "2019-02-17T06:58:10", "url": "https://files.pythonhosted.org/packages/32/b7/5c7fdc9755a6cb6ae0d68399683b5ceef33c8ef1e4a7a0b50c0248880e46/pygan-0.0.1.linux-x86_64.tar.gz" }, { "comment_text": "", "digests": { "md5": "b25fb055bd705e2add6f375179f8453a", "sha256": "8caff411dacd6d978eba3d8b951e4532440a0666e3fff0716b6f2ac45e9e8b04" }, "downloads": -1, "filename": "pygan-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "b25fb055bd705e2add6f375179f8453a", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 26664, "upload_time": "2019-02-17T06:49:23", "url": "https://files.pythonhosted.org/packages/22/cc/ff67db84432b0d1b3e03fef53b1d8ec3e08eddb976f40addde2517215726/pygan-0.0.1-py3-none-any.whl" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "5a5bf38c93966d8aec6254b0ae2e8c69", "sha256": "2cfe1cd2fd7a6d24bdbeb5f61f54abd4b1078e09094cdcfed19984577ae573ec" }, "downloads": -1, "filename": "pygan-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "5a5bf38c93966d8aec6254b0ae2e8c69", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 26658, "upload_time": "2019-02-17T07:01:53", "url": "https://files.pythonhosted.org/packages/70/76/015fc10ec8526714a823a77eeaf77cc0e85549b45e419352d800ad2b6ecf/pygan-0.0.2-py3-none-any.whl" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "75916503aa2b2129a2dcf64a28564231", "sha256": "61ed5ce89fc2a0effe3d5ed7335b5442d24912d07037b9d4949baa2f779e3392" }, "downloads": -1, "filename": "pygan-0.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "75916503aa2b2129a2dcf64a28564231", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 50264, "upload_time": "2019-03-02T13:28:21", "url": "https://files.pythonhosted.org/packages/09/99/21c01105e838e5ff4cd728db38bbabeaf652abead13f73ea052c0b3d0129/pygan-0.0.3-py3-none-any.whl" } ], "1.0.0": [ { "comment_text": "", "digests": { "md5": "6ca9e30654eb51de5b9259536716e395", "sha256": "40fa56ea35834e205a5ddeb8a523a43cf4d4bdc72d9ae92542ab89f9f68710d7" }, "downloads": -1, "filename": "pygan-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "6ca9e30654eb51de5b9259536716e395", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 58705, "upload_time": "2019-03-23T15:17:48", "url": "https://files.pythonhosted.org/packages/d4/9f/23e77dfd7a1fd8f3093916dd9e7e92f2c08b0bfcee9195c0ef51cebf08eb/pygan-1.0.0-py3-none-any.whl" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "536c00d231488a3b302d401f3b02790d", "sha256": "c88ccb50520bf233945fee689f3b3eae9977471d7940e9507b538d129b35de4c" }, "downloads": -1, "filename": "pygan-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "536c00d231488a3b302d401f3b02790d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 58778, "upload_time": "2019-04-07T03:16:20", "url": "https://files.pythonhosted.org/packages/33/20/ee05dc7daeebd89b76d6c1cd34f0a8f57d6cc8a315d115c00b4edfb8a15a/pygan-1.0.1-py3-none-any.whl" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "11a389a2823eb4f8897566cb403ec2ae", "sha256": "9af944879056c93c11b487bce112cbc2640ecd1635cf194437a27ae0e0139fef" }, "downloads": -1, "filename": "pygan-1.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "11a389a2823eb4f8897566cb403ec2ae", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 66602, "upload_time": "2019-04-20T15:28:28", "url": "https://files.pythonhosted.org/packages/40/1c/041014fbafb6bc3e9be0f2e95ec4da5d173c3cacf7ab0c05df89d1869185/pygan-1.0.2-py3-none-any.whl" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "15ed2108298f0cc008a70335fa367a91", "sha256": "4defbfa7c5d53d4305666ba8601fb298bb2ff563e997fa59b1c9bb33938c5191" }, "downloads": -1, "filename": "pygan-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "15ed2108298f0cc008a70335fa367a91", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 73824, "upload_time": "2019-06-03T13:32:21", "url": "https://files.pythonhosted.org/packages/87/eb/5c5aef27825088c3826a476d95a0b7e461e0ce1e9205a06afdfb8c9adc51/pygan-1.0.3-py3-none-any.whl" } ], "1.0.4": [ { "comment_text": "", "digests": { "md5": "29fc9f6748b17583234369e048836f71", "sha256": "622a3d0a3643aaa3d12c86b53714b11082cf90f0329f90ef7118a840e773d38d" }, "downloads": -1, "filename": "pygan-1.0.4.tar.gz", "has_sig": false, "md5_digest": "29fc9f6748b17583234369e048836f71", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 49859, "upload_time": "2019-06-08T15:31:36", "url": "https://files.pythonhosted.org/packages/32/35/13d6d340fad62d9a3aa01ebd5d92c409474e5eea3720de1f578d43445a8a/pygan-1.0.4.tar.gz" } ], "1.0.5": [ { "comment_text": "", "digests": { "md5": "555a2fcb6f6f63931a4df53324df471e", "sha256": "c350f2084b5751382ecb0b54e5070b832e798c74d421ce501197dbf991907c70" }, "downloads": -1, "filename": "pygan-1.0.5.tar.gz", "has_sig": false, "md5_digest": "555a2fcb6f6f63931a4df53324df471e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 52368, "upload_time": "2019-06-23T11:28:22", "url": "https://files.pythonhosted.org/packages/c8/a7/64faab941481d417ba53f775bcf2be16548f48e0eb38120f2dcd238d5599/pygan-1.0.5.tar.gz" } ], "1.0.6": [ { "comment_text": "", "digests": { "md5": "c86e3da8cf4c09345da1531ba094affe", "sha256": "1bee19f7dccc65dec79da64121b70cd266540f616e7fe83f7738739a92369a74" }, "downloads": -1, "filename": "pygan-1.0.6.tar.gz", "has_sig": false, "md5_digest": "c86e3da8cf4c09345da1531ba094affe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 53059, "upload_time": "2019-06-30T08:42:30", "url": "https://files.pythonhosted.org/packages/c8/3d/aed88c8403277f3f624f1c19dcb9b7a0d3457ff027213a403dc6e5b9967f/pygan-1.0.6.tar.gz" } ], "1.0.7": [ { "comment_text": "", "digests": { "md5": "9e1e1229a1e2b888b9cf0c6bfef8628a", "sha256": "cc3f5ca0b400d74c1aa3d4830dc9f64b105069ef8f5af56f1a47e75ed41b7244" }, "downloads": -1, "filename": "pygan-1.0.7.tar.gz", "has_sig": false, "md5_digest": "9e1e1229a1e2b888b9cf0c6bfef8628a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 55557, "upload_time": "2019-07-21T14:11:48", "url": "https://files.pythonhosted.org/packages/96/f9/d960fed6e2268884b18f794e8b1d8aa65a261a2da6b00a5bef7267151f0b/pygan-1.0.7.tar.gz" } ], "1.0.8": [ { "comment_text": "", "digests": { "md5": "8bceed57e88dad4dd552d505f7ccd8ff", "sha256": "73e251a199919cabd7c9eaef71e97a0703cfb316cce679dbf2fa1dc31d255dc1" }, "downloads": -1, "filename": "pygan-1.0.8.tar.gz", "has_sig": false, "md5_digest": "8bceed57e88dad4dd552d505f7ccd8ff", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 58923, "upload_time": "2019-09-29T12:13:25", "url": "https://files.pythonhosted.org/packages/1f/82/37db74b0acd7dc1fc7eb9f2cb03f6b810ae6a9d5020427b0ec248d095df2/pygan-1.0.8.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8bceed57e88dad4dd552d505f7ccd8ff", "sha256": "73e251a199919cabd7c9eaef71e97a0703cfb316cce679dbf2fa1dc31d255dc1" }, "downloads": -1, "filename": "pygan-1.0.8.tar.gz", "has_sig": false, "md5_digest": "8bceed57e88dad4dd552d505f7ccd8ff", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 58923, "upload_time": "2019-09-29T12:13:25", "url": "https://files.pythonhosted.org/packages/1f/82/37db74b0acd7dc1fc7eb9f2cb03f6b810ae6a9d5020427b0ec248d095df2/pygan-1.0.8.tar.gz" } ] }