import numpy as np import matplotlib.pyplot as plt %matplotlib inline XOR GATE Exercise 1: Please fix the exercise below¶ Try to combine the gates we discussed. It's already if you do so by trial and...

1 answer below »


import numpy as np



import matplotlib.pyplot as plt



%matplotlib inline






XOR GATE Exercise 1: Please fix the exercise below




Try to combine the gates we discussed. It's already if you do so by trial and error. To help you out, the code below specifies that our combination first passes the inputs to two separate hidden gates or hidden neurons, and then passes the outcome of that to a single output neuron

In[26]:

nand_gate = Neuron(w1=-1, w2=-1, b=2)
In[27]:

nor_gate = Neuron(w1= -1, w2= -1, b= 1)
In[28]:

and_gate = Neuron(w1=1, w2=1, b=-1)
In[29]:

or_gate = Neuron(w1=2, w2=4, b=-1)

make_truth_table(or_gate)

0, 0: 0.0 0, 1: 1.0 1, 0: 1.0 1, 1: 1.0
In[46]:
# Uncomment the xor_gate line and find out which neurons besides the or_gate neuron the

# network should have in its hidden and output layer to produce the right values.


class Network():




def __init__(self, gate1, gate2, out_gate):


self.hidden_neuron1 = gate1


self.hidden_neuron2 = gate2


self.out_neuron = out_gate




def activate(self, x1, x2):


z1 = self.hidden_neuron1.activate(x1, x2)


z2 = self.hidden_neuron2.activate(x1, x2)


return self.out_neuron.activate(z1, z2)



xor_gate = Network(nor_gate,nand_gate, and_gate) #tried alll gates here ....., cannot get output to show value above

make_truth_table(xor_gate)

0, 0: 1.0 0, 1: 1.0 1, 0: 1.0 1, 1: 0.0







XOR GATE xercise 2: Please complete/add the missing code





Finish this version of an XOR gate that more closely resembles a neural network by determining the shapes the weights and biases need to have.

In[194]:

W1 = np.array(...)

b1 = np.array(...)


W2 = np.array(...)

b2 = np.array(...)


hidden_layer = Layer(W1, b1)

output_layer = Layer(W2, b2)

--------------------------------------------------------------------------- IndexError                                Traceback (most recent call last)  in        5 b2 = np.array(...)       6  ----> 7 hidden_layer = Layer(W1, b1)       8 output_layer = Layer(W2, b2)   in __init__(self, W, b)       2        3   def __init__(self, W, b): ----> 4     self.m = W.shape[0]       5     self.n = W.shape[1]       6     self.W = W  IndexError: tuple index out of range
In[]:

class Network():




def __init__(self, hidden, output):


self.hidden = hidden


self.output = output




def activate(self, X):


z = self.hidden.activate(X)


return self.output.activate(z)


xor_gate = Network(hidden_layer, output_layer)


xor_output = xor_gate.activate(X)

np.round(xor_output)










Answered 1 days AfterJun 16, 2021

Answer To: import numpy as np import matplotlib.pyplot as plt %matplotlib inline XOR GATE Exercise 1: Please...

Vibhav answered on Jun 18 2021
142 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "a8YglsAarO23"
},
"source": [
"# The power of neural networks\n",
"\n",
"Neurons can be used to model logic gates, the building bl
ocks behind all digital computing. In this compulsory task we talk you through how to do so. We also explain how to represent neural networks in terms of matrix computations."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "nyr9_Tu6rO27"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "7-mcTJnprO3I"
},
"source": [
"## Neurons as logic gates\n",
"\n",
"A neuron works by applying an activation function, usually the sigmoid function, to a combination of inputs, input weights and a bias. "
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Ft6TavXAWSS0"
},
"outputs": [],
"source": [
"class Neuron():\n",
" \n",
" def __init__(self, W, b):\n",
" self.W = W\n",
" self.b = b\n",
" \n",
" def activate(self, X):\n",
" return sigmoid(W * X + b)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "H3pcg1o5XVuN"
},
"source": [
"Here's a reminder of what the sigmoid function is and what it's output looks like:\n",
"\n",
"$$\n",
"\\sigma = \\frac{1}{1 + e^{-x}}\n",
"$$\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 286
},
"colab_type": "code",
"executionInfo": {
"elapsed": 617,
"status": "ok",
"timestamp": 1564935493778,
"user": {
"displayName": "Esther van den Berg",
"photoUrl": "",
"userId": "04126586416623518388"
},
"user_tz": -120
},
"id": "7WrC8RUprO3L",
"outputId": "4abfec7a-65ec-4c5f-b5f7-dee85c7a37ba"
},
"outputs": [
{
"data": {
"text/plain": [
"(-10, 10)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here