diff --git a/Custom and Distributed Training with Tensorflow/Week 1/C2W1_Assignment.ipynb b/Custom and Distributed Training with Tensorflow/Week 1/C2W1_Assignment.ipynb new file mode 100644 index 0000000..cf9ca69 --- /dev/null +++ b/Custom and Distributed Training with Tensorflow/Week 1/C2W1_Assignment.ipynb @@ -0,0 +1,605 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BQRxz8e7wvxv" + }, + "source": [ + "# Basic Tensor operations and GradientTape.\n", + "\n", + "In this graded assignment, you will perform different tensor operations as well as use [GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape). These are important building blocks for the next parts of this course so it's important to master the basics. Let's begin!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "jqev488WJ9-R" + }, + "outputs": [], + "source": [ + "import tensorflow as tf\n", + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oL4Vni_Lwvx_" + }, + "source": [ + "## Exercise 1 - [tf.constant]((https://www.tensorflow.org/api_docs/python/tf/constant))\n", + "\n", + "Creates a constant tensor from a tensor-like object." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "MYdVyiSoLPgO" + }, + "outputs": [], + "source": [ + "# Convert NumPy array to Tensor using `tf.constant`\n", + "def tf_constant(array: np.ndarray) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " array (numpy.ndarray): tensor-like array.\n", + "\n", + " Returns:\n", + " tensorflow.python.framework.ops.EagerTensor: tensor.\n", + " \"\"\"\n", + " ### START CODE HERE ###\n", + " tf_constant_array: tf.Tensor = tf.constant(value=array)\n", + " ### END CODE HERE ###\n", + " return tf_constant_array" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sSlr-uQQwvyI", + "outputId": "74fd006a-a571-4d62-ff02-23bf39e56df0" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "tmp_array: np.ndarray = np.arange(1,10)\n", + "x: tf.Tensor = tf_constant(tmp_array)\n", + "x\n", + "\n", + "# Expected output:\n", + "# " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UjnP9hdVwvyM" + }, + "source": [ + "Note that for future docstrings, the type `EagerTensor` will be used as a shortened version of `tensorflow.python.framework.ops.EagerTensor`." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FnNvdTH_wvyP" + }, + "source": [ + "## Exercise 2 - [tf.square](https://www.tensorflow.org/api_docs/python/tf/math/square)\n", + "\n", + "Computes the square of a tensor element-wise." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "W6BTwNJCLjV8" + }, + "outputs": [], + "source": [ + "# Square the input tensor\n", + "def tf_square(array: tf.Tensor) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " array (numpy.ndarray): tensor-like array.\n", + "\n", + " Returns:\n", + " EagerTensor: tensor.\n", + " \"\"\"\n", + " # make sure it's a tensor\n", + " array = tf.constant(array)\n", + "\n", + " ### START CODE HERE ###\n", + " tf_squared_array: tf.Tensor = tf.square(x=array)\n", + " ### END CODE HERE ###\n", + " return tf_squared_array" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0wgBxzptwvyZ", + "outputId": "ab5e408d-3a7b-479f-bcc4-e28c8ab068a4" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "tmp_array: tf.Tensor = tf.constant(np.arange(1, 10))\n", + "x: tf.Tensor = tf_square(tmp_array)\n", + "x\n", + "\n", + "# Expected output:\n", + "# " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YCKmMPpIwvyg" + }, + "source": [ + "## Exercise 3 - [tf.reshape](https://www.tensorflow.org/api_docs/python/tf/reshape)\n", + "\n", + "Reshapes a tensor." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "7nzBSX8-L0Xt" + }, + "outputs": [], + "source": [ + "# Reshape tensor into the given shape parameter\n", + "def tf_reshape(array: np.ndarray, shape: tuple[int, int]) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " array (EagerTensor): tensor to reshape.\n", + " shape (tuple): desired shape.\n", + "\n", + " Returns:\n", + " EagerTensor: reshaped tensor.\n", + " \"\"\"\n", + " # make sure it's a tensor\n", + " array = tf.constant(array)\n", + " ### START CODE HERE ###\n", + " tf_reshaped_array: tf.Tensor = tf.reshape(tensor=array, shape=shape)\n", + " ### END CODE HERE ###\n", + " return tf_reshaped_array" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IcuDGLdDwvyq", + "outputId": "837d12c1-bc70-48f2-e879-547738b3696d" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "# Check your function\n", + "tmp_array: np.ndarray = np.array([1,2,3,4,5,6,7,8,9])\n", + "# Check that your function reshapes a vector into a matrix\n", + "x: tf.Tensor = tf_reshape(tmp_array, (3, 3))\n", + "x\n", + "\n", + "# Expected output:\n", + "# tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " array (EagerTensor): tensor to be casted.\n", + " dtype (tensorflow.python.framework.dtypes.DType): desired new type.\n", + " (Should be a TF dtype!)\n", + "\n", + " Returns:\n", + " EagerTensor: casted tensor.\n", + " \"\"\"\n", + " # make sure it's a tensor\n", + " array = tf.constant(array)\n", + "\n", + " ### START CODE HERE ###\n", + " tf_cast_array = tf.cast(x=array, dtype=dtype)\n", + " ### END CODE HERE ###\n", + " return tf_cast_array" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ffQj9tX9wvzN", + "outputId": "982f03c1-5e6c-4cf3-d7f5-502c0e44d589" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ], + "source": [ + "# Check your function\n", + "tmp_array: list = [1,2,3,4]\n", + "x: tf.Tensor = tf_cast(tmp_array, tf.float32)\n", + "x\n", + "\n", + "# Expected output:\n", + "# " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GyTwYoZYwvzP" + }, + "source": [ + "## Exercise 5 - [tf.multiply](https://www.tensorflow.org/api_docs/python/tf/multiply)\n", + "\n", + "Returns an element-wise x * y." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "ivepGtD5MKP5" + }, + "outputs": [], + "source": [ + "# Multiply tensor1 and tensor2\n", + "def tf_multiply(tensor1: tf.Tensor, tensor2: tf.Tensor) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " tensor1 (EagerTensor): a tensor.\n", + " tensor2 (EagerTensor): another tensor.\n", + "\n", + " Returns:\n", + " EagerTensor: resulting tensor.\n", + " \"\"\"\n", + " # make sure these are tensors\n", + " tensor1 = tf.constant(tensor1)\n", + " tensor2 = tf.constant(tensor2)\n", + "\n", + " ### START CODE HERE ###\n", + " product: tf.Tensor = tf.multiply(x=tensor1, y=tensor2)\n", + " ### END CODE HERE ###\n", + " return product\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8EHKJYlvwvzW", + "outputId": "08836282-996a-4bb8-b9da-6650817d1ce7" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], + "source": [ + "# Check your function\n", + "tmp_1: tf.Tensor = tf.constant(np.array([[1,2],[3,4]]))\n", + "tmp_2: tf.Tensor = tf.constant(np.array(2))\n", + "result: tf.Tensor = tf_multiply(tmp_1, tmp_2)\n", + "result\n", + "\n", + "# Expected output:\n", + "# " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i4HjosUjwvzY" + }, + "source": [ + "## Exercise 6 - [tf.add](https://www.tensorflow.org/api_docs/python/tf/add)\n", + "\n", + "Returns x + y element-wise." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "BVlntdYnMboh" + }, + "outputs": [], + "source": [ + "# Add tensor1 and tensor2\n", + "def tf_add(tensor1: tf.Tensor, tensor2: tf.Tensor) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " tensor1 (EagerTensor): a tensor.\n", + " tensor2 (EagerTensor): another tensor.\n", + "\n", + " Returns:\n", + " EagerTensor: resulting tensor.\n", + " \"\"\"\n", + " # make sure these are tensors\n", + " tensor1 = tf.constant(tensor1)\n", + " tensor2 = tf.constant(tensor2)\n", + "\n", + " ### START CODE HERE ###\n", + " total: tf.Tensor = tf.add(x=tensor1, y=tensor2)\n", + " ### END CODE HERE ###\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7ihhqavMwvzd", + "outputId": "8c44457b-e287-409b-d538-82020f62b9ec" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ], + "source": [ + "# Check your function\n", + "tmp_1: tf.Tensor = tf.constant(np.array([1, 2, 3]))\n", + "tmp_2: tf.Tensor = tf.constant(np.array([4, 5, 6]))\n", + "tf_add(tmp_1, tmp_2)\n", + "\n", + "# Expected output:\n", + "# " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9EN0W15EWNjD" + }, + "source": [ + "## Exercise 7 - Gradient Tape\n", + "\n", + "Implement the function `tf_gradient_tape` by replacing the instances of `None` in the code below. The instructions are given in the code comments.\n", + "\n", + "You can review the [docs](https://www.tensorflow.org/api_docs/python/tf/GradientTape) or revisit the lectures to complete this task." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "p3K94BWZM6nW" + }, + "outputs": [], + "source": [ + "def tf_gradient_tape(x: tf.Tensor) -> tf.Tensor:\n", + " \"\"\"\n", + " Args:\n", + " x (EagerTensor): a tensor.\n", + "\n", + " Returns:\n", + " EagerTensor: Derivative of z with respect to the input tensor x.\n", + " \"\"\"\n", + " with tf.GradientTape() as t:\n", + "\n", + " ### START CODE HERE ###\n", + " # Record the actions performed on tensor x with `watch`\n", + " t.watch(x)\n", + "\n", + " # Define a polynomial of form 3x^3 - 2x^2 + x\n", + " y: tf.Tensor = (3*(x ** 3)) - (2*(x ** 2)) + x\n", + "\n", + " # Obtain the sum of the elements in variable y\n", + " z: tf.Tensor = tf.reduce_sum(y)\n", + "\n", + " # Get the derivative of z with respect to the original input tensor x\n", + " dz_dx: tf.Tensor = t.gradient(target=z, sources=x)\n", + " ### END CODE HERE\n", + "\n", + " return dz_dx" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "nat6WZ4fwvzk", + "outputId": "a8e78139-0d62-4d65-a670-b6116b16c53e" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "29.0" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ], + "source": [ + "# Check your function\n", + "tmp_x: tf.Tensor = tf.constant(2.0)\n", + "dz_dx: tf.Tensor = tf_gradient_tape(tmp_x)\n", + "result: np.ndarray = dz_dx.numpy()\n", + "result\n", + "\n", + "# Expected output:\n", + "# 29.0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sq_GgW2uwvzn" + }, + "source": [ + "**Congratulations on finishing this week's assignment!**\n", + "\n", + "**Keep it up!**" + ] + } + ], + "metadata": { + "coursera": { + "schema_names": [ + "TF3C2W1-1", + "TF3C2W1-2", + "TF3C2W1-3", + "TF3C2W1-4", + "TF3C2W1-5", + "TF3C2W1-6", + "TF3C2W1-7" + ] + }, + "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.7.6" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file