Below is a sketch of a small C++ program to implement simple fixed-size perceptr
ID: 3782986 • Letter: B
Question
Below is a sketch of a small C++ program to implement simple fixed-size perceptrons (three nodes, two edges). It includes a main program that – once the functions are working correctly – will print the truth tables for AND, OR, and NAND! See the sections marked “TODO”. Here is the expected output:
AND:
0 0: 0
0 1: 0
1 0: 0
1 1: 1
OR:
0 0: 0
0 1: 1
1 0: 1
1 1: 1
NAND:
0 0: 1
0 1: 1
1 0: 1
1 1: 0
// Simple perceptron logic gate implementation
#include <ios stream>
using namespace std;
float step_function(float threshold, float value)
{
// TODO
return 0;
}
float run_perceptron(float weightA, float weightB, float valueA, float valueB, float threshold)
{
// TODO
return 0;
}
void show_truth_table(float weightA, float weightB, float threshold)
{
// TODO: call run_perceptron 4 times,
// feeding it all four values of A,B.
}
int main()
{
cout << "AND: ";
show_truth_table(0.4, 0.4, 0.5);
cout << "OR: ";
show_truth_table(0.6, 0.6, 0.5);
// TODO: can you figure out two weights and a threshold
// that will implement "NAND"? This is the opposite of AND.
cout << "NAND: ";
show_truth_table(0,0,0);
return 0;
}
Explanation / Answer
// Simple perceptron logic gate implementation
#include <iostream>
using namespace std;
//function which returns 0 or 1 based on value and threshold
float step_function(float threshold, float value)
{
if (value>threshold)
return 1;
else
return 0;
}
// this function returns 0 or 1 based on perceptron result
float run_perceptron(float weightA, float weightB, float valueA, float valueB, float threshold)
{
return step_function(threshold, weightA*valueA + weightB*valueB);
//return 0;
}
//Function to pass all possible combinations to run_perceptron
void show_truth_table(float weightA, float weightB, float threshold)
{
cout << "0 0: " << run_perceptron(weightA,weightB,0,0,threshold) << " ";
cout << "0 1: " << run_perceptron(weightA,weightB,0,1,threshold) << " ";
cout << "1 0: " << run_perceptron(weightA,weightB,1,0,threshold) << " ";
cout << "1 1: " << run_perceptron(weightA,weightB,1,1,threshold) << " ";
}
int main()
{
cout << "AND: ";
show_truth_table(0.4, 0.4, 0.5);
cout << "OR: ";
show_truth_table(0.6, 0.6, 0.5);
cout << "NAND: ";
// This function call will generate truth table for NAND
show_truth_table(-0.4,-0.4,-0.5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.