{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# پشت صحنه خط تولید (TensorFlow)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Install the Transformers, Datasets, and Evaluate libraries to run this notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install datasets evaluate transformers[sentencepiece]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'label': 'POSITIVE', 'score': 0.9598047137260437},\n", " {'label': 'NEGATIVE', 'score': 0.9994558095932007}]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from transformers import pipeline\n", "\n", "classifier = pipeline(\"sentiment-analysis\")\n", "classifier(\n", " [\n", " \"I've been waiting for a HuggingFace course my whole life.\",\n", " \"I hate this so much!\",\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoTokenizer\n", "\n", "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n", "tokenizer = AutoTokenizer.from_pretrained(checkpoint)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{\n", " 'input_ids': ,\n", " 'attention_mask': \n", "}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_inputs = [\n", " \"I've been waiting for a HuggingFace course my whole life.\",\n", " \"I hate this so much!\",\n", "]\n", "inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors=\"tf\")\n", "print(inputs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import TFAutoModel\n", "\n", "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n", "model = TFAutoModel.from_pretrained(checkpoint)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 16, 768)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outputs = model(inputs)\n", "print(outputs.last_hidden_state.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import TFAutoModelForSequenceClassification\n", "\n", "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n", "model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint)\n", "outputs = model(inputs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(outputs.logits.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(outputs.logits)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tf.Tensor(\n", "[[4.01951671e-02 9.59804833e-01]\n", " [9.9945587e-01 5.4418424e-04]], shape=(2, 2), dtype=float32)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tensorflow as tf\n", "\n", "predictions = tf.math.softmax(outputs.logits, axis=-1)\n", "print(predictions)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'NEGATIVE', 1: 'POSITIVE'}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.config.id2label" ] } ], "metadata": { "colab": { "name": "پشت صحنه خط تولید (TensorFlow)", "provenance": [] } }, "nbformat": 4, "nbformat_minor": 4 }