{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-Single label text classification\n", "\n", "*Note: This example builds upon multi-label classification. If you are unfamiliar with it, refer to the previous examples.*\n", "\n", "Multi-single label classification is a type of hierarchical classification: First, a sample is classified with multi-label classification, yielding a mumber of classes. Then, a single prediction (in a second dimension) is given for each of those classes. \n", "We showcase this with an ABSA (aspect-based sentiment analysis) example, using reviews of hotels. Just like a multi-label dataset, each sample has multiple aspects, but then each aspect additionally has a sentiment. For example, \"I liked the breakfast, althought the staff doesn’t even bother to greet you when you enter\" has the aspects \"food\" with positive sentiment, and the aspect \"staff\" with a negative sentiment. The label would thus be `[[\"Food\", \"positive\"], [\"Staff\", \"negative\"]]." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from autotransformers import AutoTransformer, DatasetLoader" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Below is an example dataset for multi-single-label tasks. Additionally to the \"classes\", we now have to configure a list of \"inner classes\" for the inner classification task. Note the `None` inner class, which is used when a sample is not labeled with the corresponding outer class. \n", "The samples' values are lists of pairs in multi-single classification: Each pair specified an outer and its corresponding inner class." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset = {\n", " \"meta\": {\n", " \"name\": \"example_multisingle_label\",\n", " \"version\": \"1.0.0\",\n", " \"created_with\": \"wizard\"\n", " },\n", " \"config\": [\n", " {\n", " \"domain\": \"text\",\n", " \"type\": \"IText\"\n", " },\n", " {\n", " \"task_id\": \"task1\",\n", " \"classes\": [\"Room\", \"Staff\", \"Cleanliness\"],\n", " \"inner_classes\": [\"None\", \"positive\", \"neutral\", \"negative\"],\n", " \"none_inner_class\": \"None\",\n", " \"type\": \"TMultiSingleClassification\"\n", " }\n", " ],\n", " \"train\": [\n", " [\n", " {\"value\": \"the room was very spacious.\"},\n", " {\"value\": [[\"Room\", \"positive\"]]},\n", " ],\n", " [\n", " {\"value\": \"Everything was spotless, bed lines were indeed heavenly and the bed was super comfortable with wonderful pillows.\"},\n", " {\"value\": [[\"Cleanliness\", \"positive\"]]},\n", " ],\n", " [\n", " {\"value\": \"In the room there is a tiny bathroom with shower and seperate toilet.\"},\n", " {\"value\": [[\"Room\", \"neutral\"]]},\n", " ],\n", " [\n", " {\"value\": \"Our agent had booked the wrong room type and the hotel room we were given was poor.\"},\n", " {\"value\": [[\"Room\", \"negative\"]]},\n", " ],\n", " ],\n", " \"eval\": [\n", " [\n", " {\"value\": \"Really clean right by a canal staff really friendly and helpful.\"},\n", " {\"value\": [[\"Staff\", \"positive\"], [\"Cleanliness\", \"positive\"]]},\n", " ],\n", " ],\n", " \"test\": [\n", " [\n", " {\"value\": \"The concierge was also very helpful in organising some train tickets\"},\n", " {\"value\": [[\"Staff\", \"positive\"]]},\n", " ]\n", " ]\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As before, we simply create a DatasetLoader from the dataset and start training." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl = DatasetLoader(dataset)\n", "\n", "# Or create a DatasetLoader from a file\n", "# dl = DatasetLoader(\"path/to/my-dataset.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# In this example, we only train for one epoch to finish fast. \n", "# In reality, you want to set this to a higher value for better results.\n", "config = [\n", " (\"engine/stop_condition/type\", \"MaxEpochs\"),\n", " (\"engine/stop_condition/value\", 1),\n", "]\n", "at = AutoTransformer(config)\n", "\n", "at.init(dataset_loader=dl, path=\".models/example03\")\n", "at.train(dl)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And check our result:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "at(\"I liked the breakfast, althought the staff doesn't even bother to greet you when you go anywhere.\")" ] } ], "metadata": { "kernelspec": { "display_name": "env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "83ee97f1e4ad98a710574577955c6720418d3d8f987616cd4f238f891737d017" } } }, "nbformat": 4, "nbformat_minor": 2 }