Libraries used:
Code: Import libraries
|
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
|
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:
|
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 :
|
Full implementation —
|
Output:
Wbjcibw! Nfisb! xsjujoh! gps! HffltgpsHfflt