Beginning ML – Sentiment Analysis Using Neural Network cont. : KnowDev

This post is a continuation from this .

I hope you have got a good understanding why we have to pre-process. In this post we shall train our model and also input our own sentences.

First of all we shall get our feature sets that we have created either from pickle or call the function to store into a variable.

from create_sentiment_featuresets import create_feature_sets_and_labels
train_x, train_y, test_x, test_y = create_feature_sets_and_labels('pos.txt', 'neg.txt')

We will be using the same neural network model that we used here. First we have define our placeholder for features.

x = tf.placeholder('float', [None, len(train_x[0])])
y = tf.placeholder('float')

len( train_x[0] ) returns the length the features.

The neural network model is define using neural_network_model function. After the neural network is defined it’s time to train our model.

First we’ll capture the prediction / output of neural network using

prediction = neural_network_model(x)

Then, we have to find the cross entropy of the prediction made by our model. We are using softmax regresstion.

#1
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                                                      prediction, y))

After finding the cross entropy is time to back propagate and try reduce the difference.

#2
optimizer = tf.train.AdadeltaOptimizer(0.5).minimize(cross_entropy)

both #1 and #2 makes the training step. We’ll start session and using number of epochs as 10.

The accuracy we could achieve was 55.44

sentiment-54-accuracy

The trained model is saved into ‘sentiment_model.ckpt’, later we can use that to restore our variables ( i.e weights and biases ) to use.

Making predictions :

To make predictions using our model that we have just trained we have to preprocess our input sentence so that can be passed as features to our model. After we prepocess our input sentence we predict.

result = (sess.run(tf.argmax(prediction.eval(
feed_dict={x: [features[:423]]}), 1)))

we print out whether the output is positive or negative using

if result[0] == 0:
    print('Positive:', input_data)
elif result[0] == 1:
    print('Negative:', input_data

sentiment_output

As you can see our model makes pretty good prediction even though the accuracy is 54% .

In this post we have seen how we can train our own data as well as use it. In less than a week time we are able to make a machine which can predict the sentiment of any sentence pretty interesting right ? In next post I will introduce you to more sophisticated version of sentiment analysis. See you in next !

link to complete source code :  here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s