diff --git a/lecture1/exercise_0_pytorch.ipynb b/lecture1/exercise_0_pytorch.ipynb index 54c3075bcab156325a9a9d5cd1e7dc68fc94271b..ab00ed2acea09c6905ada6d171c38505d88ab9aa 100644 --- a/lecture1/exercise_0_pytorch.ipynb +++ b/lecture1/exercise_0_pytorch.ipynb @@ -5,9 +5,29 @@ "id": "3881ec5d", "metadata": {}, "source": [ - "# PyTorch tutorial\n", + "# PyTorch Tutorial\n", "---------------\n", - "Welcome to the pytorch tutorial!" + "Welcome to the pytorch tutorial! Here we will go through some of the basics of pytorch.\n", + "\n", + "The library can be imported the usual way:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "e812c4c3", + "metadata": {}, + "outputs": [], + "source": [ + "import torch" + ] + }, + { + "cell_type": "markdown", + "id": "232eb87a", + "metadata": {}, + "source": [ + "## Tensors" ] } ], @@ -27,7 +47,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.10.4" } }, "nbformat": 4, diff --git a/lecture1/exercise_0_tensorflow.ipynb b/lecture1/exercise_0_tensorflow.ipynb index f2015742d142e55649d588e59541635b7c912f2a..88128dd69a0c29eabc4ad1abf889d3470c0dd8d7 100644 --- a/lecture1/exercise_0_tensorflow.ipynb +++ b/lecture1/exercise_0_tensorflow.ipynb @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "id": "e776d989", "metadata": {}, "outputs": [ @@ -49,16 +49,26 @@ "[[1. 1.]\n", " [1. 1.]\n", " [1. 1.]], shape=(3, 2), dtype=float32)\n", - "tf.random_uniform([1, 3]) = tf.Tensor([[0.84930134 0.71385014 0.2513721 ]], shape=(1, 3), dtype=float32)\n", + "tf.random_uniform([1, 3]) = tf.Tensor([[0.6821799 0.35027337 0.35916233]], shape=(1, 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-01 14:33:35.064582: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set\n", + "2022-08-01 14:33:35.067452: 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\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2022-08-01 14:33:35.073213: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.\n" + ] } ], "source": [ "t1 = tf.zeros([5]) # Zeros of length 5 (note the necessary squared brackets)\n", "t2 = tf.ones([3, 2]) # Array of ones of shape (3, 2)\n", - "t3 = tf.random.uniform([1, 3]) # Random sampling from the interval [1, 3)-\n", + "t3 = tf.random.uniform([1, 3]) # Random sampling from the interval [0, 1).\n", "t4 = tf.linspace(1, 7, 4) # Create a tensor of linear spacing from 1 to 7 with 4 entries\n", "t5 = tf.convert_to_tensor(np.linspace(1, 7, 4))\n", "\n", @@ -70,6 +80,34 @@ "print(\"tf.convert_to_tensor( np.linspace(1, 7, 4) ) = %s\" % t5)" ] }, + { + "cell_type": "markdown", + "id": "33a5ef69", + "metadata": {}, + "source": [ + "You can **transform** a tensor **to a numpy array** using ```tensor.numpy()```:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0b576f35", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.6821799 0.35027337 0.35916233]]\n", + "<class 'numpy.ndarray'>\n" + ] + } + ], + "source": [ + "print(t3.numpy()) # Conversion to numpy\n", + "print(type(t3.numpy())) # Printing the type of the converted array" + ] + }, { "cell_type": "markdown", "id": "f904ef1e", @@ -81,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "a70aa763", "metadata": {}, "outputs": [ @@ -120,7 +158,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 6, "id": "367e4ba0", "metadata": {}, "outputs": [ @@ -143,7 +181,7 @@ " [0., 0., 0.]], dtype=float32)>" ] }, - "execution_count": 11, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" }