This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.python.client import device_lib | |
print(device_lib.list_local_devices()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#preference is install for scrip runfile local. dont use apt | |
sudo apt purge nvidia-396 nvidia-* | |
sudo /etc/init.d/sddm stop | |
sudo /etc/init.d/gdm stop | |
sudo killall Xorg | |
sudo apt purge xserver-xorg-video-nouveau* | |
sudo vim /etc/modprobe.d/blacklist-nouveau.conf | |
#blacklist nouveau | |
#options nouveau modeset=0 | |
sudo sh ./cuda_9.2.148_396.37_linux.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//make sure to have textField outlet | |
let vector = spam.tfidf(sentence: self.textField.text ?? "") | |
let mlarray = spam.multiarray(vector: vector) | |
let res = try model.prediction(message: mlarray) | |
print(res.spam_or_not) | |
print(res.classProbability) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func multiarray(vector:[Int:Double]) -> MLMultiArray { | |
let array = try! MLMultiArray(shape: [NSNumber(integerLiteral: self.vocabulary.count)], dataType: .double) | |
for (key, value) in vector { | |
array[key] = NSNumber(floatLiteral: value) | |
} | |
return array | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func idf(word:String) -> Double { | |
if let pos = self.vocabulary[word] { | |
return self.idf[pos] | |
} else { | |
return Double(0.0) | |
} | |
} | |
func tfidf(sentence:String) -> [Int:Double] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func countVector(sentence:String) -> [Int:Int]? { | |
var vec = [Int:Int]() | |
for word in self.tokenize(sentence) { | |
if let pos = self.vocabulary[word] { | |
if let i = vec[pos] { | |
vec[pos] = i+1 | |
} else { | |
vec[pos] = 1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tokenize(_ message:String) -> [String] { | |
let trimmed = message.lowercased().trimmingCharacters(in: CharacterSet(charactersIn: "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")) | |
let tokens = trimmed.components(separatedBy: CharacterSet.whitespaces) | |
return tokens | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init() { | |
let wordsPath = Bundle.main.url(forResource:"words_array", withExtension:"json") | |
do { | |
let wordsData = try Data(contentsOf: wordsPath!) | |
if let wordsDict = try JSONSerialization.jsonObject(with: wordsData, options: []) as? [String:Int] { | |
self.vocabulary = wordsDict | |
} | |
} catch { | |
fatalError("oops could not load words_array") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Spam { | |
var idf = [Double]() | |
var vocabulary = [String:Int]() | |
var norm:Bool = true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import coremltools | |
coreml_model = coremltools.converters.sklearn.convert(spam_detector, "message", "spam_or_not") | |
#set parameters of the model | |
coreml_model.short_description = "Classify whether message is spam or not" | |
coreml_model.input_description["message"] = "TFIDF of message to be classified" | |
coreml_model.output_description["spam_or_not"] = "Whether message is spam or not" | |
#save the model | |
coreml_model.save("SpamMessageClassifier.mlmodel") |