ML.NeuralNetwork
1from .model import Model 2from os import environ 3environ['TF_CPP_MIN_LOG_LEVEL'] = '1' 4 5from numpy import mean 6import tensorflow as tf 7from tensorflow.keras.preprocessing.text import Tokenizer 8from tensorflow.keras.preprocessing.sequence import pad_sequences 9# installing a whole module from req is NOT worth man ðŸ˜ðŸ˜ðŸ˜ 10from sklearn.model_selection import train_test_split 11import numpy as np 12 13class NeuralNetwork(Model): 14 def _preprocess(self, tweets): 15 """ 16 Builds a vocab map from vectorising every tweet in the test data. 17 Args: 18 tweets (list(str)): The list of tweets that need vectorising 19 Returns: 20 padded_sequences(list(int)) : Every tweet vectorised and standadized length 21 """ 22 # Build vocap map. 23 tokenizer = Tokenizer(num_words=5000, oov_token='<OOV>') 24 tokenizer.fit_on_texts(self.pos_data + self.neg_data) 25 26 #Vectorizes every tweet. 27 sequences = tokenizer.texts_to_sequences(tweets) 28 29 # Pads every tweet. 30 padded_sequences = pad_sequences(sequences, maxlen=100, truncating='post') 31 return padded_sequences 32 33 def _trainmodel(self): 34 """ 35 Using tensowflows keras model, using two dense ReLU activation functions, and 36 a sigmoid output layer. 37 Returns a model as NeuralNetwork.keras 38 """ 39 padded_sequences = self._preprocess(self.pos_data + self.neg_data) 40 labels = np.concatenate([np.ones(len(self.pos_data)), np.zeros(len(self.neg_data))]) 41 42 X_train, X_test, y_train, y_test = train_test_split(padded_sequences, labels, test_size=0.2, random_state=42) 43 44 model = tf.keras.Sequential([ 45 tf.keras.layers.Embedding(5000, 16, input_length=100), 46 tf.keras.layers.GlobalAveragePooling1D(), 47 tf.keras.layers.Dense(24, activation='relu'), 48 tf.keras.layers.Dense(1, activation='sigmoid') 49 ]) 50 51 # Compile the model 52 model.compile (loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 53 54 # Train the model 55 model.fit(X_train, y_train, epochs=7, batch_size=16, validation_data=(X_test, y_test)) 56 model.save(r'backend\ML\models\NeuralNetwork.keras') 57 58 async def predict(self, tweet): 59 """ 60 By using the trained keras file in models, classify a single tweet 61 Args: 62 tweet (str) : text data inside of tweet. 63 Returns: 64 'p' to indicate positive and 'n' to indicate negative 65 """ 66 print(f' predict is recieving: {tweet}') 67 68 tweet = self._preprocess(tweet) 69 model = tf.keras.models.load_model(r'backend\ML\models\NeuralNetwork.keras') 70 71 values = model.predict(tweet) 72 avg = mean(values) 73 return 'p' if avg > 0.5 else 'n'
14class NeuralNetwork(Model): 15 def _preprocess(self, tweets): 16 """ 17 Builds a vocab map from vectorising every tweet in the test data. 18 Args: 19 tweets (list(str)): The list of tweets that need vectorising 20 Returns: 21 padded_sequences(list(int)) : Every tweet vectorised and standadized length 22 """ 23 # Build vocap map. 24 tokenizer = Tokenizer(num_words=5000, oov_token='<OOV>') 25 tokenizer.fit_on_texts(self.pos_data + self.neg_data) 26 27 #Vectorizes every tweet. 28 sequences = tokenizer.texts_to_sequences(tweets) 29 30 # Pads every tweet. 31 padded_sequences = pad_sequences(sequences, maxlen=100, truncating='post') 32 return padded_sequences 33 34 def _trainmodel(self): 35 """ 36 Using tensowflows keras model, using two dense ReLU activation functions, and 37 a sigmoid output layer. 38 Returns a model as NeuralNetwork.keras 39 """ 40 padded_sequences = self._preprocess(self.pos_data + self.neg_data) 41 labels = np.concatenate([np.ones(len(self.pos_data)), np.zeros(len(self.neg_data))]) 42 43 X_train, X_test, y_train, y_test = train_test_split(padded_sequences, labels, test_size=0.2, random_state=42) 44 45 model = tf.keras.Sequential([ 46 tf.keras.layers.Embedding(5000, 16, input_length=100), 47 tf.keras.layers.GlobalAveragePooling1D(), 48 tf.keras.layers.Dense(24, activation='relu'), 49 tf.keras.layers.Dense(1, activation='sigmoid') 50 ]) 51 52 # Compile the model 53 model.compile (loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 54 55 # Train the model 56 model.fit(X_train, y_train, epochs=7, batch_size=16, validation_data=(X_test, y_test)) 57 model.save(r'backend\ML\models\NeuralNetwork.keras') 58 59 async def predict(self, tweet): 60 """ 61 By using the trained keras file in models, classify a single tweet 62 Args: 63 tweet (str) : text data inside of tweet. 64 Returns: 65 'p' to indicate positive and 'n' to indicate negative 66 """ 67 print(f' predict is recieving: {tweet}') 68 69 tweet = self._preprocess(tweet) 70 model = tf.keras.models.load_model(r'backend\ML\models\NeuralNetwork.keras') 71 72 values = model.predict(tweet) 73 avg = mean(values) 74 return 'p' if avg > 0.5 else 'n'
async def
predict(self, tweet):
59 async def predict(self, tweet): 60 """ 61 By using the trained keras file in models, classify a single tweet 62 Args: 63 tweet (str) : text data inside of tweet. 64 Returns: 65 'p' to indicate positive and 'n' to indicate negative 66 """ 67 print(f' predict is recieving: {tweet}') 68 69 tweet = self._preprocess(tweet) 70 model = tf.keras.models.load_model(r'backend\ML\models\NeuralNetwork.keras') 71 72 values = model.predict(tweet) 73 avg = mean(values) 74 return 'p' if avg > 0.5 else 'n'
By using the trained keras file in models, classify a single tweet Args: tweet (str) : text data inside of tweet. Returns: 'p' to indicate positive and 'n' to indicate negative