{ "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 simply considered Batch as a numpy version of python dictionary." ] }, { "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": [ "import numpy as np\n", "from tianshou.data import Batch\n", "import torch\n", "import pickle" ] }, { "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 is simply a dictionary which stores all passed in data as key-value pairs, and automatically turns the value into a numpy array if possible.\n", "\n", "## Why 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 these data can be organized as a dictionary and a Batch module helps Tianshou unify the interface of a diverse set of algorithms. Plus, Batch supports advanced indexing, concatenation and splitting, formatting print just like any other numpy array, which may be very helpful for developers.\n", "