Change language

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 
    
    
    
                       

    Shop

    Gifts for programmers

    Learn programming in R: courses

    $FREE
    Gifts for programmers

    Best Python online courses for 2022

    $FREE
    Gifts for programmers

    Best laptop for Fortnite

    $399+
    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best computer for crypto mining

    $499+
    Gifts for programmers

    Best laptop for Sims 4

    $

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers

    News


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically