<jupyter_start><jupyter_text>Yesil Science - Machine Learning Workshop -  Yeditepe University IEEE WIE - 24.11.2019  Simple Artificial Neural Network  Import libraries<jupyter_code>import decimal
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Main<jupyter_code>class ANN:

    def __init__(self):
       
        self.weights = 2 * np.random.random((4, 1)) - 1
        print("Weights: ", self.weights)

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return x * (1 - x)

    def predict(self, inputs):
        # e.g. [1,0,0,1]
        inputs = inputs.astype(float)
        output = self.sigmoid(np.dot(inputs, self.weights))
        return output

    def train(self, training_inputs, training_outputs, number_of_iterations):
        for self.iteration in range(number_of_iterations):
            output = self.predict(training_inputs)
            self.error = training_outputs - output
            adjustments = np.dot(training_inputs.T, self.error * self.sigmoid_derivative(output))
            self.weights += adjustments


if __name__ == "__main__":
    ann = ANN()

    print("First weights:  {}".format(ann.weights))
   
    iterations = 10000

    training_inputs = np.array([[1, 0, 0, 1],
                                [0, 1, 1, 0],
                                [1, 0, 1, 0],
                                [0, 0, 0, 1]])

    training_outputs = np.array([[1],
                                 [0],
                                 [1],
                                 [0]])

    ann.train(training_inputs, training_outputs, iterations)


    print("Weights after training:  {}".format(ann.weights))


    input = np.array([0, 0, 1, 0])
    output = ann.predict(input)  # e.g. 0.9989834

    result = decimal.Decimal(output[0]).quantize(decimal.Decimal('1'), rounding=decimal.ROUND_HALF_UP)
    print("The answer for {} is: {}".format(input, result))<jupyter_output>Weights:  [[-0.57323111]
 [-0.23653979]
 [ 0.31048407]
 [ 0.94200269]]
First weights:  [[-0.57323111]
 [-0.23653979]
 [ 0.31048407]
 [ 0.94200269]]
Weights after training:  [[ 8.94528437]
 [-3.70704292]
 [-1.56727369]
 [-4.36900959]]
The answer for [0 0 1 0] is: 0
{'fraction_non_alphanumeric': 0.1349911190053286, 'fraction_numerical': 0.07593250444049733, 'mean_word_length': 2.265217391304348, 'pattern_counts': {'":': 1, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '229406', 'license': 'MIT', 'n_tokens_mistral': 869, 'n_tokens_neox': 743, 'n_words': 164, 'path': 'week2 - yeditepe/Workshop - Yeditepe - ANN.ipynb', 'repo_name': 'mustafamerttunali/YesilScience-AI-Workshop', 'size': '2252'}