cross validation<\/a> to enhance our prediction.<\/p>\n\n\n\ntrain_text = pad_sequences(tokenizer.texts_to_sequences(df_train_comment[\"comment\"]), maxlen=MAX_SEQUENCE_LENGTH)\ntest_text = pad_sequences(tokenizer.texts_to_sequences(df_test_comment[\"comment\"]), maxlen=MAX_SEQUENCE_LENGTH)\ntrain_target = df_train_comment[\"target\"]\nn_splits=3\nsplits = list(KFold(n_splits).split(train_text,train_target))\ntest_preds = np.zeros((df_test_comment.shape[0]))\nfor fold in list(range(n_splits)):\n K.clear_session()\n tr_ind, val_ind = splits[fold]\n checkpoint = ModelCheckpoint(f'gru_{fold}.hdf5', save_best_only = True)\n earlystop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3)\n model = build_model()\n model.fit(train_text[tr_ind],\n train_target[tr_ind],\n batch_size=2048,\n epochs=100,\n validation_data=(train_text[val_ind], train_target[val_ind]),\n callbacks = [earlystop,checkpoint])\n test_preds += model.predict(test_text)[:,0]\ntest_preds \/= n_splits\nsubmission = pd.read_csv('..\/input\/jigsaw-unintended-bias-in-toxicity-classification\/sample_submission.csv', index_col='id')\nsubmission['prediction'] = test_preds\nsubmission.reset_index(drop=False, inplace=True)\n<\/pre>\n\n\n\nAfter around 2 hours processing time, we have the prediction and we can discover how well a machine can do for a human’s work. <\/p>\n\n\n\n
validation_df = pd.merge(test_df, submission, on='id')\nvalidation_df[validation_df.prediction > 0.5].head()\nvalidation_df[validation_df.prediction < 0.5].head()\n<\/pre>\n\n\n\nThis is a group of machine classified toxic comments: <\/p>\n\n\n\n <\/figure>\n\n\n\n And this is a group of non-toxic comments: <\/p>\n\n\n\n <\/figure>\n\n\n\nI do not have any issue for non-toxic comments, just the toxic comments part is a bit, \"high moral standard\" :]] .<\/p>\n\n\n\n
When I submit above prediction to Kaggle, it turns out scoring 0.92x , i.e. 92.x% accuracy. There is still room for improvement, keep trying and learning!<\/p>\n\n\n\n
<\/div>\n\n\n\n
What have we learnt in this post? <\/h3>\n\n\n\nDefinition of Recurrent Neural Network<\/li> Concept of LSTM<\/li> Concept of Bidirectional structure<\/li> Building RNN model in Python<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"From our Part 1 of NLP and Python topic, we talked about word pre-processing for a machine to handle words. This time, we are going to talk about building a model for a machine to classify words. We learned to use CNN to classify images in past. Then we use another neural network, Recurrent Neural […]<\/p>\n","protected":false},"author":1,"featured_media":1982,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"Build a RNN (Recurrent Neural Network) in Python","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[18],"tags":[57,26,146,140,145,144],"jetpack_publicize_connections":[],"yoast_head":"\n
RNN (Recurrent Neural Network) in NLP and Python - Part 2 ⋆ Code A Star<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n