ML | Encrypting Words Using Keras

Libraries used:

  • TensorFlow: Learn to install TensorFlow from here
  • NumPy: Learn to install NumPy from here
  • Code: Import libraries

    # importing libraries

    import tensorflow as tf

    import numpy as np 

    from tensorflow import keras

    We will also use Keras — a neural network library written in Python and added to the Tensorflow library.

    Model operation:
    The purpose of this example — get the input code from the user and then encrypt it so that no one can understand it without knowing his key. Here we will see the message character by character and replace it with a character that has the ASCII value of 1 + the ASCII value of the given character. But first of all, we will create a neural structure that will do this work for us. We`ll call it model here and define it with Keras. Here keras.Sequential defines that our model will be sequential or that the model is a linear stack of layers, and then we define the type of layer (we will use a tightly coupled neural network (NN) layer) with a number of layers, or units we want. Finally, we will give the desired number of neurons as input_shape and compile the model with an optimizer and loss. Optimizer and Loss — these are two functions that will help our model work in the right direction. Loss will calculate the error of the prediction made by our model, and the optimizer will check for this error and move the model in the direction that the error decreases.

    Code: Create a sequential model

    # creating the simplest neural structure with only
    # one layer and only one neuron

    model = keras.Sequential ([keras.layers.Dense (units = 1

    input_shape = [ 1 ])])

     
    # compiled no models with an optimizer for prediction and
    # loss to keep checking the accuracy of predictions

    model. compile (optimizer = `sgd` , loss = ` mean_squared_error` )

    After that, we will specify our training data as a Numpy array that will connect all x values ​​to the corresponding y, and then we will train our model on the given data using model.fit . Here Epochs will indicate how many times our model will run on the training data to improve its accuracy with Loss and Optimizer.


    Code:

    # here we will give an example of data with which program
    # will learn and make predictions based on it

    xs = np.array ([ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ], 

      dtype = float )

    ys = np.array ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 < code class = "plain">, 11 ], 

    dtype = float )

     
    # we will put the data into the model and epoch
    # the number of times the model will run sample data for
    # improve its accuracy

    model.fit (xs, ys, epochs = 1000 )

    Finally, we will take input from the user and split it into characters using a tuple function, then we store all those characters in a list. We will now create a for loop and send each character one at a time in our model to make a prediction, and we will print all predictions so that together they form an encrypted message that will not be easy to understand until you know the key to This.

    Code :

    # accept user input

    code = input ( "code is:" )

    n = len (code)

     
    # dividing the input message into its characters

    c = tuple (code)

    d = list (c)

     
    # predict the output for each character and print it

    for i in range ( 0 , n):

    s = ord (d [ i]) 

    x = model.predict ([s])

    print ( chr (x), end = " ")

      

    Full implementation —

    # importing libraries

    import tensorflow as tf

    import numpy as np 

    from tensorflow import keras

     
    # creating the simplest neural structure
    # with only one layer and one neuron

    model keras.Sequential ([keras.layers.Dense (units = 1

    input_shape = [ 1 ])] )

    # compiling a model with an optimizer for prediction and
    # loss to keep checking prediction accuracy

    model. compile (optimizer = `sgd` , loss = `mean_squared_error` )

      
    # here we give an example of data with which The program will learn
    # and make predictions based on it

    xs = np.array ([ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ], dtype = float )

    ys = np.array ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ], dtype = float )

     
    # we will put data into the model, and the number of epochs
    # times the model will run sampled data to improve accuracy < / code>

    model.fit (xs, ys, epochs = 1000 )

      
    # accept user input

    code = " Vaibhav Mehra writing for Python.Engineering "

    n = len (code)

     
    # dividing the input message into its characters

    c = tuple (code)

    d = list (c)

     
    # predict the output for each character and print it

    for i in range ( 0 , n):

    s = ord (d [i]) 

    x = model.predict ([s])

    print ( chr (x), end = " ")

    Output:

     Wbjcibw! Nfisb! xsjujoh! gps! HffltgpsHfflt