# Perceptrons as Logical Operators

## AND Perceptron

#### What are the weights and bias for the AND perceptron?



Set the weights (weight1, weight2) and bias (bias) to values that will correctly determine the AND operation as shown above. 

More than one set of values will work!


```python
import pandas as pd

# TODO: Set weight1, weight2, and bias
weight1 = 0.8 
weight2 = 0.8
bias = -1


# DON'T CHANGE ANYTHING BELOW
# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [False, False, False, True]
outputs = []

# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
    linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
    output = int(linear_combination >= 0)
    is_correct_string = 'Yes' if output == correct_output else 'No'
    outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])

# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', '  Input 2', '  Linear Combination', '  Activation Output', '  Is Correct'])
if not num_wrong:
    print('Nice!  You got it all correct.\n')
else:
    print('You got {} wrong.  Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))

```

    Nice!  You got it all correct.
    
    Input 1    Input 2    Linear Combination    Activation Output   Is Correct
          0          0                  -1.0                    0          Yes
          0          1                  -0.2                    0          Yes
          1          0                  -0.2                    0          Yes
          1          1                   0.6                    1          Yes


## OR Perceptron



The OR perceptron is very similar to an AND perceptron. In the image below, the OR perceptron has the same line as the AND perceptron, except the line is shifted down. What can you do to the weights and/or bias to achieve this? Use the following AND perceptron to create an OR Perceptron.


          
The two ways to go from an AND perceptron to an OR perceptron

- Increase the weights
- Decrease the magnitude of the bias



```python
import pandas as pd

# TODO: Set weight1, weight2, and bias
weight1 = 1.1
weight2 = 1.1
bias = -1

#weight1 = 0.8
#weight2 = 0.8
#bias = -0.5 


# DON'T CHANGE ANYTHING BELOW
# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [False, True, True, True]
outputs = []

# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
    linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
    output = int(linear_combination >= 0)
    is_correct_string = 'Yes' if output == correct_output else 'No'
    outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])

# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', '  Input 2', '  Linear Combination', '  Activation Output', '  Is Correct'])
if not num_wrong:
    print('Nice!  You got it all correct.\n')
else:
    print('You got {} wrong.  Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))

```

    You got 1 wrong.  Keep trying!
    
    Input 1    Input 2    Linear Combination    Activation Output   Is Correct
          0          0                   0.0                    1           No
          0          1                   1.1                    1          Yes
          1          0                   1.1                    1          Yes
          1          1                   2.2                    1          Yes


## NOT Perceptron



Unlike the other perceptrons we looked at, the NOT operation only cares about one input. The operation returns a 0 if the input is 1 and a 1 if it's a 0. The other inputs to the perceptron are ignored.

In this quiz, you'll set the weights (weight1, weight2) and bias bias to the values that calculate the NOT operation on the second input and ignores the first input.


```python
import pandas as pd

# TODO: Set weight1, weight2, and bias
weight1 = 0.5
weight2 = -0.7
bias = 0.0


# DON'T CHANGE ANYTHING BELOW
# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [True, False, True, False]
outputs = []

# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
    linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
    output = int(linear_combination >= 0)
    is_correct_string = 'Yes' if output == correct_output else 'No'
    outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])

# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', '  Input 2', '  Linear Combination', '  Activation Output', '  Is Correct'])
if not num_wrong:
    print('Nice!  You got it all correct.\n')
else:
    print('You got {} wrong.  Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))
```

    Nice!  You got it all correct.
    
    Input 1    Input 2    Linear Combination    Activation Output   Is Correct
          0          0                   0.0                    1          Yes
          0          1                  -0.7                    0          Yes
          1          0                   0.5                    1          Yes
          1          1                  -0.2                    0          Yes


## XOR Perceptron



#### Build an XOR Multi-Layer Perceptron
Now, let's build a multi-layer perceptron from the AND, NOT, and OR perceptrons to create XOR logic!

The neural network below contains 3 perceptrons, A, B, and C. The last one (AND) has been given for you. The input to the neural network is from the first node. The output comes out of the last node.

The multi-layer perceptron below calculates XOR. Each perceptron is a logic operation of AND, OR, and NOT. However, the perceptrons A, B, and C don't indicate their operation. In the following quiz, set the correct operations for the perceptrons to calculate XOR.





#### Solution




## Perceptron Algorithm

Implement the perceptron algorithm to separate the following data (given in the file data.csv).



Recall that the perceptron step works as follows. For a point with coordinates (p,q)(p,q), label yy, and prediction given by the equation 

\begin{align}
\hat{y} = step(w_1x_1 + w_2x_2 + b)
\end{align}

- If the point is correctly classified, do nothing.
- If the point is classified positive, but it has a negative label, subtract αp,αq, and α from w_1, w_2,w_1,w_2, and bb respectively.
- If the point is classified negative, but it has a positive label, add αp,αq, and α to w_1, w_2,w_1,w_2, and bb respectively.

graph the solution that the perceptron algorithm gives you. It'll actually draw a set of dotted lines, that show how the algorithm approaches to the best solution, given by the black solid line.


```python
import numpy as np
# Setting the random seed, feel free to change it and see different solutions.
np.random.seed(42)

def stepFunction(t):
    if t >= 0:
        return 1
    return 0

def prediction(X, W, b):
    return stepFunction((np.matmul(X,W)+b)[0])

# TODO: Fill in the code below to implement the perceptron trick.
# The function should receive as inputs the data X, the labels y,
# the weights W (as an array), and the bias b,
# update the weights and bias W, b, according to the perceptron algorithm,
# and return W and b.
def perceptronStep(X, y, W, b, learn_rate = 0.01):
    # Fill in code
    for i in range(len(X)):
        y_hat = prediction(X[i],W,b)
        if y[i]-y_hat == 1:
            W[0] += X[i][0]*learn_rate
            W[1] += X[i][1]*learn_rate
            b += learn_rate
        elif y[i]-y_hat == -1:
            W[0] -= X[i][0]*learn_rate
            W[1] -= X[i][1]*learn_rate
            b -= learn_rate
    return W, b
    
# This function runs the perceptron algorithm repeatedly on the dataset,
# and returns a few of the boundary lines obtained in the iterations,
# for plotting purposes.
# Feel free to play with the learning rate and the num_epochs,
# and see your results plotted below.
def trainPerceptronAlgorithm(X, y, learn_rate = 0.01, num_epochs = 25):
    x_min, x_max = min(X.T[0]), max(X.T[0])
    y_min, y_max = min(X.T[1]), max(X.T[1])
    W = np.array(np.random.rand(2,1))
    b = np.random.rand(1)[0] + x_max
    # These are the solution lines that get plotted below.
    boundary_lines = []
    for i in range(num_epochs):
        # In each epoch, we apply the perceptron step.
        W, b = perceptronStep(X, y, W, b, learn_rate)
        boundary_lines.append((-W[0]/W[1], -b/W[1]))
    return boundary_lines

```

3660a357fdddca2173402e77427b338e345adbc4
ipynb
Jupyter Notebook
Lesson_2/Perceptrons.ipynb
Vector202/PyTorch-Scholarship-Challenge-1
ccc944b54cd455a0d62475f1c73d365492cbb3f5
MIT
2018-11-24T10:47:31.000Z
2020-01-24T17:58:59.000Z
Lesson_2/Perceptrons.ipynb
Vector202/PyTorch-Scholarship-Challenge-1
ccc944b54cd455a0d62475f1c73d365492cbb3f5
MIT
Lesson_2/Perceptrons.ipynb
Vector202/PyTorch-Scholarship-Challenge-1
ccc944b54cd455a0d62475f1c73d365492cbb3f5
MIT
2018-12-08T23:10:07.000Z
2020-12-23T06:08:57.000Z
Qwen/Qwen-72B
1. YES
2. YES