News Classifier

Bahram Mohammadpour
AI Backyard
Published in
4 min readAug 24, 2018

this is a joint affair of Armin Behjati and Bahram Mohammadpour.

Here we try to make a news classifier based on the news language model we made earlier . back then we saved the encoder to use it here as the backbone of this model .

as before we use the fantastic fastai library to make things faster and easier !

{10: 0, 2: 1, 18: 2, 5: 3}
array([2, 2, 18, ..., 18, 2, 5], dtype=object)

We shuffle the dataset to add an element of randomness .

We split the data so that we have 9000 news files as training data and the rest is for validation .

(1171, 2)

We have 4 labels referring to 4 news categories .

{0, 1, 2, 3}

As before we save our data and then tokenize it .

'\xa0معاون بازاريابي فروش ايران خودرو با بيان اين كه اجراي روش هاي متنوع و جامع براي فروش محصولات يكي از مسيرهاي افزايش رضايتمندي مشتريان و فراهم كردن امكان خريد كالاي ايراني است تاكيد كرد: ايران خودرو در سال حمايت از كالاي ايراني برنامه هاي متنوعي را براي خريد محصولات به اجرا خواهد گذاشت. \xa0\xa0 \xa0\xa0به گزارش ايكوپرس، مصطفي خان كرمي با تشريح برنامه هاي فروش ايران خودرو در سال 97، اعمال تخفيف خريد كالاي ايراني را از جمله مشوق هاي خريد محصولات ايران خودرو دانست و اظهار كرد:\u200c با مطالعه و بررسي بازار و نيازسنجي از مشتريان برنامه هاي فروش را منطبق با خواست و سليقه آنان به اجرا مي گذاريم.وي افزود:\u200c طرح هاي فروش را متناسب با بودجه مشتريان در نظر مي گيريم و در اين راستا برنامه هاي فروش اقساطي و اعتباري را طرح ريزي مي كنيم تا همه گروه هاي درآمدي موفق به خريد محصولات ايراني ايران خودرو شوند.خان كرمي طرح هاي پيش فروش را از جمله طرح هاي فروش محصولات ايران خودرو نام برد و گفت: در پيش فروش هايی كه به صورت سرمايه گذاري پيشنهاد مي شود امكان مشاركت مشتريان در فرآيند توليد محصول ايجاد مي شود.وي درعين حال تصريح كرد: پرواضح است كه ايران خودرو نيز از مشاركت هاي مردمي در طرح هاي توليد بهره مند مي شود. با اين روش بازگشت سرمايه مشتريان تا موعد تحويل خودرو كاملا تضمين شده است.خان كرمي افزود:\u200c در طرح هاي پيش فروش عادي نيز امكان برنامه ريزي مالي بهتري را براي مشتريان فراهم مي كنيم.معاون بازاريابي و فروش ايران خودرو اضافه كرد: علاوه بر فروش هاي نقدي كه محصول با قيمت قطعي به دست مشتريان مي رسد، فروش اقساطي و اعتباري را نيز به اجرا مي گذاريم كه با اعمال شرايط منعطف، علاوه بر شناور بودن مبلغ پيش پرداخت، مشتريان قادر هستند زمان بازپرداخت اقساط را براساس شرايط مالي خود انتخاب كنند. | '
0
1
2
3
4
5
6
7
8
0
1

Here we just save the tokens .

Now we load itos we saved before in language model part and make a stoi function from it .

45303

Classifier !

The hyperparameters should be same as before as we are going to load the encoder . but the dropouts and bptt can be changed .

The first thing we need is a Dataset. So we have a Dataset passing in the documents and their labels.then we use sortish which makes it so the first things in the list are, on the whole, shorter than the things at the end, but a little bit random as well.
sampler parameter and sampler is a class we are going to define that tells the data loader how to shuffle.
For validation set, we are going to define something that actually just sorts. It just deterministically sorts it so that all the shortest documents will be at the start, all the longest documents will be at the end, and that’s going to minimize the amount of padding.

So now we can call that function as before .this timewith the head we add on, we have a few more things we can do. we can add more than one hidden layer. In layers=[em_sz*3, 50, c]
em_sz*3 : is the input to the classifier head
50: output of the first layer .
c : output of the last layer or the number of classes .

We are going to use RNN_Learner just like before.

Here we use weight decay and load the encoder we have saved .

Now we just train the last layer and we get 89% accuracy !

epoch      trn_loss   val_loss   accuracy                    
0 0.367081 0.870484 0.899231
[array([0.87048]), 0.8992314260297103]

Here we train the last three layers and we get the 92% accuracy !

epoch      trn_loss   val_loss   accuracy                    
0 0.297732 0.232494 0.918873
[array([0.23249]), 0.9188727579190118]

And here id the model architecture :

SequentialRNN(
(0): MultiBatchRNN(
(encoder): Embedding(45303, 400, padding_idx=1)
(encoder_with_dropout): EmbeddingDropout(
(embed): Embedding(45303, 400, padding_idx=1)
)
(rnns): ModuleList(
(0): WeightDrop(
(module): LSTM(400, 1150)
)
(1): WeightDrop(
(module): LSTM(1150, 1150)
)
(2): WeightDrop(
(module): LSTM(1150, 1150)
)
(3): WeightDrop(
(module): LSTM(1150, 400)
)
)
(dropouti): LockedDropout(
)
(dropouths): ModuleList(
(0): LockedDropout(
)
(1): LockedDropout(
)
(2): LockedDropout(
)
(3): LockedDropout(
)
)
)
(1): PoolingLinearClassifier(
(layers): ModuleList(
(0): LinearBlock(
(lin): Linear(in_features=1200, out_features=50, bias=True)
(drop): Dropout(p=0.2)
(bn): BatchNorm1d(1200, eps=1e-05, momentum=0.1, affine=True)
)
(1): LinearBlock(
(lin): Linear(in_features=50, out_features=4, bias=True)
(drop): Dropout(p=0.1)
(bn): BatchNorm1d(50, eps=1e-05, momentum=0.1, affine=True)
)
)
)
)

--

--

Bahram Mohammadpour
AI Backyard

Interested in AI, Deep Learning, Machine Learning, Big Data and Startups