def predict(self): weightDict, biasDict = self.getWeightBiasDictionaries() model = MoodNeuralNetwork(weights=weightDict, biases=biasDict) input_data, mood_data = self.transformUserData(1) output = model.feedforward(input_data[0]) mood = model.roundClass(output) return mood
def test_setEmotions(self): emotions = ['Love', 'Sad', 'Hesitant', 'Calm', 'Happy'] model1 = MoodNeuralNetwork() emotions2 = model1.getEmotions() self.assertNotEqual(emotions, emotions2) self.assertTrue(model1.setEmotions(emotions)) self.assertEqual(emotions, model1.getEmotions())
def test_setBias(self): biasDict = {} for i in range(21): biasDict['bias' + str(i)] = np.random.normal() model = MoodNeuralNetwork() self.assertNotEqual(biasDict, model.getBiases()) self.assertTrue(model.setBias(biasDict)) self.assertEqual(biasDict, model.getBiases())
def test_setWeights(self): weightDict = {} for i in range(208): weightDict['weight' + str(i)] = np.random.normal() model = MoodNeuralNetwork() self.assertNotEqual(weightDict, model.getWeights()) self.assertTrue(model.setWeights(weightDict)) self.assertEqual(weightDict, model.getWeights())
def setUp(self): model1 = MoodNeuralNetwork() weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = np.random.normal() if i < 21: biasDict['weight' + str(i)] = np.random.normal() model2 = MoodNeuralNetwork(weights=weightDict, biases=biasDict)
def test_loss(self): network = MoodNeuralNetwork() sample_data = np.array(list(range(100))) sample_real = [x + 0.1 for x in list(range(0,100))] sample_real = np.array(sample_real) self.assertEqual(0.009999999999999724, network.loss(sample_data, sample_real)) sample_real = list(range(0,100)) self.assertEqual(0, network.loss(sample_data, sample_real))
def test_getBiases(self): weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = np.random.normal() if i < 21: biasDict['bias' + str(i)] = np.random.normal() model = MoodNeuralNetwork(weights=weightDict, biases=biasDict) self.assertEqual(biasDict, model.getBiases()) self.assertTrue(model.getBiases())
def test_deriv_activation(self): network = MoodNeuralNetwork() self.assertEqual(0, network.deriv_activation(1)) self.assertEqual(0.25, network.deriv_activation(0.5)) self.assertEqual(0.5, network.activation(0)) self.assertEqual(-0.75, network.deriv_activation(-.5)) self.assertEqual(0.16000000000000003, network.deriv_activation(0.2)) self.assertEqual(-90, network.deriv_activation(10)) self.assertEqual(-2024.75, network.deriv_activation(-44.5)) self.assertEqual(0.09000000000000001, network.deriv_activation(0.1))
def test_activation(self): network = MoodNeuralNetwork() self.assertEqual(0.7310585786300049, network.activation(1)) self.assertEqual(0.6224593312018546, network.activation(0.5)) self.assertEqual(0.5, network.activation(0)) self.assertEqual(0.3775406687981454, network.activation(-.5)) self.assertEqual(0.549833997312478, network.activation(0.2)) self.assertEqual(0.9999546021312976, network.activation(10)) self.assertEqual(4.719495271526123e-20, network.activation(-44.5)) self.assertEqual(0.52497918747894, network.activation(0.1))
def setWeightsBias(self, biases_list=False): if biases_list: if len(biases_list) != 21: return False self.bias_int_list = ",".join(str(x) for x in biases_list) else: model = MoodNeuralNetwork() biasDict = model.getBiases() biases = [] for i in range(len(biasDict)): biases.append(biasDict["bias" + str(i)]) self.setWeightsBias(biases) return True
def setWeightsWeights(self, weights_list=False): if weights_list: if len(weights_list) != 208: return False self.weights_int_list = ",".join(str(x) for x in weights_list) else: model = MoodNeuralNetwork() weightDict = model.getWeights() weights = [] for i in range(len(weightDict)): weights.append(weightDict["weight" + str(i)]) self.setWeightsWeights(weights) return True
def predict(self): weightDict, biasDict = self.getWeightBiasDictionaries() model = MoodNeuralNetwork(weights=weightDict, biases=biasDict) input_data, mood_data = self.transformUserData(1) try: output = model.feedforward(input_data[0]) except: return -1, 0 mood = model.roundClass(output) self.updateMoodPrediction(mood) profile = self.user.profile obs = Observation.objects.filter( user__user__username=profile.user.username) print("\n\n\n\n", obs, "\n\n\n\n") return mood, len(obs)
def test_train(self): weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = i if i < 21: biasDict['bias' + str(i)] = i network = MoodNeuralNetwork(weights=weightDict, biases=biasDict) sample_data = np.array([[2,1,4,5,6,2,6,7,3,6,6]]) true = np.array([30]) prediction = network.feedforward(sample_data[0]) loss1 = network.loss(true, prediction) network.train(sample_data, true) prediction = network.feedforward(sample_data[0]) loss2 = network.loss(true, network.roundClass(prediction)) self.assertTrue(loss2 < loss1)
def test_roundClass(self): network = MoodNeuralNetwork() self.assertEqual(0, int(network.roundClass(0))) self.assertEqual(41, int(network.roundClass(0.999))) self.assertEqual(8, int(network.roundClass(0.2))) self.assertEqual(12, int(network.roundClass(0.3))) self.assertEqual(20, int(network.roundClass(0.5))) self.assertEqual(29, int(network.roundClass(0.7)))
def test_feedforward(self): weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = i / 8 if i < 21: biasDict['bias' + str(i)] = i / 8 network = MoodNeuralNetwork(weights=weightDict, biases=biasDict) sample_data = [2, 1, 4, 5, 6, 2, 6, 7, 3, 6, 6] self.assertEqual(1, int(network.feedforward(sample_data)[0])) weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = i / 208 if i < 21: biasDict['bias' + str(i)] = i / 208 network = MoodNeuralNetwork(weights=weightDict, biases=biasDict) sample_data = list(range(11)) self.assertEqual(0, int(network.feedforward(sample_data)[0]))
def retrain(self): weightDict, biasDict = self.getWeightBiasDictionaries() model = MoodNeuralNetwork(weights=weightDict, biases=biasDict) input_data, mood_data = self.transformUserData(7) model.train(input_data, mood_data) weightDict = model.getWeights() weights = [] for i in range(len(weightDict)): weights.append(weightDict["weight" + str(i)]) self.setWeightsWeights(weights) biasDict = model.getBiases() biases = [] for i in range(len(biasDict)): biases.append(biasDict["bias" + str(i)]) self.setWeightsBias(biases) return True
import json from mood_model.mood_neural_network import MoodNeuralNetwork baseModel = MoodNeuralNetwork() emotions = baseModel.getEmotions() emotion_map = {} for i in range(len(emotions)): emotion_map[emotions[i]] = i with open('notifications.txt', 'r') as file: # Use file to refer to the file object data = file.read().splitlines() print(data) reminders = {} curr = "" for i in range(len(data)): if data[i] in emotion_map: reminders[data[i]] = [] curr = data[i] else: reminders[curr].append(data[i]) print(reminders) print(reminders['Sad']) with open('notifications.json', 'w') as fp: json.dump(reminders, fp)
def test_normalize(self): network = MoodNeuralNetwork() data = np.array([list(range(11))]) normalized = network.normalize(data) for j in range(len(data[0])): self.assertEqual(data[0][j], normalized[0][j])
def test_getEmotions(self): emotions = getEmotions() model1 = MoodNeuralNetwork() emotions2 = model1.getEmotions() self.assertEqual(emotions, emotions2) self.assertTrue(model1.getEmotions())
def test_feedforward(self): weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = i/8 if i < 21: biasDict['bias' + str(i)] = i/8 network = MoodNeuralNetwork(weights=weightDict, biases=biasDict) sample_data = [2,1,4,5,6,2,6,7,3,6,6] self.assertEqual(41, network.roundClass(network.feedforward(sample_data))) weightDict, biasDict = {}, {} for i in range(208): weightDict['weight' + str(i)] = i/208 if i < 21: biasDict['bias' + str(i)] = i/208 network = MoodNeuralNetwork(weights=weightDict, biases=biasDict) sample_data = list(range(11)) self.assertEqual(39, network.roundClass(network.feedforward(sample_data))) network = MoodNeuralNetwork() sample_data = [4.51,0,0,0,0,0,0,0,-1,8,35] self.assertEqual(25, network.roundClass(network.feedforward(sample_data))) sample_data = [15,25,50,0,0,0,0,0,-1,-100,0] self.assertEqual(33, network.roundClass(network.feedforward(sample_data)))