Loading

DLNLP-note

[Starter Code] DL-NLP-1 Sentiment Analysis

This a starter code for DL-NLP Sentiment Analysis Assignment

S.Rathi

You're given a starter code for DL-NLP Sentiment Analysis Assignment. It contains basic instructions on using the notebook to make submissions & pre-defined functions for the participants to fill in. You will be making a copy of this notebook and working with that.

AIcrowd

Assignment 1, Sentiment Analysis

In this assignment, you’re tasked with identifying the rating of a review using sentiment analysis.

The training dataset consists of {sentence id, review, rating}. Given a review, your neural network should be able to assign a rating score between 1 to 5 - low to high, respectively.

Do note:

  • Perform sentiment analysis using a neural network with just one input and output layer (with softmax as activation function).
  • You’re not allowed to have hidden layers.

How to use this notebook? 📝

This is a shared template and any edits you make here will not be saved. You should make a copy in your own drive. Click the "File" menu (top-left), then "Save a Copy in Drive". You will be working in your copy however you like.

notebook overview

  • Update the config parameters. You can define the common variables here
Variable Description
TRAIN_DATA_PATH Path to the file containing training data (The data will be available at /data/).
TEST_DATA_PATH Path to the file containing test data (The data will be available at /data/).
PREDICTIONS_PATH Path to write the output to.
ASSETS_DIR In case your notebook needs additional files (like model weights, etc.,), you can add them to a directory and specify the path to the directory here (please specify relative path). The contents of this directory will be sent to AIcrowd for evaluation.
API_KEY In order to submit your code to AICrowd, you need to provide your account's API key. This key is available at https://www.aicrowd.com/participants/me
  • Installing packages. Please use the Install packages πŸ—ƒ section to install the packages
  • Training your models. All the code within the Training phase βš™οΈ section will be skipped during evaluation. Please make sure to save your model weights in the assets directory and load them in the predictions phase section

Dataset Specifications 💾

  • train.csv: has 3 columns with latter two being 'reviews' & corresponding 'ratings'.
  • test.csv: has 2 columns with latter being 'reviews'. You will have to predict the corresponding 'ratings'.
  • 'ratings' in predictions should be integers in range [1,5] i.e. {1,2,3,4,5}

Setup AIcrowd Utilities 🛠

We use this to bundle the files for submission and create a submission on AIcrowd. Do not edit this block.

In [ ]:
!pip install -U git+https://gitlab.aicrowd.com/aicrowd/aicrowd-cli.git@notebook-submission-v2 > /dev/null
In [ ]:
%load_ext aicrowd.magic

Install packages 🗃

Please add all pacakages installations in this section

In [ ]:
!pip install numpy pandas

Import necessary modules and packages 📚

In [ ]:
import os
import pandas as pd
import numpy as np

#Add your necessary modules & packages here

AIcrowd Runtime Configuration 🧷

Define configuration parameters. Please include any files needed for the notebook to run under ASSETS_DIR. We will copy the contents of this directory to your final submission file πŸ™‚

The dataset is available under /data on the workspace.

In [ ]:
class AIcrowdConfig:
  DATASET_DIR = "data"
  TEST_DATA_PATH = os.path.join(DATASET_DIR, "test.csv")
  TRAIN_DATA_PATH = os.path.join(DATASET_DIR, "train.csv")
  PREDICTIONS_PATH = "predictions.csv"
  ASSETS_DIR = "assets"
  API_KEY = "" # Get your key from https://www.aicrowd.com/participants/me (ctrl + click the link)

Download the dataset 📲

AIcrowd magic functions will download the dataset after authenticating your API key.

In [ ]:
%aicrowd login --api-key "$AIcrowdConfig.API_KEY"
%aicrowd dataset download -c dlnlp-note

Extract the downloaded dataset to data directory

In [ ]:
!mkdir $AIcrowdConfig.DATASET_DIR
!mv train.csv $AIcrowdConfig.DATASET_DIR
!mv test.csv $AIcrowdConfig.DATASET_DIR

Define preprocessing code 💻

The code that is common between the training and the predictions sections should be defined here. During evaluation, we completely skip the training section. Please make sure to add any common logic between the training and prediction sections here.

In [ ]:
'''
About the task:

You are provided with a codeflow- which consists of functions to be implemented(MANDATORY).

You need to implement each of the functions mentioned below, you may add your own function parameters if needed.
'''


def encode_data(text):
    # This function will be used to encode the reviews using a dictionary(created using corpus vocabulary) 
    
    # Example of encoding :"The food was fabulous but pricey" has a vocabulary of 4 words, each one has to be mapped to an integer like: 
    # {'The':1,'food':2,'was':3 'fabulous':4 'but':5 'pricey':6} this vocabulary has to be created for the entire corpus and then be used to 
    # encode the words into integers 

    # return encoded examples
    pass



def convert_to_lower(text):
    # return the reviews after convering then to lowercase
    pass


def remove_punctuation(text):
    # return the reviews after removing punctuations
    pass


def remove_stopwords(text):
    # return the reviews after removing the stopwords
    pass

def perform_tokenization(text):
    # return the reviews after performing tokenization
    pass


def perform_padding(data):
    # return the reviews after padding the reviews to maximum length
    pass

def preprocess_data(data):
    # make all the following function calls on your data

    review = data["reviews"]
    review = convert_to_lower(review)
    review = remove_punctuation(review)
    review = remove_stopwords(review)
    review = perform_tokenization(review)
    review = encode_data(review)
    processed_data = perform_padding(review)

    # return processed_data # Uncomment this
    # Remove this dummy code at the bottom
    return np.zeros( (len(data["reviews"]), 100) )

Define your Softmax function

You have to write your own implementation from scratch and return softmax values(using predefined softmax is prohibited)

In [ ]:
def softmax_activation(x):
    # write your implementation here
    pass

Training phase ⚙️

You can define your training code here. This sections will be skipped during evaluation.

Define your model

You should define your medal related methods here using the given template

In [ ]:
# Example with tensorflow, but you can replace with pytorch
# For better code add all imports to the top cell marked for imports
import tensorflow
from tensorflow import keras
In [ ]:
class NeuralNet:

    def __init__(self, reviews, ratings):

        self.reviews = reviews
        self.ratings = ratings


    def build_nn(self):
        #add the input and output layer here; you can use either tensorflow or pytorch
        model = keras.models.Sequential()
        model.add(keras.layers.Input((100,)))
        model.add(keras.layers.Dense(np.max(self.ratings)+1, activation='softmax') )

        ####### Use the softmax activation that you wrote code for above #####
        
        model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')

        self.model = model

    def train_nn(self,batch_size,epochs):
        # write the training loop here; you can use either tensorflow or pytorch
        # print validation accuracy
        self.model.fit(x=self.reviews, y=self.ratings, epochs=3)

    def predict(self, reviews):
        # return a list containing all the ratings predicted by the trained model

        self.model.predict(reviews)

Load training data 💻

In [ ]:
train_data = pd.read_csv(AIcrowdConfig.TRAIN_DATA_PATH)
train_data.head()
Out[ ]:
Unnamed: 0 reviews ratings
0 0 This book was very informative, covering all a... 4
1 1 I am already a baseball fan and knew a bit abo... 5
2 2 I didn't like this product it smudged all unde... 1
3 3 I simply love the product. I appreciate print ... 5
4 4 It goes on very easily and makes my eyes look ... 5

Initialize & Train your model

In [ ]:
batch_size, epochs= 1000, 3
    
train_reviews=preprocess_data(train_data)
train_ratings=train_data['ratings'].values - 1

model=NeuralNet(train_reviews,train_ratings)
model.build_nn()
model.train_nn(batch_size,epochs)

Save your trained model

In [ ]:
if not os.path.isdir(AIcrowdConfig.ASSETS_DIR):
  os.mkdir(AIcrowdConfig.ASSETS_DIR)
# This is the example for a keras model, save your model according to your framework
model.model.save(os.path.join(AIcrowdConfig.ASSETS_DIR, "dummy_model.h5"))

Prediction phase 🔎

Please make sure to save the weights from the training section in your assets directory and load them in this section

In [ ]:
from tensorflow import keras
model = keras.models.load_model(os.path.join(AIcrowdConfig.ASSETS_DIR, "dummy_model.h5"))

Load test data

In [ ]:
test_data = pd.read_csv(AIcrowdConfig.TEST_DATA_PATH)
test_data.head()
Out[ ]:
Unnamed: 0 reviews
0 0 Doesn't work at ALL. Don't waste your money or...
1 1 What crap. Would need a lot more power to do ...
2 2 Has no suction and didn't work. Not worth trying.
3 3 That is definitely a trash. Unable to clean an...
4 4 Didn't even worked on cleaning the ears at all...

Read and preprocess the data

In [ ]:
test_reviews=preprocess_data(test_data)

Generate predictions

In [ ]:
#Make your predictions here based on your model
raw_predictions = model.predict(test_reviews)
predictions = np.argmax(raw_predictions, axis=-1)

Save predictions 📨

In [ ]:
pd.DataFrame(predictions, columns=["ratings"]).to_csv(AIcrowdConfig.PREDICTIONS_PATH)

Submit to AIcrowd 🚀

NOTE: PLEASE SAVE THE NOTEBOOK BEFORE SUBMITTING IT (Ctrl + S)

In [ ]:
%aicrowd submission create --jupyter -c dlnlp-note

Comments

You must login before you can post a comment.

Execute