Neural network: How to calculate the error for a unit - neural-network

I am trying to work out question 26 from this exam paper (the exam is from 2002, not one I'm getting marked on!)
This is the exact question:
The answer is B.
Could someone point out where I'm going wrong?
I worked out I1 from the previous question on the paper to be 0.982.
The activation function is sigmoid. So should the sum be, for output 1:
d1 = f(Ik)[1-f(Ik)](Tk-Zk)
From the question:
T1 = 0.58
Z1 = 0.83
T1 - Z1 = -0.25
sigmoid(I1) = sigmoid(0.982) = 0.728
1-sigmoid(I1) = 1-0.728 = 0.272
So putting this all together:
d1 = (0.728)(0.272)(-0.25)
d1 = -0.049
But the answer should be d1 = -0.0353
Can anyone show me where I'm going wrong?
Edit 1: I tried to work backwards to understand the situation, but I still got stuck.
I said:
d1 = f(Ik)[1-f(Ik)](Tk-Zk)
-0.0353 = f'(Ik)(-0.25) (where I know -0.0353 is the right answer, and -0.25 is Tk - Zk)
0.1412 = f'(Ik)
0.1412 = f(Ik)[1-f(Ik)]
0.1412 = sigmoid(x).(1-sigmoid(x))
...but then I got stuck, if anyone has an idea

The problem is, that the I₁ you got from the previous question is not the same I₁ you need for this task.
The value of I₁ changes depending on the input values(which are different for this question)!
For the solution of this question you can instead use the fact that f(Iₖ) = zₖ:
δₖ = f(Iₖ)·[1 - f(Iₖ)]·(tₖ - zₖ)
= zₖ·[1 - zₖ]·(tₖ - zₖ)
→ δ₁ = 0.83·[1 - 0.83]·(-0.25) = -0.2075·0.17 = -0.035275 ≈ -0.0353
→ δ₂ = 0.26·[1 - 0.26]·(0.70 - 0.26) ≈ -0.0847
→ δ₃ = 0.56·[1 - 0.56]·(0.20 - 0.56) ≈ -0.0887

Related

Undefined operator '>' for input arguments of type 'msspoly' (optimization software)

I know there are many similar questions like the title ask here.
However, I still believe no of these discussions can solve my problem.
I am testing the following software "SPOTless" developed by MIT:
https://github.com/spot-toolbox/spotless
This is a software which transform the polynomial optimization problem to LP or SOCP. And then call the other software "mosek" to solve it.
This example is from the tutorial in:
https://github.com/spot-toolbox/spotless/tree/master/doc (please see the last example)
n = 2 ;
d = 4 ;
x = msspoly('x',n ) ;
basis = monomials(x , 0 : d ) ;
p = randn(length(basis))'*basis ;
g = 1 - x'*x ;
prog = spotsosprog ;
prog = prog.withIndeterminate(x) ;
[prog,r] = prog.newFree(1) ;
[prog,f] = prog.newFreePoly(x,monomials(x,0:d-2)) ;
prog = prog.withSOS(r-p-f*g) ;
sol = prog.minimize(r) ;
However, when run it, Matlab shows the following error:
The line 401 and the related part is the following:
function [pr,poly,coeff] = newFreePoly(pr,basis,n)
if nargin < 3, n = 1; end
if ~isempty(basis) && n > 0 % line 401
if ~pr.isPolyInIndet(basis)
error('Basis must be polynomial in the indeterminates.');
end
[pr,coeff] = pr.newFree(length(basis)*n);
poly = reshape(coeff,n,length(basis))*basis;
else
poly = [];
end
end
$n$ is an integer, so $n > 0$ is valid obviously.
I ask the related professor (not the author of this software); he told me he has not seen this problem before. The problem comes from one .m file in the software.
I am not sure if someone can help me test this software and this example and let me know the result or how to solve this error.
Note: if you really test this software (easy to install), please also install "mosek", which is a famous optimization software.
Note: if you do not want to install "mosek", please cross out the last two lines in the code.

Aligning the dimensions for conformity in matlab

A number of times, I run this simple code. It keeps displaying a usual error related to the dimensions but I have tried to extend the dimensions of rho_1. I am not sure if the error is mainly due to the CDF function. Any suggestions for solving this problem? Thanks
rho_1 = [2*10^-4];
beta=4;
Cua = pi*gamma(1+2/beta)*gamma(1-2/beta);
A = (4*pi-36*sqrt(3)+64)/(12*pi-9*sqrt(3));
p2=10^(15/10);
p1=10^(15/10);
T_1 = 10^(2/10);
T_2 = 10^(2/10);
B_one = 1/2*rho_1*Cua*((T_2)^(2/beta))*(A^2)*(((p2/p1)^(2/beta))+ 1);
Ry_low = 0:10:50; A=(4*pi-36*sqrt(3)+64)/(12*pi-9*sqrt(3));
Ry_high = 50;
D_one= 1/2*rho_1*Cua*((T_2)^(2/beta)) * (A^2) *(((p1/p2)^(2/beta))+ 1) ;
C_rov = ((pi* rho_1)/(2* sqrt(B_one*D_one)*(Ry_high - Ry_low).^2))*((normcdf(sqrt(2*B_one)*Ry_high) - (normcdf(sqrt(2*B_one)*Ry_low))) *((normcdf(sqrt(2*D_one)*Ry_high) - (normcdf(sqrt(2*D_one)*Ry_low)))));
plot(Ry_low,C_rov)
Use dot multiplication/division. Also corrected D_1 to D_one. Please replace your line 13 with this:
C_rov = ((pi* rho_1)./(2* sqrt(B_one*D_one).*(Ry_high - Ry_low).^2)).*((normcdf(sqrt(2*B_one)*Ry_high) - (normcdf(sqrt(2*B_one)*Ry_low))).*((normcdf(sqrt(2*D_one)*Ry_high) - (normcdf(sqrt(2*D_one)*Ry_low)))));

How can I sum up functions that are made of elements of the imported dataset?

See the code and error. I have already tried Do, For,...and it is not working.
CODE + Error from Mathematica:
Import of survival probabilities _{k}p_x and _{k}p_y (calculated in excel)
px = Import["C:\Users\Eva\Desktop\kpx.xlsx"];
px = Flatten[Take[px, All], 1];
NOTE: The probability _{k}p_x can be found on the position px[[k+2, x -16]
i = 0.04;
v = 1/(1 + i);
JointLifeIndep[x_, y_, n_] = Sum[v^k*px[[k + 2, x - 16]]*py[[k + 2, y - 16]], {k , 0, n - 1}]
Part::pkspec1: The expression 2+k cannot be used as a part specification.
Part::pkspec1: The expression 2+k cannot be used as a part specification.
Part::pkspec1: The expression 2+k cannot be used as a part specification.
General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.
Part of dataset (left corner of the dataset):
k\x 18 19 20
0 1 1 1
1 0.999478086278185 0.999363078716059 0.99927911905056
2 0.998841497412202 0.998642656911039 0.99858030519133
3 0.998121451605207 0.99794428814123 0.99788275311401
4 0.997423447323642 0.997247180349674 0.997174407432264
5 0.996726703362208 0.996539285828369 0.996437857252448
6 0.996019178300768 0.995803204773039 0.99563600297737
7 0.995283481416241 0.995001861216016 0.994823584922968
8 0.994482556091416 0.994189960607964 0.99405569519175
9 0.993671079225432 0.99342255996206 0.993339856748282
10 0.992904079096455 0.992707177451333 0.992611817294026
11 0.992189069953677 0.9919796017009 0.991832027835091
Without having the exact same data files to work with it is often easy for each of us to make mistakes that the other cannot reproduce or understand.
From your snapshot of your data set I used Export in Mathematica to try to reproduce your .xlsx file. Then I tried the following
px = Import["kpx.xlsx"];
px = Flatten[Take[px, All], 1];
py = px; (* fake some py data *)
i = 0.04;
v = 1/(1 + i);
JointLifeIndep[x_, y_, n_] := Sum[v^k*px[[k+2,x-16]]*py[[k+2,y-16]], {k,0,n-1}];
JointLifeIndep[17, 17, 12]
and it displays 362.402
Notice I used := instead of = in my definition of JointLifeIndep. := and = do different things in Mathematica. = will immediately evaluate the right hand side of that definition. This is possibly the reason that you are getting the error that you do.
You should also be careful with your subscript values and make sure that every subscript is between 1 and the number of rows (or columns) in your matrix.
So see if you can try this example with an Excel sheet containing only the snapshot of data that you showed and see if you get the same result that I do.
Hopefully that will be enough for you to make progress.

Fit with the parameter

I am quite new to Matlab and I am trying to use this code I found online.
I am trying to fit a graph described by the HydrodynamicSpectrum. But instead of having it fit after inputting fvA and fmA, I am trying to obtain the fitted parameters for this value also.
I have tried removing them, changing them. But none is working. I was wondering if any one here will be able to point me into the right direction of fixing this.
specFunc = #(f, para)HydrodynamicSpectrum(f, [para fvA fmA]);
[fit.AXfc, fit.AXD] = NonLinearFit(fit.f(indXY), fit.AXSpec(indXY), specFunc, [iguess_AXfc iguess_AXD]);
[fit.AYfc, fit.AYD] = NonLinearFit(fit.f(indXY), fit.AYSpec(indXY), specFunc, [iguess_AYfc iguess_AYD]);
[fit.ASumfc, fit.ASumD] = NonLinearFit(fit.f(indSum), fit.ASumSpec(indSum), specFunc, [iguess_ASumfc iguess_ASumD]);
predictedAX = HydrodynamicSpectrum(fit.f, [fit.AXfc fit.AXD fvA fmA]);
predictedAY = HydrodynamicSpectrum(fit.f, [fit.AYfc fit.AYD fvA fmA]);
predictedASum = HydrodynamicSpectrum(fit.f, [fit.ASumfc fit.ASumD fvA fmA]);
function spec = HydrodynamicSpectrum(f, para);
fc = para(1);
D = para(2);
fv = para(3);
fm = para(4);
f = abs(f); %Kludge!
spec = D/pi^2*(1+sqrt(f/fv))./((fc - f.*sqrt(f./fv) - (f.^2)/fm).^2 + (f + f.*sqrt(f./fv)).^2);
function [fc, D, sfc, sD] = NonLinearFit(f, spec, specFunc, init);
func = #(para, f)spec./specFunc(f, para);
[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);
fc = paraFit(1);
D = paraFit(2);
ci = nlparci(real(paraFit), real(resid), real(J)); % Kludge!!
sfc = (ci(1,2) - ci(1,1))/4;
sD = (ci(2,2) - ci(2,1))/4;
[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);
It looks like you get your fitted parameter using this line. And you are further processing them to get other stuff out from the function. You can modify your second function to get them out as well.
As there are very few comments, and questions seems to be application specific, there is not much help I can give with what you have presented.

Extract numbers from specific image

I am involved in a project that I think you can help me. I have multiple images that you can see here Images to recognize. The goal here is to extract the numbers between the dashed lines. What is the best approach to do that? The idea that I have from the beginning is to find the coordinates of the dash lines and do the crop function, then is just run OCR software. But is not easy to find those coordinates, can you help me? Or if you have a better approach tell me.
Best regards,
Pedro Pimenta
You may start by looking at more obvious (bigger) objects in your images. The dashed lines are way too small in some images. Searching for the "euros milhoes" logo and the barcode will be easier and it will help you have an idea of the scale and rotation involved.
To find these objects without using match template you can binarize your image (watch out for the background texture) and use the Hu moments on the contours/blobs.
Don't expect a good OCR accuracy on images where the numbers are smaller than 8-10 pixels.
You can use python-tesseract https://code.google.com/p/python-tesseract/ ,it works with your image.What you need to do is to split the result string.I use your https://www.dropbox.com/sh/kcybs1i04w3ao97/u33YGH_Kv6#f:euro9.jpg to test.And source code is below.UPDATE
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageEnhance
import tesseract
im = Image.open('test.jpg')
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(4)
im = im.convert('1')
w, h = im.size
im = im.resize((w * (416 / h), 416))
pix = im.load()
LINE_CR = 0.01
WHITE_HEIGHT_CR = int(h * (20 / 416.0))
status = 0
white_line = []
for i in xrange(h):
line = []
for j in xrange(w):
line.append(pix[(j, i)])
p = line.count(0) / float(w)
if not p > LINE_CR:
white_line.append(i)
wp = None
for i in range(10, len(white_line) - WHITE_HEIGHT_CR):
k = white_line[i]
if white_line[i + WHITE_HEIGHT_CR] == k + WHITE_HEIGHT_CR:
wp = k
break
result = []
flag = 0
while 1:
if wp < 0:
result.append(wp)
break
line = []
for i in xrange(w):
line.append(pix[(i, wp)])
p = line.count(0) / float(w)
if flag == 0 and p > LINE_CR:
l = []
for xx in xrange(20):
l.append(pix[(xx, wp)])
if l.count(0) > 5:
break
l = []
for xx in xrange(416-1, 416-100-1, -1):
l.append(pix[(xx, wp)])
if l.count(0) > 17:
break
result.append(wp)
wp -= 1
flag = 1
continue
if flag == 1 and p < LINE_CR:
result.append(wp)
wp -= 1
flag = 0
continue
wp -= 1
result.reverse()
for i in range(1, len(result)):
if result[i] - result[i - 1] < 15:
result[i - 1] = -1
result = filter(lambda x: x >= 0, result)
im = im.crop((0, result[0], w, result[-1]))
im.save('test_converted.jpg')
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)
mImgFile = "test_converted.jpg"
mBuffer=open(mImgFile,"rb").read()
result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
print "result(ProcessPagesBuffer)=",result
Depends python 2.7 python-tesseract-win32 python-opencv numpy PIL,and be sure to follow python-tesseract's remember to .