{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#导入所需要的包\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:37:28.130335Z","iopub.execute_input":"2021-06-04T08:37:28.130867Z","iopub.status.idle":"2021-06-04T08:37:28.137572Z","shell.execute_reply.started":"2021-06-04T08:37:28.130813Z","shell.execute_reply":"2021-06-04T08:37:28.136145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#用于归一化\nclass Sampling(layers.Layer):\n    \"\"\"Uses (z_mean, z_log_var) to sample z, the vector encoding a digit.\"\"\"\n\n    def call(self, inputs):\n        z_mean, z_log_var = inputs\n        batch = tf.shape(z_mean)[0]\n        dim = tf.shape(z_mean)[1]\n        epsilon = tf.keras.backend.random_normal(shape=(batch, dim))\n        return z_mean + tf.exp(0.5 * z_log_var) * epsilon","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:37:28.604831Z","iopub.execute_input":"2021-06-04T08:37:28.60517Z","iopub.status.idle":"2021-06-04T08:37:28.610252Z","shell.execute_reply.started":"2021-06-04T08:37:28.605138Z","shell.execute_reply":"2021-06-04T08:37:28.609282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#建立神经网络模型\nlatent_dim = 100\n\nencoder_inputs = keras.Input(shape=(28, 28, 1))\nx = layers.Conv2D(32, 3, activation=\"relu\", strides=2, padding=\"same\")(encoder_inputs)\nx = layers.Conv2D(64, 3, activation=\"relu\", strides=2, padding=\"same\")(x)\nx = layers.Flatten()(x)\nx = layers.Dense(160, activation=\"relu\")(x)\nz_mean = layers.Dense(latent_dim, name=\"z_mean\")(x)\nz_log_var = layers.Dense(latent_dim, name=\"z_log_var\")(x)\nz = Sampling()([z_mean, z_log_var])\nencoder = keras.Model(encoder_inputs, [z_mean, z_log_var, z], name=\"encoder\")\nencoder.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:37:28.654653Z","iopub.execute_input":"2021-06-04T08:37:28.655042Z","iopub.status.idle":"2021-06-04T08:37:28.727348Z","shell.execute_reply.started":"2021-06-04T08:37:28.655006Z","shell.execute_reply":"2021-06-04T08:37:28.726091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#建立神经网络\nlatent_inputs = keras.Input(shape=(latent_dim,))\nx = layers.Dense(7 * 7 * 64, activation=\"relu\")(latent_inputs)\nx = layers.Reshape((7, 7, 64))(x)\nx = layers.Conv2DTranspose(64, 3, activation=\"relu\", strides=2, padding=\"same\")(x)\nx = layers.Conv2DTranspose(32, 3, activation=\"relu\", strides=2, padding=\"same\")(x)\ndecoder_outputs = layers.Conv2DTranspose(1, 3, activation=\"sigmoid\", padding=\"same\")(x)\ndecoder = keras.Model(latent_inputs, decoder_outputs, name=\"decoder\")\ndecoder.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:01.320282Z","iopub.execute_input":"2021-06-04T08:12:01.320692Z","iopub.status.idle":"2021-06-04T08:12:01.394591Z","shell.execute_reply.started":"2021-06-04T08:12:01.320652Z","shell.execute_reply":"2021-06-04T08:12:01.393345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#使用VAE类进行模型优化训练\nclass VAE(keras.Model):\n    def __init__(self, encoder, decoder, **kwargs):\n        super(VAE, self).__init__(**kwargs)\n        self.encoder = encoder\n        self.decoder = decoder\n        self.total_loss_tracker = keras.metrics.Mean(name=\"total_loss\")\n        self.reconstruction_loss_tracker = keras.metrics.Mean(\n            name=\"reconstruction_loss\"\n        )\n        self.kl_loss_tracker = keras.metrics.Mean(name=\"kl_loss\")\n\n    @property\n    def metrics(self):\n        return [\n            self.total_loss_tracker,\n            self.reconstruction_loss_tracker,\n            self.kl_loss_tracker,\n        ]\n\n    def train_step(self, data):\n        with tf.GradientTape() as tape:\n            z_mean, z_log_var, z = self.encoder(data)\n            reconstruction = self.decoder(z)\n            reconstruction_loss = tf.reduce_mean(\n                tf.reduce_sum(\n                    keras.losses.mse(data, reconstruction), axis=(1, 2)\n                )\n            )\n            kl_loss = -0.5 * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var))\n            kl_loss = tf.reduce_mean(tf.reduce_sum(kl_loss, axis=1))\n            total_loss = reconstruction_loss + kl_loss\n        grads = tape.gradient(total_loss, self.trainable_weights)\n        self.optimizer.apply_gradients(zip(grads, self.trainable_weights))\n        self.total_loss_tracker.update_state(total_loss)\n        self.reconstruction_loss_tracker.update_state(reconstruction_loss)\n        self.kl_loss_tracker.update_state(kl_loss)\n        return {\n            \"loss\": self.total_loss_tracker.result(),\n            \"reconstruction_loss\": self.reconstruction_loss_tracker.result(),\n            \"kl_loss\": self.kl_loss_tracker.result(),\n        }","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:37:28.737955Z","iopub.execute_input":"2021-06-04T08:37:28.738305Z","iopub.status.idle":"2021-06-04T08:37:28.748663Z","shell.execute_reply.started":"2021-06-04T08:37:28.738273Z","shell.execute_reply":"2021-06-04T08:37:28.747448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#进行数据读取\nimport numpy as np\ndef load_data(path='mnist.npz'):\n    \"\"\"\n        读取mnist数据所用，自带\n        自带的下载因为网络无法使用\n        自己下载数据然后把路径作为参数传入\n    \"\"\"\n    f = np.load(path)\n    x_train, y_train = f['x_train'], f['y_train']\n    x_test, y_test = f['x_test'], f['y_test']\n    f.close()\n    return (x_train, y_train), (x_test, y_test)\n \n# the data, split between train and test sets\n(x_train, y_train), (x_test, y_test) = load_data('../input/mnist-numpy/mnist.npz')\n\nmnist_digits = np.concatenate([x_train, x_test], axis=0)\nmnist_digits = np.expand_dims(mnist_digits, -1).astype(\"float32\") / 255\nmnist_digits.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:01.410874Z","iopub.execute_input":"2021-06-04T08:12:01.41132Z","iopub.status.idle":"2021-06-04T08:12:01.855692Z","shell.execute_reply.started":"2021-06-04T08:12:01.411275Z","shell.execute_reply":"2021-06-04T08:12:01.854597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndirectory = \"/kaggle/input/dog-breed-identification/\"\nds = keras.preprocessing.image_dataset_from_directory(\n    directory, labels='inferred', label_mode='int',\n    class_names=None, color_mode='grayscale', batch_size=70000, image_size=(28,\n    28), shuffle=True, seed=None, validation_split=None, subset=None,\n    interpolation='bilinear', follow_links=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:01.858546Z","iopub.execute_input":"2021-06-04T08:12:01.859009Z","iopub.status.idle":"2021-06-04T08:12:02.337087Z","shell.execute_reply.started":"2021-06-04T08:12:01.858962Z","shell.execute_reply":"2021-06-04T08:12:02.335964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagenet_gen = next( (X.numpy()/255 for X, y in ds) )\nimagenet_gen.shape\ndata = imagenet_gen\n","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:02.338501Z","iopub.execute_input":"2021-06-04T08:12:02.338859Z","iopub.status.idle":"2021-06-04T08:12:26.476358Z","shell.execute_reply.started":"2021-06-04T08:12:02.338818Z","shell.execute_reply":"2021-06-04T08:12:26.474944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#读取经过格式化后的数据为data，进行VAE的编译\ndata = mnist_digits\nvae = VAE(encoder, decoder)\nvae.compile(optimizer=keras.optimizers.Adam())","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:26.477997Z","iopub.execute_input":"2021-06-04T08:12:26.478353Z","iopub.status.idle":"2021-06-04T08:12:26.526466Z","shell.execute_reply.started":"2021-06-04T08:12:26.478308Z","shell.execute_reply":"2021-06-04T08:12:26.525586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#进行可视化训练数据 \nhistory = vae.fit(data, epochs=30, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:12:26.527787Z","iopub.execute_input":"2021-06-04T08:12:26.528397Z","iopub.status.idle":"2021-06-04T08:37:28.125035Z","shell.execute_reply.started":"2021-06-04T08:12:26.528349Z","shell.execute_reply":"2021-06-04T08:37:28.123665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#根据训练数据预测图片种类\n\nz_mean, z_log_var, z = vae.encoder.predict(data[0:1,...])\ndec = vae.decoder.predict(z)\ndec2 = vae.decoder.predict(z_mean)\ndec3 = vae.decoder.predict(z_mean - 0.5*np.exp(z_log_var))\ndec4 = vae.decoder.predict(z_mean + 0.5*np.exp(z_log_var))\nprint(z_mean.mean())\nprint(z_log_var.std())\nprint(z.std())","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:38:36.84581Z","iopub.execute_input":"2021-06-04T08:38:36.846383Z","iopub.status.idle":"2021-06-04T08:38:37.228177Z","shell.execute_reply.started":"2021-06-04T08:38:36.846338Z","shell.execute_reply":"2021-06-04T08:38:37.226997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#结果展示\nplt.plot(history.history['reconstruction_loss'])","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:40:03.666666Z","iopub.execute_input":"2021-06-04T08:40:03.667295Z","iopub.status.idle":"2021-06-04T08:40:03.806228Z","shell.execute_reply.started":"2021-06-04T08:40:03.667255Z","shell.execute_reply":"2021-06-04T08:40:03.805142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['kl_loss'])","metadata":{"execution":{"iopub.status.busy":"2021-06-04T08:40:06.247158Z","iopub.execute_input":"2021-06-04T08:40:06.247673Z","iopub.status.idle":"2021-06-04T08:40:06.36347Z","shell.execute_reply.started":"2021-06-04T08:40:06.247642Z","shell.execute_reply":"2021-06-04T08:40:06.362728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import panda as pd\nsubmission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission['Survived']=prediction\n\nsubmission.to_csv('submission2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T08:36:37.392895Z","iopub.execute_input":"2021-06-05T08:36:37.393386Z","iopub.status.idle":"2021-06-05T08:36:37.419214Z","shell.execute_reply.started":"2021-06-05T08:36:37.393348Z","shell.execute_reply":"2021-06-05T08:36:37.41714Z"},"trusted":true},"execution_count":null,"outputs":[]}]}