Text emotion classification using Intel oneAPI , Gemma and Gemini Pro [AI for Autism]

Usha Rengaraju
8 min readMar 4, 2024

--

Autism Spectrum Disorder (ASD) is a complex neurodevelopmental condition characterized by challenges in social communication and interaction. One area of difficulty for individuals with ASD is understanding and expressing emotions, which can impact their ability to navigate social interactions effectively. Recent advances in Artificial Intelligence (AI) have opened up new possibilities for supporting individuals with ASD, including the use of AI for text emotion classification. This technology has the potential to enhance emotional understanding and communication skills in individuals with ASD, leading to improved social interactions and quality of life.

Understanding Text Emotion Classification

Text emotion classification involves the use of AI algorithms to analyze written text and determine the emotions expressed in the text. These algorithms can detect subtle cues in the language, such as word choice, sentence structure, and context, to identify the underlying emotions. For individuals with ASD, who may struggle with interpreting nonverbal cues, such as facial expressions and body language, text emotion classification can provide a valuable alternative for understanding emotions in others.

source

The Potential of TEC for Autism:

  • Understanding Emotional State: TEC can help caregivers and therapists understand the emotional state of individuals with ASD through their written communication. This can be particularly beneficial for those who struggle with verbal communication.
  • Identifying Triggers: By analyzing written communication during emotional outbursts or meltdowns, TEC can help identify potential triggers. This information can be used to develop strategies for managing emotions and preventing future episodes.
  • Social Skills Development: TEC can be integrated into educational tools and social interaction platforms to provide real-time feedback on the emotional impact of an individual’s communication. This can help individuals with ASD learn to adjust their communication style to be more socially appropriate.

Benefits of Text Emotion Classification for Individuals with ASD

  1. Enhanced Social Communication: By accurately identifying the emotions expressed in written text, AI can help individuals with ASD better understand the emotional content of messages, improving their ability to engage in meaningful social interactions.
  2. Emotion Regulation: AI can also help individuals with ASD learn to regulate their own emotions by providing feedback on the emotional content of their written communication. This can help them develop strategies for expressing their emotions in a more effective and socially appropriate manner.
  3. Personalized Support: By analyzing patterns in emotional expression in written communication, AI can help identify individual differences in emotional processing among individuals with ASD. This information can be used to tailor interventions and support strategies to meet the specific needs of each individual.
  4. Learning and Development: Text emotion classification can be used as a tool for teaching individuals with ASD about emotions and emotional expression. By providing real-time feedback on the emotional content of their writing, AI can help them learn to recognize and express emotions more effectively.
source

Intel oneAPI

Intel oneAPI is a unified programming model that simplifies the development of applications for heterogeneous architectures. It provides a single set of tools and libraries that developers can use to target a wide range of hardware platforms. Intel has been actively involved in optimizing popular machine learning frameworks like TensorFlow and XGBoost to leverage the performance benefits of Intel architecture, including CPUs and accelerators like Intel Xeon processors and Intel FPGAs. These optimizations aim to improve the speed and efficiency of machine learning workloads, enabling developers to train and deploy models faster and more cost-effectively.

In this blog we have used the Intel oneAPI to show how efficiently we can train and infer models using it.

Dataset

The GoEmotions dataset is a valuable resource for researchers and developers working on text emotion classification (TEC), particularly in the context of understanding emotions expressed in online communication.

Key Features of GoEmotions:

  • Size: 58,009 carefully curated comments extracted from Reddit.
  • Labels: 27 emotion categories + “Neutral” label. These categories include admiration, amusement, anger, annoyance, approval, and many more, offering a fine-grained classification system.
  • Source: Reddit comments represent real-world online communication, providing valuable insights into natural language usage and emotional expression.
  • Availability: GoEmotions is publicly available on various platforms like Hugging Face and TensorFlow Datasets.
import pandas as pd
df = pd.read_csv('/content/data/full_dataset/goemotions_1.csv')
df = df[df['example_very_unclear'] == False]
df.drop(['id', 'author', 'subreddit' ,'link_id' ,'parent_id' ,'created_utc' ,'rater_id', 'example_very_unclear'],inplace=True,axis=1)
df['emotion'] = (df.iloc[:, 1:] == 1).idxmax(1)
cols = list(df.columns)
cols.pop(0)
emotions = ['admiration','amusement','anger','annoyance','approval','caring','confusion','curiosity','desire','disappointment','disapproval','disgust','embarrassment','excitement','fear','gratitude','grief','joy','love','nervousness', 'optimism', 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral']
df['emotion1'] = df.emotion.apply(cols.index)
x = df.text.tolist()
y = df.emotion1.tolist()

Data Analysis and Visualisation using Gemini Pro

Gemini Pro is a powerful large language model (LLM) developed by Google AI, capable of transforming data analysis and visualization. It goes beyond traditional tools by combining natural language processing (NLP) with advanced reasoning and analytical capabilities.

Here’s how Gemini Pro streamlines the data analysis and visualization process:

  1. Data Preparation: While Gemini Pro can handle some raw formats, clean and structured data like CSV, Excel, or databases is preferred for optimal results.
  2. Query Formulation: Craft clear and concise questions to guide Gemini Pro’s analysis. The better your questions are phrased, the more comprehensive and relevant the results will be.
  3. Analysis and Insights: Gemini Pro uses its vast knowledge and analytical capabilities to process your data and answer your questions. It not only provides answers but also explains the reasoning behind them and cites relevant information sources.
  4. Visualization Generation: Based on the extracted insights, Gemini Pro automatically generates visualizations like bar charts, line graphs, or scatter plots, allowing you to easily grasp the data’s story.

In this blog we will use the gemini pro API to create text embeddings of the data and then visualise them.

genai.configure(api_key='API KEY')
SAMPLE_SIZE = 150
df = (df.groupby('emotion', as_index = False)
.apply(lambda x: x.sample(SAMPLE_SIZE))
.reset_index(drop=True))
def make_embed_text_fn(model):

@retry.Retry(timeout=300.0)
def embed_fn(text: str) -> list[float]:
# Set the task_type to CLUSTERING.
embedding = genai.embed_content(model=model,
content=text,
task_type="clustering")
return embedding["embedding"]

return embed_fn

def create_embeddings(df):
model = 'models/embedding-001'
df['Embeddings'] = df['text'].progress_apply(make_embed_text_fn(model))
return df

df_train = create_embeddings(df)
X = np.array(df_train['Embeddings'].to_list(), dtype=np.float32)
tsne = TSNE(random_state=0, n_iter=1000)
tsne_results = tsne.fit_transform(X)
df_tsne = pd.DataFrame(tsne_results, columns=['TSNE1', 'TSNE2'])
df_tsne['Class Name'] = df_train['emotion'].reset_index()['emotion']# Add labels column from df_train to df_tsne

fig, ax = plt.subplots(figsize=(8,6)) # Set figsize
sns.set_style('darkgrid', {"grid.color": ".6", "grid.linestyle": ":"})
sns.scatterplot(data=df_tsne, x='TSNE1', y='TSNE2', hue='Class Name', palette='hls')
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
plt.title('Scatter plot of news using t-SNE');
plt.xlabel('TSNE1');
plt.ylabel('TSNE2');
plt.axis('equal')
# Apply KMeans
kmeans_model = KMeans(n_clusters=4, random_state=1, n_init='auto').fit(X)
labels = kmeans_model.fit_predict(X)
df_tsne['Cluster'] = labels
fig, ax = plt.subplots(figsize=(8,6)) # Set figsize
sns.set_style('darkgrid', {"grid.color": ".6", "grid.linestyle": ":"})
sns.scatterplot(data=df_tsne, x='TSNE1', y='TSNE2', hue='Cluster', palette='magma')
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
plt.title('Scatter plot of news using KMeans Clustering');
plt.xlabel('TSNE1');
plt.ylabel('TSNE2');
plt.axis('equal')

Model Building

For this particular tutorial we will be using the Gemma 2b model for feature extraction and the xgboost model for classification.

Gemma is a family of open-source, state-of-the-art large language models (LLMs) developed by Google AI. Unlike its larger cousins, like Gemini Pro, Gemma models are designed to be lightweight and accessible, making them ideal for diverse applications and wider adoption, especially for individuals and organizations with limited computational resources.

source
import keras
import keras_nlp
from keras import layers
from keras import Sequential

preprocessor = keras_nlp.models.GemmaPreprocessor.from_preset(
"gemma_2b_en", sequence_length=64
)
tx = preprocessor(x)
backbone = keras_nlp.models.GemmaBackbone.from_preset("gemma_2b_en")
model = Sequential([
backbone,
layers.Flatten(),
layers.Dense(NUM_CLASSES,activation='softmax')])
loss = keras.losses.CategoricalCrossentropy(from_logits=False)
# Optimizer
optimizer = keras.optimizers.Adam(learning_rate=1e-5)
# Compile and return
model.compile(loss=loss, optimizer=optimizer, metrics=["accuracy"])
model.fit(
tx,
y,
batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS,
)

After fine-tuning the Gemma model, we use the backbone for extracting features and then use the features to train our xgboost model.

feats = backbone.predict(tx)
import sklearn
from sklearnex import patch_sklearn, unpatch_sklearn
patch_sklearn()
import xgboost as xgb

xgb_params = {
'objective': 'binary:logistic',
'predictor': 'cpu_predictor',
'disable_default_eval_metric': 'true',
}

# Train the model
warnings.simplefilter(action='ignore', category=UserWarning)
t1_start = perf_counter() # Time fit function
model_xgb= xgb.XGBClassifier(**xgb_params)
model_xgb.fit(feats,y)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

t1_start = perf_counter() # Time fit function
model_xgb.predict(feats)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

After that we try it out without the intel optimisation to show the benchmarks.

unpatch_sklearn()
xgb_params = {
'objective': 'binary:logistic',
'predictor': 'cpu_predictor',
'disable_default_eval_metric': 'true',
}

# Train the model
warnings.simplefilter(action='ignore', category=UserWarning)
t1_start = perf_counter() # Time fit function
model_xgb= xgb.XGBClassifier(**xgb_params)
model_xgb.fit(feats,y)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

t1_start = perf_counter() # Time fit function
model_xgb.predict(feats)
t1_stop = perf_counter()
print ("It took", t1_stop-t1_start," to fit.")

Challenges and Future Directions

While text emotion classification shows promise for supporting individuals with ASD, several challenges remain. These include the need for more diverse and representative datasets, as well as the development of algorithms that can accurately detect subtle emotional cues in written text. Future research in this area will focus on addressing these challenges and further enhancing the effectiveness of AI-based approaches for supporting individuals with ASD.

Challenges:

  • Accuracy and Bias: TEC models are still under development, and their accuracy can be limited, particularly for complex or nuanced emotions. It is crucial to be aware of potential biases in the data used to train these models, as they can lead to inaccurate classifications for certain individuals or communication styles.
  • Over-reliance on Technology: TEC should not be seen as a replacement for human interaction and emotional understanding. It is important to remember that technology can only provide tools and insights, and human connection and support remain essential.
  • Ethical Considerations: The use of TEC raises ethical concerns, such as data privacy and the potential for misuse. It is important to ensure transparency and user consent when using TEC with individuals with ASD.

Conclusion

Text emotion classification has the potential to significantly impact the lives of individuals with ASD by improving their social communication skills, emotion regulation, and overall quality of life. By leveraging the power of AI, we can gain valuable insights into the emotional experiences of individuals with ASD and develop more personalized and effective interventions to support them.

Demo

You can also checkout the huggingface demo, click here.

Reference

--

--

Usha Rengaraju

Chief of Research at Exa Protocol | Autism Advocate | Corporate Trainer | Adjunct Faculty