<jupyter_start><jupyter_text>Pytorch  Framework for Multi-class Text Classification  Sentiment Classification - 3 Class Problem* Financial Phrasebank Dataset     * 0 - Positive     * 1 - Negative     * 2 - Neutral    Reading Data<jupyter_code>import sys
import numpy as np
import pandas as pd

sys.path.append("..")
pd.set_option('display.max_colwidth', -1)
df_train = pd.read_csv('../datasets/financial_phrasebank/training.csv', sep='\t')
df_test = pd.read_csv('../datasets/financial_phrasebank/testing.csv', sep='\t')
df_val = pd.read_csv('../datasets/financial_phrasebank/validation.csv', sep='\t')

df_train.shape, df_test.shape, df_val.shape
df_train.label.value_counts()
df_val.label.value_counts()<jupyter_output><empty_output><jupyter_text>Processing Data in Input format<jupyter_code>data_classes = ['positive', 'negative', 'neutral']

df_train['label'] = df_train['label'].apply(data_classes.index)
df_test['label'] = df_test['label'].apply(data_classes.index)
df_val['label'] = df_val['label'].apply(data_classes.index)
df_test.head()
df_bert_train = pd.DataFrame({'id':df_train['Unnamed: 0'],
            'text':df_train['text'],
            'label':df_train['label']})

df_bert_test = pd.DataFrame({'id':df_test['Unnamed: 0'],
            'text':df_test['text']})

df_bert_val = pd.DataFrame({'id':df_val['Unnamed: 0'],
            'text':df_val['text'],
            'label':df_val['label']})
df_bert_train.to_csv('../datasets/financial_phrasebank/train.csv',index=False)
df_bert_test.to_csv('../datasets/financial_phrasebank/test.csv',index=False)
df_bert_val.to_csv('../datasets/financial_phrasebank/dev.csv',index=False)<jupyter_output><empty_output><jupyter_text>Training Transformers (Albert | Roberta | DistilBert) classifier with FrameWork<jupyter_code>from classitransformers.report import metrics
from classitransformers.configs import Configs
from classitransformers.pytransformers import TransformersClassification<jupyter_output><empty_output><jupyter_text>Set Configurations* Pre-trained config filepath, vocab filepath and model checkpoint filepath* labels list* train, test and eval batch size* max_seq_len - how long is the each text input sentence !!* data_dir - data path folder (optional)* output_dir - output path folder (optional)<jupyter_code>config = Configs(
                pretrained_model_dir = '../models/DistilBert',
                model_name = 'distilbert',
                num_train_epochs=3,
                train_batch_size = 16,
                eval_batch_size = 8, 
                do_train = True, 
                do_eval = True, 
                max_seq_length = 64,
                label_list = ["0", "1", "2"],
                data_dir = '../datasets/financial_phrasebank/',
                output_dir = '../transformers_output_sentiment/')
model = TransformersClassification(config)<jupyter_output><empty_output><jupyter_text>Train Method<jupyter_code>model.train()<jupyter_output>======== Epoch 1 / 3 ========
Training...
  Batch    40  of    194.    Elapsed: 0:00:08.
  Batch    80  of    194.    Elapsed: 0:00:15.
  Batch   120  of    194.    Elapsed: 0:00:23.
  Batch   160  of    194.    Elapsed: 0:00:31.

  Average training loss: 0.64
  Training epcoh took: 0:00:37

Running Validation...
  Accuracy: 0.83
  Validation Loss: 0.00
  Validation took: 0:00:03

======== Epoch 2 / 3 ========
Training...
  Batch    40  of    194.    Elapsed: 0:00:08.
  Batch    80  of    194.    Elapsed: 0:00:15.
  Batch   120  of    194.    Elapsed: 0:00:23.
  Batch   160  of    194.    Elapsed: 0:00:31.

  Average training loss: 0.28
  Training epcoh took: 0:00:37

Running Validation...
  Accuracy: 0.85
  Validation Loss: 0.00
  Validation took: 0:00:03

======== Epoch 3 / 3 ========
Training...
  Batch    40  of    194.    Elapsed: 0:00:08.
  Batch    80  of    194.    Elapsed: 0:00:15.
  Batch   120  of    194.    Elapsed: 0:00:23.
  Batch   160  of    194.    Elapsed: 0:00:31.

[...]<jupyter_text>Model Test<jupyter_code>prediction = model.test()<jupyter_output><empty_output><jupyter_text>**Testing a sentence, takes a list of sentences.**<jupyter_code>model.text_inference(['Trading gives the best returns among all the options of investemnts'])
y_pred = [np.argmax(tup) for tup in prediction]
y = df_test.label<jupyter_output><empty_output><jupyter_text>Accuracy and Report<jupyter_code>metrics(y, y_pred)<jupyter_output>Accuracy: 0.866
Precision: 0.845
Recall: 0.838
F1-Score: 0.842
{'fraction_non_alphanumeric': 0.13571910871033085, 'fraction_numerical': 0.04591492234976367, 'mean_word_length': 3.692713833157339, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '430225', 'license': 'Apache-2.0', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1516, 'n_words': 319, 'path': 'sample_notebooks/PYTORCH_classi_transformers_sentiment_example.ipynb', 'repo_name': 'mgroovyank/classitransformers', 'size': '4443'}