diff --git a/lecture1/exercise_0_python.ipynb b/lecture1/exercise_0_python.ipynb index 5fbf96d4a221fbbbecbe4d1862c1c2e95075b937..ce05d5e82495d7d3d872f48f83d3e650272eb632 100644 --- a/lecture1/exercise_0_python.ipynb +++ b/lecture1/exercise_0_python.ipynb @@ -515,7 +515,7 @@ "metadata": {}, "source": [ "### Numpy One Dimensional Array Operations\n", - "```numpy``` **operates** on arrays **element-wise** thus elementary operations are performed in an intuitive way." + "```numpy``` arrays can often be used in the same way as their low dimensional counterparts. ```numpy``` interally **boradcasts** the arrays to the correct shapes to perform operations with many values in a **vectorized manner** often making them very fast and resource efficient." ] }, { diff --git a/lecture1/exercise_0_pytorch.ipynb b/lecture1/exercise_0_pytorch.ipynb index 069e474fb8d6cea4c12e5c11211f1464eddce0aa..e33b1297377c2aefa2668bb8ced05e63a88c592e 100644 --- a/lecture1/exercise_0_pytorch.ipynb +++ b/lecture1/exercise_0_pytorch.ipynb @@ -7,7 +7,7 @@ "source": [ "# PyTorch Tutorial\n", "---------------\n", - "Welcome to the pytorch tutorial! Here we will go through some of the basics of pytorch.\n", + "Welcome to the PyTorch tutorial! Here we will go through some of the basics of PyTorch.\n", "\n", "The library can be imported the as ```torch```:" ] @@ -17,19 +17,7 @@ "execution_count": 1, "id": "e812c4c3", "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'torch'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/dw/z8g0cw_s0nzcv0z1jn7vfgsm0000gn/T/ipykernel_1180/4265195184.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'torch'" - ] - } - ], + "outputs": [], "source": [ "import torch" ] @@ -181,11 +169,11 @@ "\n", "Normal Sample Properties:\n", " Shape: torch.Size([1, 10000])\n", - " Mean: tensor(0.9942)\n", - " Std: tensor(0.5033)\n", + " Mean: tensor(1.0022)\n", + " Std: tensor(0.4985)\n", "\n", "The first row of the normal samples:\n", - "tensor([0.6045, 0.4586, 0.8431, ..., 1.5992, 0.4570, 1.2081])\n" + "tensor([1.2843, 1.7621, 1.4764, ..., 1.1636, 0.1418, 0.9023])\n" ] } ], @@ -229,8 +217,8 @@ "\n", "Uniform Sample Properties:\n", " Shape: torch.Size([3, 100000])\n", - " Mean: tensor([0.5000, 0.4996, 0.5005])\n", - " Std: tensor([0.2893, 0.2885, 0.2884])\n" + " Mean: tensor([0.4984, 0.4998, 0.4998])\n", + " Std: tensor([0.2883, 0.2888, 0.2889])\n" ] } ], @@ -249,7 +237,7 @@ "metadata": {}, "source": [ "### Task <a class=\"tocSkip\">\n", - "Implement the mean-square difference function in pytorch:\n", + "Implement the mean-square difference function in ```pytorch```:\n", "$ L(x, y) = \\sum_i \\frac{(x_i-y_i)^2}{N}$" ] }, @@ -260,11 +248,10 @@ "metadata": {}, "outputs": [ { - "ename": "SyntaxError", - "evalue": "invalid syntax (3637020795.py, line 10)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m Input \u001b[0;32mIn [8]\u001b[0;36m\u001b[0m\n\u001b[0;31m print(loss(x,y))?\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0\n" ] } ], diff --git a/lecture1/exercise_0_tensorflow.ipynb b/lecture1/exercise_0_tensorflow.ipynb index 8c3ec1f525c27943abdfd192656c00e7d262ad48..0e4511684313e0bced11f1e20667cc1bf99b92b8 100644 --- a/lecture1/exercise_0_tensorflow.ipynb +++ b/lecture1/exercise_0_tensorflow.ipynb @@ -5,17 +5,17 @@ "id": "269d507c", "metadata": {}, "source": [ - "# Tensorflow Tutorial\n", + "# TensorFlow Tutorial\n", "---------------------------------\n", "\n", - "Welcome to the Tesorflow tutorial! Here we are going to go through the most fundamental functions and the syntax of the library.\n", + "Welcome to the TensorFlow tutorial! Here we are going to go through the most fundamental functions and the syntax of the library.\n", "\n", - "Usually, tensorflow is imported as ``` tf```:" + "Usually, TensorFlow is imported as ``` tf```:" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "fb7e3964", "metadata": {}, "outputs": [], @@ -30,14 +30,14 @@ "metadata": {}, "source": [ "## Tensors\n", - "One can create tensors in tensorflow using a **syntax similar to** that of **numpy**. These objects are considered to be constants by the library and hence **cannot be modified**; each operation on these objects returns a new tensor object.\n", + "One can create tensors in TensorFlow using a **syntax similar to** that of **numpy**. These objects are considered to be constants by the library and hence **cannot be modified**; each operation on these objects returns a new tensor object.\n", "\n", "You can find some examples of tensor creation below:" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "id": "e776d989", "metadata": {}, "outputs": [ @@ -51,11 +51,19 @@ " [1. 1.]\n", " [1. 1.]], shape=(3, 2), dtype=float32)\n", "tf.random_uniform([1, 3]) = tf.Tensor(\n", - "[[0.6040745 0.5626584 0.02407873]\n", - " [0.21266627 0.27840424 0.76470673]], shape=(2, 3), dtype=float32)\n", + "[[0.9423052 0.5366801 0.27889287]\n", + " [0.8005012 0.05723584 0.8107604 ]], shape=(2, 3), dtype=float32)\n", "tf.linspace(1.0, 7.0, 4) = tf.Tensor([1. 3. 5. 7.], shape=(4,), dtype=float64)\n", "tf.convert_to_tensor( np.linspace(1, 7, 4) ) = tf.Tensor([1. 3. 5. 7.], shape=(4,), dtype=float64)\n" ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-08-08 09:54:22.810114: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE4.1 SSE4.2 AVX AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" + ] } ], "source": [ @@ -66,11 +74,11 @@ "t5 = tf.convert_to_tensor(np.linspace(1, 7, 4))\n", "\n", "# Observe that all of these objects are tensors:\n", - "print(\"tf.ones([3, 2]) = %s\" % t1)\n", - "print(\"tf.zeros([5]) = %s\" % t2)\n", - "print(\"tf.random_uniform([1, 3]) = %s\" % t3)\n", - "print(\"tf.linspace(1.0, 7.0, 4) = %s\" % t4)\n", - "print(\"tf.convert_to_tensor( np.linspace(1, 7, 4) ) = %s\" % t5)" + "print(\"tf.ones([3, 2]) =\", t1)\n", + "print(\"tf.zeros([5]) =\", t2)\n", + "print(\"tf.random_uniform([1, 3]) =\", t3)\n", + "print(\"tf.linspace(1.0, 7.0, 4) =\", t4)\n", + "print(\"tf.convert_to_tensor( np.linspace(1, 7, 4) ) =\", t5)" ] }, { @@ -83,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "id": "0b576f35", "metadata": {}, "outputs": [ @@ -91,8 +99,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[0.6040745 0.5626584 0.02407873]\n", - " [0.21266627 0.27840424 0.76470673]]\n", + "[[0.9423052 0.5366801 0.27889287]\n", + " [0.8005012 0.05723584 0.8107604 ]]\n", "<class 'numpy.ndarray'>\n" ] } @@ -108,12 +116,12 @@ "metadata": {}, "source": [ "## Variables\n", - "On the other hand, ```Variables``` (based on tensors) are objects which are **updated during training** through backpropagation. Each tensorflow variable has to be initialized and their values can be changed using ```variable.assign(new_values)```:" + "On the other hand, ```Variables``` (based on tensors) are objects which are **updated during training** through backpropagation. Each TensorFlow variable has to be initialized and their values can be changed using ```variable.assign(new_values)```:" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "id": "a70aa763", "metadata": {}, "outputs": [ @@ -133,10 +141,10 @@ } ], "source": [ - "w = tf.Variable(tf.zeros([3, 2])) # Create an empty variable w\n", - "print(\"w = %s\" % w) # ...which has zeros only by default\n", - "w.assign(tf.ones([3, 2])) # Assign new values to w\n", - "print(\"w = %s\" % w) # ... and retrieve them" + "w = tf.Variable(tf.zeros([3, 2])) # Create an empty variable w\n", + "print(\"w =\", w) # ...which has zeros only by default\n", + "w.assign(tf.ones([3, 2])) # Assign new values to w\n", + "print(\"w =\", w) # ... and retrieve them" ] }, { @@ -152,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 5, "id": "367e4ba0", "metadata": {}, "outputs": [ @@ -161,10 +169,10 @@ "output_type": "stream", "text": [ "x = tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)\n", - "(x+1)**2 - 2) = tf.Tensor([-1. 2. 7. 14. 23.], shape=(5,), dtype=float32)\n", - "sin(x) tf.Tensor([ 0. 0.84147096 0.9092974 0.14112 -0.7568025 ], shape=(5,), dtype=float32)\n", - "sum(x) tf.Tensor(10.0, shape=(), dtype=float32)\n", - "mean(x) tf.Tensor(2.0, shape=(), dtype=float32)\n" + "(x+1)**2 - 2 = tf.Tensor([-1. 2. 7. 14. 23.], shape=(5,), dtype=float32)\n", + "sin(x) = tf.Tensor([ 0. 0.84147096 0.9092974 0.14112 -0.7568025 ], shape=(5,), dtype=float32)\n", + "sum(x) = tf.Tensor(10.0, shape=(), dtype=float32)\n", + "mean(x) = tf.Tensor(2.0, shape=(), dtype=float32)\n" ] } ], @@ -172,15 +180,15 @@ "x = tf.linspace(0., 4., 5) # Create a tensor array\n", "\n", "print(\"x =\", x)\n", - "print(\"(x+1)**2 - 2) =\", (x + 1.)**2 - 2.)\n", - "print(\"sin(x)\", tf.sin(x))\n", - "print(\"sum(x)\", tf.reduce_sum(x))\n", - "print(\"mean(x)\", tf.reduce_mean(x))" + "print(\"(x+1)**2 - 2 =\", (x + 1.)**2 - 2.)\n", + "print(\"sin(x) =\", tf.sin(x))\n", + "print(\"sum(x) =\", tf.reduce_sum(x))\n", + "print(\"mean(x) =\", tf.reduce_mean(x))" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 6, "id": "58c8bd1d", "metadata": {}, "outputs": [ @@ -192,7 +200,7 @@ " [0., 0., 0.]], dtype=float32)>" ] }, - "execution_count": 9, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -212,7 +220,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 7, "id": "dd02b475", "metadata": {}, "outputs": [ @@ -220,8 +228,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Means: tf.Tensor([-0.00430035 -0.00648657 0.01164796], shape=(3,), dtype=float32)\n", - "Stds: tf.Tensor([1.0067673 0.99281996 0.993856 ], shape=(3,), dtype=float32)\n" + "Means: tf.Tensor([-0.00242303 -0.00101589 -0.01899738], shape=(3,), dtype=float32)\n", + "Stds: tf.Tensor([0.99289805 1.0028254 1.00137 ], shape=(3,), dtype=float32)\n" ] } ], @@ -245,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 8, "id": "d925efd7", "metadata": {}, "outputs": [