{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "69y6AHvq1S3f" }, "source": [ "# Batch\n", "In this tutorial, we will introduce the **Batch** to you, which is the most basic data structure in Tianshou. You can consider Batch as a numpy version of python dictionary. It is also similar to pytorch's tensordict,\n", "although with a somehow different type structure." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "editable": true, "id": "NkfiIe_y2FI-", "outputId": "5008275f-8f77-489a-af64-b35af4448589", "slideshow": { "slide_type": "" }, "tags": [ "remove-output", "hide-cell" ] }, "outputs": [], "source": [ "%%capture\n", "\n", "import pickle\n", "\n", "import numpy as np\n", "import torch\n", "\n", "from tianshou.data import Batch" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = Batch(a=4, b=[5, 5], c=\"2312312\", d=(\"a\", -2, -3))\n", "print(data)\n", "print(data.b)" ] }, { "cell_type": "markdown", "metadata": { "id": "S6e6OuXe3UT-" }, "source": [ "A batch stores all passed in data as key-value pairs, and automatically turns the value into a numpy array if possible.\n", "\n", "## Why do we need Batch in Tianshou?\n", "The motivation behind the implementation of Batch module is simple. In DRL, you need to handle a lot of dictionary-format data. For instance, most algorithms would require you to store state, action, and reward data for every step when interacting with the environment. All of them can be organized as a dictionary, and the\n", " Batch class helps Tianshou in unifying the interfaces of a diverse set of algorithms. In addition, Batch supports advanced indexing, concatenation and splitting, formatting print just like any other numpy array, which proved to be helpful for developers.\n", "