I'm trying to determine for the articial neuron shown below the values (0 or 1)
for the inputs i1, i2, and i3 for which it will fire (i0 is the input for the
bias weight and will always be -1).
The weights are
W0 = 1.5
W1 = -1
W2 = 1, and W3 = 2.
Assume the activation function depicted in image below.
Please clarify your answer as I have done few examples and still I'm not able to fully understand the theory :(
Many thanks,
Mary J.
PS. Image below:
You basically have the following equation for the neuron output, where i1, i2, and i3 can each have the value 0 or 1:
2*i3 + i2 - i1 - 1.5 >= 0
First, let's look at the highest positively-weighted value. If i3 is 0, the most you can get for the left side is -0.5, so i3 has to be 1 to get a non-zero output. The equation then becomes:
i2 - i1 + 0.5 >= 0
Now look at the negatively-weighted value. If i1 is 0, the output will always be greater than zero no matter what i2 is. If i1 is 1, i2 has to be 1 as well for there to be a non-zero output.
You therefore have these combinations which create a non-zero output:
i1 i2 i3
0 0 1
0 1 1
1 1 1
It seems to be like it's just a matter of summing in * Wn and determining what combinations of i1,2,3 give a positive result. There are only 8 permutations, so just run through it by hand.
To solve this problem in a more general way, first look at what are the variables and what are the fixed parameters.
Basically you are given the input weights vector w= [1.5, -1, 1, 2] and the transfer function g(x) = (sign(x)+1)/2, and you want to find the input vector in so that: g(w*in') = +1 (as product of a row and a column vector), therefore:
g( sum_over_i( w_i*in_i ) ) = 1 # product of vectors
g( w0*in0 + w1*in1 + w2*in2 + w3*in3 ) = 1 # roll out the sum
g( -1.5 - in1 + in2 + 2*in3 ) = 1 # replace the values of w and in
0.5*(sign(-1.5 - in1 + in2 + 2*in3)+1) = 1 # definition of g(x)
sign(-1.5 - in1 + in2 + 2*in3) = 1 # simplify
-1.5 - in1 + in2 + 2*in3 >= 0 # by def: [sign(x)=1 iff x>=0]
Normally you would solve this equation by computing derivatives, but since the inputs in can only take the values 0 or 1, we can simply enumerate all cases (there are 2^n or 8 cases):
in1 in2 in3 -1.5-in1+in2+2*in3
-----------------------------------
0 0 0 -1.5
0 0 1 0.5 *
0 1 0 -0.5
0 1 1 1.5 *
1 0 0 -2.5
1 0 1 -0.5
1 1 0 -1.5
1 1 1 0.5 *
Hence we get get the values of in for which the above expression is positive.
I have cursory knowledge of AI, but as far as I know:
The sum of inputs ( Sigma i_n*W_n) has to be greater than the firing threshold. the second picture tells you the firing threshold, the first picture tells you the inputs and their weights.
You just have to figure out which sums (do not forget the bias) will result in desired threshold/intensity
Related
My program allows to multiply a given B matrix with a z factor with some characteristics listed below, to give an H matrix. I would like to have a programming idea to do the inverse of what I programmed. That is to say with a given H matrix find the value of the B matrix.
For example with a matrix B = [-1 -1 ; 1 0]
I get with my code a matrix :
H = [ 0 0 0 0 0 0 ;
0 0 0 0 0 0 ;
0 0 0 0 0 0 ;
0 1 0 1 0 0 ;
0 0 1 0 1 0 ;
1 0 0 0 0 1 ]
I would like to have from a code H the value of the matrix B.
To specify the matrix H from the matrix B, it is necessary that:
Each coefficient -1 is replaced by a null matrix of dimension z*z;
Each coefficient 0 is replaced by an identity matrix of dimension z*z;
Each coefficient 1,2,...,z-1 is replaced by a circulating permutation matrix of dimension z*z shifted by 1,2,...,z-1 position to the right.
From a matrix B and the expansion factors z , we construct an extended binary H matrix with n-k rows and n columns.
My code :
clear;
close all;
B = [-1 -1 ; 1 0];
z = 3;
H = zeros(size(B)*z);
Y = eye(z);
for X1 = 1:size(B,1)
for X2 = 1:size(B,2)
X3 = B(X1,X2);
if (X3 > -1)
X4 = circshift(Y,[0 X3]);
else
X4 = zeros(z);
end
Y1 = (X1-1)*z+1:X1*z;
Y2 = (X2-1)*z+1:X2*z;
H(Y1,Y2) = X4;
end
end
[M,N] = size(H);
Any suggestions?
Assuming that all of the input matrices are well-formed, you can determine the mapping based on the first row of each block. For example, the block mapping to 1:
0 1 0
0 0 1
1 0 0
has a 1 in column 2 of row 1. Similarly, a one in column 1 maps to 0, and column 3 maps to 2. No ones in the row maps to -1. So we just need to find the column containing the 1 in the first row.
Annoyingly, find returns null when it doesn't find a nonzero value rather than 0 (which is what we would want in this case). We can adjust to this by adding a value to the matrix row that is only 1 when all of the others are 0.
If you have the Image Processing Toolbox, you can use blockproc to handle the looping for you:
B = blockproc(H, [z z], #(A)find([~any(A.data(1,:)) A.data(1,:)])-2);
Otherwise, just loop over the blocks and apply the function to each one.
The expression for completeness relation in quantum mechanics is -
Σ |ψ_n><ψ_n| = 1
where the expression for density matrix in statistical mechanics is -
ρ = Σ p_n |ψ_n><ψ_n|
Both of the equation looks the same. So what are the differences between the density matrix and completeness relation?
What is the basic difference between them?
Formally the difference is that for the density matrix there are pre-factors p_n which sum up to 1 rather than all being 1 as in the completeness relation.
The meaning is also quite different.
Here is a rough illustration what they mean:
This object is a projection operator:
|ψ_n><ψ_n|
It projects on the n-th basis vector.
For simplicity lets take a simple example. Say our Hilbert space is 3 dimensional. Then the sum runs from 1 to 3. Each so-called pure state can be represented by a vector of length 1 in a 3 dimensional space like these examples:
|ψ_1> = (1, 0, 0)T
|ψ_2> = (0, 1, 0)T
|ψ_3> = (0, 0, 1)T
|φ> := (0, 1/2^0.5, 1/2^0.5)T
(The "T" stands for transposed)
These projection operators can be written as a matrix like for example:
/ 0 0 0 \
|ψ_2><ψ_2| = | 0 1 0 |
\ 0 0 0 /
Now what these projection operators do is projecting a vector on one of the coordinate axes. E.g. for n=2 we project to the y-axis.
|ψ_2><ψ_2|φ> = (0, 1/2^0.5, 0)
Now what the completeness relation says is that the sum of those 3 vectors you get when projection on each coordinate axis is once again the original vector (see Basis Decomposition).
As this is true for any vector, this means the operation is the identity matrix:
/ 1 0 0 \ + / 0 0 0 \ + / 0 0 0 \ / 1 0 0 \
|ψ_1><ψ_1| + |ψ_2><ψ_2| + |ψ_3><ψ_3| = | 0 0 0 | + | 0 1 0 | + | 0 0 0 | = | 0 1 0 | = 1
\ 0 0 0 / + \ 0 0 0 / + \ 0 0 1 / \ 0 0 1 /
Now the density matrix is a completely different matter. The weights p_n describe how one state is a mixture of several "pure" states. See e.g. https://en.wikipedia.org/wiki/Density_matrix
I know that this is apparently a simple question. But I can't get a better approach to get better efficiency. Here's what I'm trying. It is very naive but I still can't get it correct.
Sort the array. (Divide and Conquer)
a) Select one element at a time
b) loop through all the remaining elements of the array (in a pair) to get
the difference between them to match the selected element.
Repeat step 2 till at least all the elements are found.
Store all the elements that match the condition.
Print the stored elements.
Condition A[i] - A[j] = A[k] is equal to A[i] = A[j] + A[k], so we can look for sum.
Sort the array.
For every element search if it is sum of two others using two pointers approach (increment lower index when sum is too small, decrement upper index when sum is too big)
Resulting complexity is quadratic
Just out of interest, we can solve this problem in O(n log n + m log m) time, where m is the range, using a Fast Fourier Transform.
First sort the input. Now consider that each of the attainable distances between numbers can be achieved by subtracting one difference-prefix-sum from another. For example:
input: 1 3 7
diff-prefix-sums: 2 6
difference between 7 and 3 is 6 - 2
Now let's add the total (the rightmost prefix sum) to each side of the equation:
ps[r] - ps[l] = D
ps[r] + (T - ps[l]) = D + T
Let's list the differences:
1 3 7
2 4
and the prefix sums:
p => 0 2 6
T - p => 6 4 0 // 6-0, 6-2, 6-6
We need to efficiently determine the counts of all the different achievable differences. This is akin to multiplying the polynomial with coefficients [1, 0, 0, 0, 1, 0, 1] by the polynomial with coefficients, [1, 0, 1, 0, 0, 0, 0] (we don't need the zero coefficient in the second set since it only generates degrees less than or equal to T), which we can accomplish in m log m time, where m is the degree, with a Fast Fourier Transform.
The resultant coefficients would be:
1 0 0 0 1 0 1
*
1 0 1 0 0 0 0
=>
x^6 + x^2 + 1
*
x^6 + x^4
= x^12 + x^10 + x^8 + 2x^6 + x^4
=> 1 0 1 0 1 0 1 0 1 0 0 0 0
We discard counts of degrees lower than or equal to T, and display our ordered results:
1 * 12 = 1 * (T + 6) => 1 diffs of 6
1 * 10 = 1 * (T + 4) => 1 diffs of 4
1 * 8 = 1 * (T + 2) => 1 diffs of 2
If any of the coefficients, their negatives, or T are in our set of array elements, we have a match.
consider an area with size m*n. Here the size of m and n is unknown. Now I am extracting data from each point in the area. I am scanning the area first going in the x direction till m point and the again returning to m=0 and n=1, i.e the second row. Again I scan along the x direction till the end of m. An example of the data has been shown below. Here I get value for different x,y coordinates during the scan. I can carry out operation between the first two points in x direction by
p1 = A{1}; %%reading the data from the text file
p2 = A{2};
LA=[p1 p2];
for m=1:length(y)
p= LA(m,1);
t= LA(m,2);
%%and
q=LA(m+1,1)
r=LA(m+1,2)
I want to do the same for y axis. That is I want to operate between first point in x=0 and y=1 then between x=2 and y=1 and so on. Hope you have got it.
g x y
2 0 0
3 1 0
2 2 0
4 3 0
1 4 0
2 m 0
3 0 1
2 1 1
4 2 1
5 3 1
.
.
.
.
2 m 1
now I was thinking of a logic where I will first find the size of n by counting the number of zeros
NUMX = 0;
while y((NUMX+1),:) == 0
NUMX = NUMX + 1;
end
NU= NUMX;
And then I was thinking of applying the following loop
for m=1:NU:n-1
%%and
p= LA(m,1);
t= LA(m,2);
%%and
q=LA(m+1,1)
r=LA(m+1,2)
But its showing error. Please help!!
??? Attempted to access del2(99794,:); index out of bounds because
size(del2)=[99793,1].
Here NUMX=198
Comment: The nomenclature in your question is inconsistent, making it difficult to understand what you are doing. The variable del2 you mention in the error message is nowhere to be seen.
1.) Let's start off by creating a minimal working example that illustrates the data structure and provides knowledge of the dimensions we want to retrieve later. You matrix is not m x n but m*n x 3.
The following example will set up a matrix with data similar to what you have shown in your question:
M = zeros(8,3);
for J=1:4
for I=1:2
M((J-1)*2+I,1) = rand(1);
M((J-1)*2+I,2) = I;
M((J-1)*2+I,3) = J-1;
end
end
M =
0.469 1 0
0.012 2 0
0.337 1 1
0.162 2 1
0.794 1 2
0.311 2 2
0.529 1 3
0.166 2 3
2.) Next, let's determine the number of x and y, to use the nomenclature of your question:
NUMX = 0;
while M(NUMX+1,3) == 0
NUMX = NUMX + 1;
end
NUMY = size(M,1)/NUMX;
NUMX =
2
NUMY =
4
3.) The data processing you want to do still is unclear, but here are two approaches that can be used for different means:
(a)
COUNT = 1;
for K=1:NUMX:size(M,1)
A(COUNT,1) = M(K,1);
COUNT = COUNT + 1;
end
In this case, you step through the first column of M with a step-size corresponding to NUMX. This will result in all the values for x=1:
A =
0.469
0.337
0.794
0.529
(b) You can also use NUMX and NUMY to reorder M:
for J=1:NUMY
for I=1:NUMX
NEW_M(I,J) = M((J-1)*NUMX+I,1);
end
end
NEW_M =
0.469 0.337 0.794 0.529
0.012 0.162 0.311 0.166
The matrix NEW_M now is of size m x n, with the values of constant y in the columns and the values of constant x in the rows.
Concluding remark: It is unclear how you define m and n in your code, so your specific error message cannot be resolved here.
If I have sequence 1 0 0 0 1 0 1 0 1 1 1
how to effectively locate zero which has from both sides 1.
In this sequence it means zero on position 6 and 8. The ones in bold.
1 0 0 0 1 0 1 0 1 1 1
I can imagine algorithm that would loop through the array and look one in back and one in front I guess that means O(n) so probably there is not any more smooth one.
If you can find another way, I am interested.
Use strfind:
pos = strfind(X(:)', [1 0 1]) + 1
Note that this will work only when X is a vector.
Example
X = [1 0 0 0 1 0 1 0 1 1 1 ];
pos = strfind(X(:)', [1 0 1]) + 1
The result:
pos =
6 8
The strfind method that #EitanT suggested is quite nice. Another way to do this is to use find and element-wise bit operations:
% let A be a logical ROW array
B = ~A & [A(2:end),false] & [false,A(1:end-1)];
elements = find(B);
This assumes, based on your example, that you want to exclude boundary elements. The concatenations [A(2:end),false] and [false,A(1:end-1)] are required to keep the array length the same. If memory is a concern, these can be eliminated:
% NB: this will work for both ROW and COLUMN vectors
B = ~A(2:end-1) & A(3:end) & A(1:end-2);
elements = 1 + find(B); % need the 1+ because we cut off the first element above
...and to elaborate on #Eitan T 's answer, you can use strfind for an array if you loop by row
% let x = some matrix of 1's and 0's (any size)
[m n] = size(x);
for r = 1:m;
pos(r,:) = strfind(x(r,:)',[1 0 1]) + 1;
end
pos would be a m x ? matrix with m rows and any returned positions. If there were no zeros in the proper positions though, you might get a NaN ... or an error. Didn't get a chance to test.