1
$\begingroup$

A.I Community, this is my first post on here I am currently reading, learning and designing models. At the moment I'm working on this sentiment analysis tool; from what I gather sentiment analysis can be tricky to fine-tune hence why I'm reaching out here to improve on my model. I am asking for tips and pointers the hows and how not I would appreciate detailed answers in the context of improvement etc. Currently, the model is bias towards positive sentiment and even negative text is yielding.6 positive when it should be obvious in the negative sentiment side. My CSV contains 65000 tweets pre-labeled there is an even number it seems of positive and negative tweets and is indeed correctly labeled.

    import pandas as pd
from sklearn.model_selection import train_test_split
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
import re

data = pd.read_csv("training_data.csv")
data = data[['Sentiment', 'SentimentText']]
data['SentimentText'] = data['SentimentText'].apply(lambda x: x.lower())
data['SentimentText'] = data['SentimentText'].apply((lambda x: re.sub('[^a-zA-z0-9\s]','',x)))
max_features = 2000
tokenizer = Tokenizer(num_words=max_features, split=' ')
tokenizer.fit_on_texts(data['SentimentText'].values)
X = tokenizer.texts_to_sequences(data['SentimentText'].values)
X = pad_sequences(X)

from keras.layers import Dense, Dropout, LSTM, Embedding
embed_dim = 50
lstm_out = 80
model = Sequential()
model.add(Embedding(max_features, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.2))
model.add(LSTM(lstm_out))
model.add(Dropout(0.2))
model.add(Dense(2,activation='softmax'))
model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())

Y = pd.get_dummies(data['Sentiment']).values
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.20, random_state=42)
print(X_train.shape,Y_train.shape)
print(X_test.shape,Y_test.shape)
model.fit(X_train, Y_train, nb_epoch=35, batch_size=32, verbose=1)

#save model to disk and print the summary
model.save('model.h5', overwrite=True)
print(model.summary())
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.