{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Einops tutorial, part 2: deep learning\n", "\n", "Previous part of tutorial provides visual examples with numpy.\n", "\n", "## What's in this tutorial?\n", "\n", "- working with deep learning packages\n", "- important cases for deep learning models\n", "- `einops.asnumpy` and `einops.layers`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from einops import rearrange, reduce" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "x = np.random.RandomState(42).normal(size=[10, 32, 100, 200])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# utility to hide answers\n", "from utils import guess" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Select your flavour\n", "\n", "Switch to the framework you're most comfortable with. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# select \"tensorflow\" or \"pytorch\"\n", "flavour = \"pytorch\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "selected pytorch backend\n" ] } ], "source": [ "print(f\"selected {flavour} backend\")\n", "if flavour == \"tensorflow\":\n", " import tensorflow as tf\n", "\n", " tape = tf.GradientTape(persistent=True)\n", " tape.__enter__()\n", " x = tf.Variable(x) + 0\n", "else:\n", " assert flavour == \"pytorch\"\n", " import torch\n", "\n", " x = torch.from_numpy(x)\n", " x.requires_grad = True" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(torch.Tensor, torch.Size([10, 32, 100, 200]))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x), x.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple computations \n", "\n", "- converting bchw to bhwc format and back is a common operation in CV\n", "- try to predict output shape and then check your guess!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "