Skip to content

Commit

Permalink
Updated colab file for 2
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroFerreiradaCosta committed Aug 24, 2020
1 parent 5d2122e commit 508ecea
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions 2 - Modelling data with multiple channels/PixelCNN_RGB.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"source": [
"%tensorflow_version 2.x"
],
"execution_count": 2,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -88,7 +88,7 @@
"import tensorflow as tf\n",
"from tensorflow import keras"
],
"execution_count": 3,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -109,14 +109,13 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Defining random seeds\n",
"random_seed = 42\n",
"tf.random.set_seed(random_seed)\n",
"np.random.seed(random_seed)\n",
"rn.seed(random_seed)"
],
"execution_count": 4,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -141,7 +140,7 @@
" \"\"\"Digitize image into q levels\"\"\"\n",
" return (np.digitize(images, np.arange(q_levels) / q_levels) - 1).astype('float32')"
],
"execution_count": 5,
"execution_count": null,
"outputs": []
},
{
Expand Down Expand Up @@ -230,7 +229,7 @@
" x = tf.nn.bias_add(x, self.bias)\n",
" return x"
],
"execution_count": 6,
"execution_count": null,
"outputs": []
},
{
Expand Down Expand Up @@ -282,7 +281,7 @@
" x += input_tensor\n",
" return x"
],
"execution_count": 7,
"execution_count": null,
"outputs": []
},
{
Expand Down Expand Up @@ -328,7 +327,7 @@
"x_train_overfit = np.transpose(x_train_overfit, (0,3,1,2,4)).reshape(50000,32,32,3)\n",
"x_test_overfit = x_train_overfit"
],
"execution_count": 10,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand All @@ -348,13 +347,12 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Quantisize the input data in q levels\n",
"q_levels = 128\n",
"x_train_quantised_of = quantisize(x_train_overfit, q_levels)\n",
"x_test_quantised_of = quantisize(x_test_overfit, q_levels)"
],
"execution_count": 62,
"execution_count": null,
"outputs": []
},
{
Expand Down Expand Up @@ -383,7 +381,7 @@
"[x.axis('off') for x in ax]\n",
"plt.show()"
],
"execution_count": 9,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -435,7 +433,6 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Creating input stream using tf.data API\n",
"batch_size = 128\n",
"train_buf = 60000\n",
Expand All @@ -460,7 +457,6 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Create PixelCNN model\n",
"n_filters = 120 \n",
"inputs = keras.layers.Input(shape=(height, width, n_channel))\n",
Expand All @@ -477,7 +473,7 @@
"\n",
"pixelcnn = tf.keras.Model(inputs=inputs, outputs=x)"
],
"execution_count": 12,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -488,15 +484,14 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Prepare optimizer and loss function\n",
"lr_decay = 0.9999\n",
"learning_rate = 5e-3 #5\n",
"optimizer = tf.keras.optimizers.Adam(lr=learning_rate)\n",
"\n",
"compute_loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)"
],
"execution_count": 13,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -507,7 +502,6 @@
"colab": {}
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"@tf.function\n",
"def train_step(batch_x, batch_y):\n",
" with tf.GradientTape() as ae_tape:\n",
Expand All @@ -524,7 +518,7 @@
"\n",
" return loss"
],
"execution_count": 14,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -549,7 +543,6 @@
"outputId": "b4b9e042-0a25-4bb9-a7b2-898438e78498"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Training loop\n",
"\n",
"n_epochs = 10\n",
Expand All @@ -575,7 +568,7 @@
" epoch_time,\n",
" epoch_time * (n_epochs - epoch)))"
],
"execution_count": 14,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -647,7 +640,6 @@
"outputId": "f852f353-0915-45df-9d89-198bc6c0f390"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Generating new images\n",
"samples = np.zeros((9, height, width, n_channel), dtype='float32')\n",
"for i in range(height):\n",
Expand All @@ -668,7 +660,7 @@
" plt.yticks(np.array([]))\n",
"plt.show()"
],
"execution_count": 15,
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -706,7 +698,6 @@
"outputId": "79723e63-c659-49e5-853d-cffab9528ed6"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Test\n",
"x_train_quantised = quantisize(x_train, q_levels)\n",
"x_test_quantised = quantisize(x_test, q_levels)\n",
Expand All @@ -728,7 +719,7 @@
"print('nll : {:} nats'.format(np.array(test_loss).mean()))\n",
"print('bits/dim : {:}'.format(np.array(test_loss).mean() / (height * width)))"
],
"execution_count": 17,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -795,7 +786,7 @@
" plt.yticks(np.array([]))\n",
" plt.show()\n"
],
"execution_count": 23,
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -847,7 +838,6 @@
"outputId": "b0f75f22-09b4-4410-bc61-2cb422417618"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Quantisize the input data in q levels\n",
"q_levels = 8\n",
"x_train_quantised = quantisize(x_train, q_levels)\n",
Expand Down Expand Up @@ -876,7 +866,7 @@
" x_test_quantised.astype('int32')))\n",
"test_dataset = test_dataset.batch(batch_size)"
],
"execution_count": 11,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -942,7 +932,6 @@
"outputId": "1c11773c-bb81-4ee2-c118-105beaa59555"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Training loop\n",
"pixelcnn = tf.keras.Model(inputs=inputs, outputs=x)\n",
"\n",
Expand All @@ -969,7 +958,7 @@
" epoch_time,\n",
" epoch_time * (n_epochs - epoch)))"
],
"execution_count": 15,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -1101,7 +1090,6 @@
"outputId": "42bdaedd-1bc1-4b80-cd8b-5a80121254c0"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Test\n",
"\n",
"test_loss = []\n",
Expand All @@ -1117,7 +1105,7 @@
"print('nll : {:} nats'.format(np.array(test_loss).mean()))\n",
"print('bits/dim : {:}'.format(np.array(test_loss).mean() / (height * width)))\n"
],
"execution_count": 16,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -1174,7 +1162,7 @@
" plt.yticks(np.array([]))\n",
"plt.show()"
],
"execution_count": 17,
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -1214,7 +1202,6 @@
"outputId": "21918116-a742-4376-f48a-72bb969d0294"
},
"source": [
"# --------------------------------------------------------------------------------------------------------------\n",
"# Generating new images\n",
"samples = np.zeros((25, height, width, n_channel), dtype='float32')\n",
"for i in range(height):\n",
Expand All @@ -1235,7 +1222,7 @@
" plt.yticks(np.array([]))\n",
"plt.show()"
],
"execution_count": 19,
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
Expand Down

0 comments on commit 508ecea

Please sign in to comment.