zallarak
zallarak

Reputation: 5515

A simple perceptron in Python

http://en.wikipedia.org/wiki/Perceptron#Example

My question is, why are there 3 input values in each vector when NAND only takes 2 parameters and returns 1:

http://en.wikipedia.org/wiki/Sheffer_stroke#Definition

Pasted code for your convenience:

th = 0.1
learning_rate = 0.1
weights = [0, 0, 0]
training_set = [((1, 0, 0), 1), ((1, 0, 1), 1), ((1, 1, 0), 1), ((1, 1, 1), 0)]

def sum_function(values):
    return sum(value * weights[index] for index, value in enumerate(values))

while True:
    print '-' * 60
    error_count = 0
    for input_vector, desired_output in training_set:
        print weights
        result = 1 if sum_function(input_vector) > th else 0
        error = desired_output - result
        if error != 0:
            error_count += 1
            for index, value in enumerate(input_vector):
                weights[index] += learning_rate * error * value
    if error_count == 0:
        break

Upvotes: 2

Views: 5324

Answers (4)

beoliver
beoliver

Reputation: 5759

It is because you need one value that is a constant input. - also known as a bias.

If you notice you have three weights as well, so the first item in the triple ( which seems to always be 1 ) should be seen as 'input 0', (the bias). This is a constant.

I would suggest having a look at these videos on youtube: simple explanation of neural networks

hope this helps

Upvotes: 2

Satwik Bhamidi
Satwik Bhamidi

Reputation: 21

In this problem, we are trying to learn the NAND function. And therefore the input vector is (a,b,a NAND b) and the desired output is whether this input combination is correct or wrong, i-e whether it is a correct representation of NAND function.

For example ((1,1,1), 0) means 1 NAND 1 = 1 is wrong and therefore it is classified as 0(Wrong). ((1,1,0), 1) means 1 NAND 1 = 0 is correct and therefore it is classified as 1(Correct).

And as other combinations are added as part of training data then the classifier will learn NAND function. Following are a few more tuples that can be added to training data.

((0,0,1),1)
((0,1,1),1)
((1,0,1),1)
((0,0,0),0)
((0,1,0),0)
((1,0,0),0)

Upvotes: 1

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

My answer is in Java, I am just getting into Python so I will update this answer when I get around to it.

boolean nand(boolean[] a) {
  boolean b = true
  if (a.length > 1) {
    b = a[0] && a[1];
    if (a.length > 2)
      for (int i = 2; i < a.length; i++)
        b &= a[i];
  }
  return !b;
}

Usage (Should return true):

nand(training_set[i][0]) == training_set[i][1]

Edit

Over two years later, I have come back to this question to add to my answer...

I have created an iterative and recursive solution in python. I have also attempted to code golf the recursive. I got it down to 106 bytes.

Implementations

Iterative

def nand(*a):
    if len(a) > 1:
        b = a[0] and a[1]
        if len(a) > 2:
            for i in range(2, len(a)):
                b = b and a[i]
        return not b
    return None

Recursive

def nand(*a):
    if len(a) < 2:
        return None
    elif len(a) == 2:
        return not (a[0] and a[1])
    else:
        return nand(*([a[0] and a[1]] + list(a[2:])))

Recursive (lambda)

nand=lambda*a:None if len(a)<2 else not(a[0]and a[1])if len(a)==2 else nand(*([a[0]and a[1]]+list(a[2:])))

Results

print nand(True,  True,  True)   #  ¬(1 ∧ 1 ∧ 1) == 0
print nand(True,  True,  False)  #  ¬(1 ∧ 1 ∧ 0) == 1
print nand(True,  False, True)   #  ¬(1 ∧ 0 ∧ 1) == 1
print nand(True,  False, False)  #  ¬(1 ∧ 0 ∧ 0) == 1
print nand(False, True,  True)   #  ¬(0 ∧ 1 ∧ 1) == 1
print nand(False, True,  False)  #  ¬(0 ∧ 1 ∧ 0) == 1
print nand(False, False, True)   #  ¬(0 ∧ 0 ∧ 1) == 1
print nand(False, False, False)  #  ¬(0 ∧ 0 ∧ 0) == 1

Upvotes: 0

jmkelm08
jmkelm08

Reputation: 687

NAND can take as many arguments as you would like. Simply AND all of the input values together and negate the output.

// 2 arguments
!(A & B)

// 3 arguments
!(A & B & C)

// n argument
!(A & B & ... & n)

Upvotes: -1

Related Questions