{ "cells": [ { "cell_type": "markdown", "id": "6312daad", "metadata": {}, "source": [ "# variational-autoencoders" ] }, { "cell_type": "markdown", "id": "ba0c701f", "metadata": {}, "source": [ "A model skeleton for Variational Auto-Encoders (VAE). It was first introduced in the paper titled *[Auto-Encoding Variational Bayes]*(https://arxiv.org/abs/1312.6114) by Diederik P Kingma and Max Welling." ] }, { "cell_type": "markdown", "id": "a6ddd815", "metadata": {}, "source": [ "> In machine learning, a variational autoencoder, also known as VAE, is the artificial neural network architecture belonging to the families of probabilistic graphical models and variational Bayesian methods.\n", "\n", "> It is often associated with the autoencoder model because of its architectural affinity, but there are significant differences both in the goal and in the mathematical formulation. Variational autoencoders are meant to compress the input information into a constrained multivariate latent distribution (encoding) to reconstruct it as accurately as possible (decoding). Although this type of model was initially designed for unsupervised learning, its effectiveness has been proven in other domains of machine learning such as semi-supervised learning or supervised learning. - *WikiPedia*" ] }, { "cell_type": "code", "execution_count": 1, "id": "de7b27bc", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload" ] }, { "cell_type": "markdown", "id": "1753d7e2", "metadata": {}, "source": [ "## usage" ] }, { "cell_type": "code", "execution_count": 2, "id": "af3b496b", "metadata": { "execution": { "iopub.execute_input": "2022-01-03T21:33:05.773384Z", "iopub.status.busy": "2022-01-03T21:33:05.772674Z", "iopub.status.idle": "2022-01-03T21:33:28.873989Z", "shell.execute_reply": "2022-01-03T21:33:28.874242Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "type \n" ] }, { "ename": "TypeError", "evalue": "isinstance expected 2 arguments, got 1", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdeeply\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mvae\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdeeply\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"convolutional-variational-autoencoder\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m28\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mvae\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/dev/deeply/src/deeply/deeply.py\u001b[0m in \u001b[0;36mhub\u001b[0;34m(name, *args, **kwargs)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;31m# kwargs[\"weights\"] = kwargs.get(\"weights\", DEFAULT[\"weights\"])\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mModelFactory\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/dev/deeply/src/deeply/model/factory.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(name, *args, **kwargs)\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mmodel_class\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"model_class\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 27\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmodel_class\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/dev/deeply/src/deeply/model/vae.py\u001b[0m in \u001b[0;36mConvolutionalVAE\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 584\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdeeply\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"convolutional-variational-autoencoder\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 585\u001b[0m \"\"\"\n\u001b[0;32m--> 586\u001b[0;31m model = VAE(\n\u001b[0m\u001b[1;32m 587\u001b[0m \u001b[0mname\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"convolutional-vae\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 588\u001b[0m \u001b[0mlayer_block\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mConvBlock\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/dev/deeply/src/deeply/model/vae.py\u001b[0m in \u001b[0;36mVAE\u001b[0;34m(x, y, channels, init_units, layer_growth_rate, layer_depth, n_dense, latent_dim, activation, batch_norm, dropout_rate, final_activation, kernel_initializer, name, backbone, backbone_weights, weights, layer_block, kernel_size, strides, n_classes, padding, final_conv_size, final_unit_size, attention_gate, freeze_backbone, *args, **kwargs)\u001b[0m\n\u001b[1;32m 302\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 303\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"type\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer_block\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 304\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer_block\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 305\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 306\u001b[0m layer_block_args = dict(activation = activation, dropout_rate = dropout_rate, width = n_dense,\n", "\u001b[0;31mTypeError\u001b[0m: isinstance expected 2 arguments, got 1" ] } ], "source": [ "import deeply\n", "\n", "vae = deeply.hub(\"convolutional-variational-autoencoder\", x = 28)\n", "vae.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "59c60b6a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.9" } }, "nbformat": 4, "nbformat_minor": 5 }