Serendip is an independent site partnering with faculty at multiple colleges and universities around the world. Happy exploring!

You are here

Question about using neural networks...

SunnySingh's picture
Projects: 
In my final project, I want to take several cryptosystems/encryption algorithms and see if it's possible for a neural network to recognize the pattern. If all goes to plan, my dataset will consist of a ciphertext and it's plaintext equivalent; this is what I'd like to train my network with. I'll test simple substitution/transposition ciphers and maybe even ones that rely on the factoring of large primes (ie RSA); however, I highly doubt the network will be able to 'crack' the latter. If time permits, I may also explore the idea of training two networks on one another in an attempt to create an encryption/decryption system. Anyway, I'm running into a problem when I try to set the inputs and outputs for the neural network. Here's the block of code that's giving me problems: ------------------------------------------------------- n.setInputs("HELLO","NEURAL NETWORKS","HOPELESS","NEW JERSEY","QUARTER","PYTHON","A QUICK BROWN FOX JUMPED OVER THE LAZY DOGS") n.setOutputs("WTAAD","CTJGPA CTILDGZH","WDETATHH","CTL YTGHTN","FJPGITG","ENIWDC","P FJXRZ QGDLC UDM YJBETS DKTG IWT APON SDVH") ------------------------------------------------------- The error: [ssingh@arneb FinalProject]$ python CipherNN.py Traceback (most recent call last): File "CipherNN.py", line 10, in ? n.setInputs("HELLO","NEURAL NETWORKS","HOPELESS","NEW JERSEY","QUARTER","PYTHON","A QUICK BROWN FOX JUMPED OVER THE LAZY DOGS") TypeError: setInputs() takes exactly 2 arguments (8 given) ------------------------------------------------------- Line 10 refers to the n.setInputs(...) line. Is it necessary for the input to contain 2 arguements? What other argument could I use in addition to the ciphertext? It SHOULD be possible to train the network on characters, right? Is there something that needs to be modified in the conx.py file?? Someone help!

Comments

JoshCarp's picture

So: setInputs() takes an array of floats or ints. See the setInputs() method in the source; also verifyArguments(): (For whatever reason, indents don't look right here.) def verifyArguments(self, arg): """ Verifies that arguments to setInputs and setTargets are appropriately formatted. """ for l in arg: if not type(l) == list and \ not type(l) == type(Numeric.array([0.0])) and \ not type(l) == tuple: return 0 for i in l: if not type(i) == float and not type(i) == int: return 0 return 1
SunnySingh's picture

Hm...I can't really think straight because it's late...but is it even possible for me to make this work somehow?
Doug Blank's picture

Of course, in the end, you have to use numbers as inputs and targets for a neural network. So, you can do something like:

net.setInputs([[0, 1, 1, 0],
               [1, 0, 1, 0],
               [0, 0, 0, 1]])
net.setTargets([[0],
                [1],
                [0]])

That would work for a network that has 4 units in the input layer and 1 unit in the output layer. Notice that each of these is a list of lists.

If you want to use an advanced feature, you can use patterns, but this is really only a time saving device. You can replace a LIST of numbers with a symbol. For example, for the above example, you could:

net.setPatterns( {"john":   [0, 1, 1, 0],
                  "sally":  [1, 0, 1, 0],
                  "martha": [0, 0, 0, 1],
                  "yes": [1],
                  "no": [0]} )
net.setInputs(["john", "sally", "martha"])
net.setTargets(["no", "yes", "no"])

but you don't need to do that. Hope that helps.

jrohwer's picture

Yo Sandeep... I was going to do something like your project. Not necessarily encryption, but something with language--so something using text. Then I thought, "How will I make it accept character strings as input and how will I convert the output back?" And the answer was, Using some method that is too much work for me. The way it's set up, no, there is no easy way to do it, I don't think. ONE OF A COUPLE WAYS I CAN THINK OF: feed it n 6-bit numbers (cuz you need more than 16 characters, and not more than 32, right? So... 2 ^ 5), one each for the n characters in each input string. You can write some code to do that, so that part in itself isn't so bad, but adding to that will be the fact that the network now has to learn that every 6 bits represent a different character before it can even start deciphering codes. Then you would probably also have to train it to output the letters using the same method. ANOTHER WAY, MAYBE THIS ONE IS BETTER: Set a really low error tolerance and divide the space from 0 to 1 up into 26 pieces, and train the network that it has to localize it's outputs very close to each of those intervals of 1/26. Assuming 26 characters, that is. This might be a better way to go about it. Yeah. Good luck. Let me know how you finally decide to try to do it.