From d75527f7eecc4e2fcdd18ab157412506717c8adb Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Mon, 7 Nov 2022 23:36:11 -0700 Subject: add blog post --- docs/posts/2019-12-22-Fake-News-Detector.html | 96 ++++++++++++++++++--------- 1 file changed, 64 insertions(+), 32 deletions(-) (limited to 'docs/posts/2019-12-22-Fake-News-Detector.html') diff --git a/docs/posts/2019-12-22-Fake-News-Detector.html b/docs/posts/2019-12-22-Fake-News-Detector.html index 46297b0..9b62b00 100644 --- a/docs/posts/2019-12-22-Fake-News-Detector.html +++ b/docs/posts/2019-12-22-Fake-News-Detector.html @@ -60,48 +60,63 @@ Whenever you are looking for a dataset, always try searching on Kaggle and GitHu This allows you to train the model on the GPU. Turicreate is built on top of Apache's MXNet Framework, for us to use GPU we need to install a CUDA compatible MXNet package.

-
!pip install turicreate
+
+
!pip install turicreate
 !pip uninstall -y mxnet
 !pip install mxnet-cu100==1.4.0.post0
-
+
+

If you do not wish to train on GPU or are running it on your computer, you can ignore the last two lines

Downloading the Dataset

-
!wget -q "https://github.com/joolsa/fake_real_news_dataset/raw/master/fake_or_real_news.csv.zip"
+
+
!wget -q "https://github.com/joolsa/fake_real_news_dataset/raw/master/fake_or_real_news.csv.zip"
 !unzip fake_or_real_news.csv.zip
-
+
+

Model Creation

-
import turicreate as tc
+
+
import turicreate as tc
 tc.config.set_num_gpus(-1) # If you do not wish to use GPUs, set it to 0
-
+
+
-
dataSFrame = tc.SFrame('fake_or_real_news.csv')
-
+
+
dataSFrame = tc.SFrame('fake_or_real_news.csv')
+
+

The dataset contains a column named "X1", which is of no use to us. Therefore, we simply drop it

-
dataSFrame.remove_column('X1')
-
+
+
dataSFrame.remove_column('X1')
+
+

Splitting Dataset

-
train, test = dataSFrame.random_split(.9)
-
+
+
train, test = dataSFrame.random_split(.9)
+
+

Training

-
model = tc.text_classifier.create(
+
+
model = tc.text_classifier.create(
     dataset=train,
     target='label',
     features=['title','text']
 )
-
+
+
-
+-----------+----------+-----------+--------------+-------------------+---------------------+
+
+
+-----------+----------+-----------+--------------+-------------------+---------------------+
 | Iteration | Passes   | Step size | Elapsed Time | Training Accuracy | Validation Accuracy |
 +-----------+----------+-----------+--------------+-------------------+---------------------+
 | 0         | 2        | 1.000000  | 1.156349     | 0.889680          | 0.790036            |
@@ -111,39 +126,50 @@ a CUDA compatible MXNet package.

| 4 | 8 | 1.000000 | 1.814194 | 0.999063 | 0.925267 | | 9 | 14 | 1.000000 | 2.507072 | 1.000000 | 0.911032 | +-----------+----------+-----------+--------------+-------------------+---------------------+ -
+
+

Testing the Model

-
est_predictions = model.predict(test)
+
+
est_predictions = model.predict(test)
 accuracy = tc.evaluation.accuracy(test['label'], test_predictions)
 print(f'Topic classifier model has a testing accuracy of {accuracy*100}% ', flush=True)
-
+
+
-
Topic classifier model has a testing accuracy of 92.3076923076923%
-
+
+
Topic classifier model has a testing accuracy of 92.3076923076923%
+
+

We have just created our own Fake News Detection Model which has an accuracy of 92%!

-
example_text = {"title": ["Middling ‘Rise Of Skywalker’ Review Leaves Fan On Fence About Whether To Threaten To Kill Critic"], "text": ["Expressing ambivalence toward the relatively balanced appraisal of the film, Star Wars fan Miles Ariely admitted Thursday that an online publication’s middling review of The Rise Of Skywalker had left him on the fence about whether he would still threaten to kill the critic who wrote it. “I’m really of two minds about this, because on the one hand, he said the new movie fails to live up to the original trilogy, which makes me at least want to throw a brick through his window with a note telling him to watch his back,” said Ariely, confirming he had already drafted an eight-page-long death threat to Stan Corimer of the website Screen-On Time, but had not yet decided whether to post it to the reviewer’s Facebook page. “On the other hand, though, he commended J.J. Abrams’ skillful pacing and faithfulness to George Lucas’ vision, which makes me wonder if I should just call the whole thing off. Now, I really don’t feel like camping outside his house for hours. Maybe I could go with a response that’s somewhere in between, like, threatening to kill his dog but not everyone in his whole family? I don’t know. This is a tough one.” At press time, sources reported that Ariely had resolved to wear his Ewok costume while he murdered the critic in his sleep."]}
+
+
example_text = {"title": ["Middling ‘Rise Of Skywalker’ Review Leaves Fan On Fence About Whether To Threaten To Kill Critic"], "text": ["Expressing ambivalence toward the relatively balanced appraisal of the film, Star Wars fan Miles Ariely admitted Thursday that an online publication’s middling review of The Rise Of Skywalker had left him on the fence about whether he would still threaten to kill the critic who wrote it. “I’m really of two minds about this, because on the one hand, he said the new movie fails to live up to the original trilogy, which makes me at least want to throw a brick through his window with a note telling him to watch his back,” said Ariely, confirming he had already drafted an eight-page-long death threat to Stan Corimer of the website Screen-On Time, but had not yet decided whether to post it to the reviewer’s Facebook page. “On the other hand, though, he commended J.J. Abrams’ skillful pacing and faithfulness to George Lucas’ vision, which makes me wonder if I should just call the whole thing off. Now, I really don’t feel like camping outside his house for hours. Maybe I could go with a response that’s somewhere in between, like, threatening to kill his dog but not everyone in his whole family? I don’t know. This is a tough one.” At press time, sources reported that Ariely had resolved to wear his Ewok costume while he murdered the critic in his sleep."]}
 example_prediction = model.classify(tc.SFrame(example_text))
 print(example_prediction, flush=True)
-
+
+
-
+-------+--------------------+
+
+
+-------+--------------------+
 | class |    probability     |
 +-------+--------------------+
 |  FAKE | 0.9245648658345308 |
 +-------+--------------------+
 [1 rows x 2 columns]
-
+
+

Exporting the Model

-
model_name = 'FakeNews'
+
+
model_name = 'FakeNews'
 coreml_model_name = model_name + '.mlmodel'
 exportedModel = model.export_coreml(coreml_model_name)
-
+
+

Note: To download files from Google Colab, simply click on the files section in the sidebar, right click on filename and then click on download

@@ -162,7 +188,8 @@ DescriptionThe bag-of-words model is a simplifying representation used in NLP, i

We define our bag of words function

-
func bow(text: String) -> [String: Double] {
+
+
func bow(text: String) -> [String: Double] {
         var bagOfWords = [String: Double]()
 
         let tagger = NSLinguisticTagger(tagSchemes: [.tokenType], options: 0)
@@ -181,22 +208,26 @@ DescriptionThe bag-of-words model is a simplifying representation used in NLP, i
 
         return bagOfWords
     }
-
+
+

We also declare our variables

-
@State private var title: String = ""
+
+
@State private var title: String = ""
 @State private var headline: String = ""
 @State private var alertTitle = ""
 @State private var alertText = ""
 @State private var showingAlert = false
-
+
+

Finally, we implement a simple function which reads the two text fields, creates their bag of words representation and displays an alert with the appropriate result

Complete Code

-
import SwiftUI
+
+
import SwiftUI
 
 struct ContentView: View {
     @State private var title: String = ""
@@ -271,7 +302,8 @@ DescriptionThe bag-of-words model is a simplifying representation used in NLP, i
         ContentView()
     }
 }
-
+
+
-- cgit v1.2.3