Let us worry about your assignment instead!

We Helped With This MATLAB Programming Homework: Have A Similar One?

SOLVED
CategoryProgramming
SubjectMATLAB
DifficultyUndergraduate
StatusSolved
More InfoHelp With Matlab
103111

Short Assignment Requirements

Attached the requirement please kindly help me solve

Assignment Description

CSCI 410 Pattern Recognition

Assignment Four

by Denton Bobeldyk

 

 

1.       Using Matlab, create a multi-layer perceptron with 3 layers: input layer, hidden layer, output layer (using a sigmoid function).

2.       Define the learning rate and total iterations learningRate = 0.5; totalIterations = 500;

 

3.       Define the size of the input layer and the hidden layer:

inputLayerNumber = 2; hiddenLayerNumber = 2;

 

4.       Define the input and hidden layer:

inputLayer = zeros(inputLayerNumber, 1);

hiddenLayer = zeros(hiddenLayerNumber, 1);

 

 

5.       Add the bias to the input and hidden layer:

inputLayerWithBias = zeros(inputLayerNumber + 1, 1); hiddenLayerWithBias = zeros(hiddenLayerNumber + 1, 1);

 

6.       Define the output layer:

outputLayer = 0;

7.       Randomly assign the weights to the input and hidden layer: inputLayerWeights = rand( (inputLayerNumber + 1) ,hiddenLayerNumber) - .5 ; hiddenLayerWeights = rand( (hiddenLayerNumber + 1), 1) - .5;

8.       Define the input data:

inputLayer = [0 0; 0 1; 1 0; 1 1];

 

9.       Define the target output for the input layer: ANDtargetOutput = [0; 0; 0; 1]; targetOutput = ANDtargetOutput;

10.   Define the variable `m’ as the number of samples:

m = size(targetOutput, 1);

 

11.   Create a for loop, that will step through each of the samples one at a time (Note: this is known as online learning)

 

for iter=1:totalIterations

    

     for i = 1:m;

 

        hiddenLayerActivation = inputLayerWithBias(i, :) * inputLayerWeights;         hiddenLayer = sigmoid(hiddenLayerActivation);

 

        %Add the bias to the hiddenLayer         hiddenLayerWithBias = [1, hiddenLayer];

            

        outputLayer = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

 

%Calculate the error:

deltaOutput = targetOutput(i) - outputLayer;

        deltaHidden(1) = (deltaOutput * hiddenLayerWeights(1)) .* ((hiddenLayerWithBias(1) * (1.0 - hiddenLayerWithBias(1))));         deltaHidden(2) = (deltaOutput * hiddenLayerWeights(2)) .* ((hiddenLayerWithBias(2) * (1.0 - hiddenLayerWithBias(2))));         deltaHidden(3) = (deltaOutput * hiddenLayerWeights(3)) .*

((hiddenLayerWithBias(3) * (1.0 - hiddenLayerWithBias(3)))); 

        % Fixed Step Gradient Descent - Update the weights

        hiddenLayerWeights(1) = hiddenLayerWeights(1) + (learningRate *

(deltaOutput * hiddenLayerWithBias(1)));

        hiddenLayerWeights(2) = hiddenLayerWeights(2) + (learningRate *

(deltaOutput * hiddenLayerWithBias(2)));

        hiddenLayerWeights(3) = hiddenLayerWeights(3) + (learningRate *

(deltaOutput * hiddenLayerWithBias(3)));

        

        %update each weight according to the part that they played         inputLayerWeights(1,1) = inputLayerWeights(1,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 1));

        inputLayerWeights(1,2) = inputLayerWeights(1,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 1));

        

        inputLayerWeights(2,1) = inputLayerWeights(2,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 2));

        inputLayerWeights(2,2) = inputLayerWeights(2,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 2));

        

        inputLayerWeights(3,1) = inputLayerWeights(3,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 3));

        inputLayerWeights(3,2) = inputLayerWeights(3,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 3));  

 

  

            end

     end

 

 

12.   Create the sigmoid function:

 

function a = sigmoid(z)

 

a = 1.0 ./ (1.0 + exp(-z));

  end

 

 

13.   Create the cost function:

% This function will only work for NN with just one output (k = 1) function [averageCost] = costFunction(inputLayerWithBias, inputLayerWeights, hiddenLayerWeights, targetOutput)

%Sum of square errors cost function m = 4;

hiddenLayer = sigmoid(inputLayerWithBias * inputLayerWeights);

 

hiddenLayerWithBias = [ones(m,1) hiddenLayer];

 

outputLayer = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

 

 

% Step through all of the samples and calculate the cost at each one for i=1:m

    cost(i) = (1/2) * ((outputLayer(i) - targetOutput(i)) .^ 2); end

  

%Sum up all of the individual costs totalCost = sum(cost);

 

%average them out

averageCost = totalCost * (1/m);

    end

 

 

 

 

14. Create a function that will summarize the output of the 4 samples:

 function outputSummary(inputLayerWithBias, inputLayerWeights, hiddenLayerWeights, targetOutput, totalIterations)

 

cost = costFunction(inputLayerWithBias, inputLayerWeights, hiddenLayerWeights, targetOutput);

   

hiddenLayer = sigmoid(inputLayerWithBias * inputLayerWeights);

 

%we have multiple samples, so we need to add the bias to each of them hiddenLayerWithBias = [ones(size(targetOutput,1),1) hiddenLayer];

 

actualOutput = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

 

   

fprintf('========================================= '); fprintf('Output Summary (after %d iterations): ', totalIterations); fprintf('Total Cost: [%f] ', cost);

    for i=1:length(actualOutput)     if(actualOutput(i) > 0.5)         thresholdedValue = 1;     else

        thresholdedValue = 0;     end     

    if(thresholdedValue == targetOutput(i))

        fprintf('Sample[%d]: Target = [%f] Thresholded Value = [%f] Actual= [%f] ', i, targetOutput(i), thresholdedValue, actualOutput(i));     else  % else print the error in red

        fprintf(2,'Sample[%d]: Target = [%f] Thresholded Value = [%f] Actual= [%f] ', i, targetOutput(i), thresholdedValue, actualOutput(i));     end          end   

fprintf('=========================================');

  end

 

 

 

 

 

 

 

15.   Attempt to learn the following target outputs:

ANDtargetOutput = [0; 0; 0; 1];

ORtargetOutput = [0; 1; 1; 1];

NANDtargetOutput = [1; 1; 1; 0];

NORtargetOutput = [1; 0; 0; 0];

XORtargetOutput = [0; 1; 1; 0];

 

16.   Which of the above target outputs does it have the hardest time learning and why?

 

 

 

Turn-in:

1.       A single matlab script that performs the above tasks.

2.       The answer to question 16 output using ‘fprintf’ commands in the above script.

3.       A word document containing the copy and pasted output of the script execution. Please make sure there are no line wrappings (decrease the font if necessary).

 

 

 

 

 

 

 

 

 

 

 

References:

Andrew NG, Machine Learning course from Coursera

Bishop, Christopher M. Neural networks for pattern recognition. Oxford university press, 1995.

 

Frequently Asked Questions

Is it free to get my assignment evaluated?

Yes. No hidden fees. You pay for the solution only, and all the explanations about how to run it are included in the price. It takes up to 24 hours to get a quote from an expert. In some cases, we can help you faster if an expert is available, but you should always order in advance to avoid the risks. You can place a new order here.

How much does it cost?

The cost depends on many factors: how far away the deadline is, how hard/big the task is, if it is code only or a report, etc. We try to give rough estimates here, but it is just for orientation (in USD):

Regular homework$20 - $150
Advanced homework$100 - $300
Group project or a report$200 - $500
Mid-term or final project$200 - $800
Live exam help$100 - $300
Full thesis$1000 - $3000

How do I pay?

Credit card or PayPal. You don't need to create/have a Payal account in order to pay by a credit card. Paypal offers you "buyer's protection" in case of any issues.

Why do I need to pay in advance?

We have no way to request money after we send you the solution. PayPal works as a middleman, which protects you in case of any disputes, so you should feel safe paying using PayPal.

Do you do essays?

No, unless it is a data analysis essay or report. This is because essays are very personal and it is easy to see when they are written by another person. This is not the case with math and programming.

Why there are no discounts?

It is because we don't want to lie - in such services no discount can be set in advance because we set the price knowing that there is a discount. For example, if we wanted to ask for $100, we could tell that the price is $200 and because you are special, we can do a 50% discount. It is the way all scam websites operate. We set honest prices instead, so there is no need for fake discounts.

Do you do live tutoring?

No, it is simply not how we operate. How often do you meet a great programmer who is also a great speaker? Rarely. It is why we encourage our experts to write down explanations instead of having a live call. It is often enough to get you started - analyzing and running the solutions is a big part of learning.

What happens if I am not satisfied with the solution?

Another expert will review the task, and if your claim is reasonable - we refund the payment and often block the freelancer from our platform. Because we are so harsh with our experts - the ones working with us are very trustworthy to deliver high-quality assignment solutions on time.

Customer Feedback

"Thanks for explanations after the assignment was already completed... Emily is such a nice tutor! "

Order #13073

Find Us On

soc fb soc insta


Paypal supported