created README.md
Browse files
README.md
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
library_name: pytorch
|
4 |
+
---
|
5 |
+
|
6 |
+
# or
|
7 |
+
|
8 |
+
A neuron that performs the OR logical computation.
|
9 |
+
|
10 |
+
It is inspired by McCulloch & Pitts' 1943 paper 'A Logical Calculus of the Ideas Immanent in Nervous Activity'.
|
11 |
+
|
12 |
+
It doesn't contain any parameters.
|
13 |
+
|
14 |
+
It takes as input two column vectors of zeros and ones. It outputs a single column vector of zeros and ones.
|
15 |
+
|
16 |
+
Its mechanism is outlined in Figure 10-3 of Aurelien Geron's book 'Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow'.
|
17 |
+
|
18 |
+
![](https://raw.githubusercontent.com/sambitmukherjee/handson-ml3-pytorch/main/chapter10/Figure_10-3.png)
|
19 |
+
|
20 |
+
Like all the other neurons in Figure 10-3, it is activated when at least two of its input connections are active.
|
21 |
+
|
22 |
+
Code: https://github.com/sambitmukherjee/handson-ml3-pytorch/blob/main/chapter10/logical_computations_with_neurons.ipynb
|
23 |
+
|
24 |
+
## Usage
|
25 |
+
|
26 |
+
```
|
27 |
+
import torch
|
28 |
+
import torch.nn as nn
|
29 |
+
from huggingface_hub import PyTorchModelHubMixin
|
30 |
+
|
31 |
+
# Let's create two column vectors containing `0`s and `1`s.
|
32 |
+
batch = {'a': torch.tensor([[0], [0], [1], [1]]), 'b': torch.tensor([[0], [1], [0], [1]])}
|
33 |
+
|
34 |
+
class OR(nn.Module, PyTorchModelHubMixin):
|
35 |
+
def __init__(self):
|
36 |
+
super().__init__()
|
37 |
+
self.operation = "C = A OR B"
|
38 |
+
|
39 |
+
def forward(self, x):
|
40 |
+
a = x['a']
|
41 |
+
b = x['b']
|
42 |
+
inputs = torch.cat([a, a, b, b], axis=1)
|
43 |
+
column_sum = torch.sum(inputs, dim=1, keepdim=True)
|
44 |
+
output = (column_sum >= 2).long()
|
45 |
+
return output
|
46 |
+
|
47 |
+
# Instantiate:
|
48 |
+
logical_or = OR.from_pretrained("sadhaklal/or")
|
49 |
+
|
50 |
+
# Forward pass:
|
51 |
+
output = logical_or(batch)
|
52 |
+
print(output)
|
53 |
+
```
|