Neural Network Artificial Intelligence - artificial-intelligence

In a simple perception, can someone explain to me the concept of the Threshold and and how to set it, i.e. initially what is the value of the Threshold input and weight??

Since by definition the perceptron is a binary classifier even in it's simplest incarnation you can think of it as having a bias/threshold of 0:
y = f( wn* xn> 0 ? 1 : 0 )
But since 0 is a pretty arbitrary value the bias/threshold is explicitly introduced into the model as a variable:
y = f( wn* xn> b'? 1 : 0 ) or y = f( wn* xn+ b > 0 ? 1 : 0 )
The problem is that now the model another variables (b [which is a scalar] apart from the original wn [which is a vector]) that needs to be taken into account when training.
There are many ways to do that, the naive way being to just pick some of the possible values of b and for each train the model on wn and keep the ( b, wn) pair that produced the best result.
A more elegant way is to just think of the bias/threshold variable b as being a weight that is attached to an input that is always 1 which basically brings the model back to the original form with only 1 variable w except that now the vectors x and w have n + 1 elements:
y = f( wn + 1* xn + 1> 0 ? 1 : 0 )

Related

A matrix and a column vector containing indices, how to iterate with no loop?

I have a big matrix (500212x7) and a column vector like below
matrix F vector P
0001011 4
0001101 3
1101100 6
0000110 1
1110000 7
The vector contains indices considered within the matrix rows. P(1) is meant to point at F(1,4), P(2) at F(2,3) and so on.
I want to negate a bit in each row in F in a column pointed by P element (in the same row).
I thought of things like
F(:,P(1)) = ~F(:,P(1));
F(:,P(:)) = ~F(:,P(:));
but of course these scenarios won't produce the result I expect as the first line won't make P element change and the second one won't even let me start the program because a full vector cannot make an index.
The idea is I need to do this for all F and P rows (changing/incrementing "simultaneously") but take the value of P element.
I know this is easily achieved with for loop but due to large dimensions of the F array such a way to solve the problem is completely unacceptable.
Is there any kind of Matlab wizardry that lets solving such a task with the use of matrix operations?
I know this is easily achieved with for loop but due to large dimensions of the F array such a way to solve the problem is completely unacceptable.
You should never make such an assumption. First implement the loop, then check to see if it really is too slow for you or not, then worry about optimizing.
Here I'm comparing Luis' answer and the trival loop:
N = 500212;
F = rand(N,7) > 0.6;
P = randi(7,N,1);
timeit(#()method1(F,P))
timeit(#()method2(F,P))
function F = method1(F,P)
ind = (1:size(F,1)) + (P(:).'-1)*size(F,1); % create linear index
F(ind) = ~F(ind); % negate those entries
end
function F = method2(F,P)
for ii = 1:numel(P)
F(ii,P(ii)) = ~F(ii,P(ii));
end
end
Timings are 0.0065 s for Luis' answer, and 0.0023 s for the loop (MATLAB Online R2019a).
It is especially true for very large arrays, that loops are faster than vectorized code, if the vectorization requires creating an intermediate array. Memory access is expensive, using more of it makes the code slower.
Lessons: don't dismiss loops, don't prematurely try to optimize, and don't optimize without comparing.
Another solution:
xor( F, 1:7 == P )
Explanation:
1:7 == P generates one-hot arrays.
xor will cause a bit to retain its value against a 0, and flip it against a 1
Not sure if it qualifies as wizardry, but linear indexing does exactly what you want:
F = [0 0 0 1 0 1 1; 0 0 0 1 1 0 1; 1 1 0 1 1 0 0; 0 0 0 0 1 1 0; 1 1 1 0 0 0 0];
P = [4; 3; 6; 1; 7];
ind = (1:size(F,1)) + (P(:).'-1)*size(F,1); % create linear index
F(ind) = ~F(ind); % negate those entries

Inverse matrix with SparseMatrix<..,RowMajor>

I'm looking for a way to retrieve the inverse of a SparseMatrix using RowMajor storage with EIGEN 3.3.4. I found an answer to this question there, but I'm experiencing issues adapting it to my case.
The given way to do it is by using a Solver, and resolve Ax=I (I is identity), to retrieve the inverse of A.
What I do not understand is the following :
auto A_inv = solver.solve(I);
Looks like, in the author case, the result of solver.solve() is a SparseMatrix object. In mine, it is a Solve object and this is why I'm in trouble right now.
I'm using SimplicialLDLT as follow :
SimplicialLDLT<SparseMatrix<short, RowMajor> solver;
solver.compute(A);
SparseMatrix<short,RowMajor> identity(A.rows(), A.cols()); identity.setIdentity();
auto result = solver.solve(identity);
std::cout<<"INVERSE MATRIX : "<<result<<std::endl;
Which does not work since result is a Solve(I guess there's no << overload). What I'm looking for is a way to do :
SparseMatrix<short,RowMajor> matrix_result = result;
For now, the result of this conversion is an error : THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES.
The question is : How do you setup a RowMajor solver, and how do you retrieve the result of this in a usable RowMajor matrix?
Thanks in advance.
With ColMajor
Using ColMajor for both the solver and rhs (lhs is still rowmajor, I can't change it), the result is wrong. The matrix A is :
A = 1 -1 0
0 1 0
0 0 1
The code is :
SparseMatrix<short,RowMajor> A;
//set A values
SparseMatrix<short,ColMajor> I(A.rows(),A.cols());I.setIdentity();
SimplicialLDLT<SparseMatrix<short,ColMajor>> solver;
solver.compute(A);
SparseMatrix<short,ColMajor> result = solver.solve(I);
std::cout<<result<<std::endl;
Displaying the wrong following :
1 0 0
0 1 0
0 0 1
Where the expected result was :
1 1 0
0 1 0
0 0 1
I'm probably doing something wrong there but I can't figure it out.
Solution : Change the solver to SparseLU, SimplicialLDLT is only working for symmetric matrices.

smallest subset of array whose sum is equal to key. Condition : Values can be used any number of times

I was asked this question in interview.
Given a list of 'N' coins, their values being in an array A[], return the minimum number of coins required to sum to 'S' (you can use as many coins you want). If it's not possible to sum to 'S', return -1
Note here i can use same coins multiple times.
Example:
Input #00:
Coin denominations: { 1,3,5 }
Required sum (S): 11
Output #00:
3
Explanation:
The minimum number of coins requires is: 3 - 5 + 5 + 1 = 11;
Is there any better way we can think except Sorting the array and start it by both ends?
This is the change-making problem.
A simple greedy approach, which you seem to be thinking of, won't always produce an optimal result. If you elaborate a bit on what exactly you mean by starting from both ends, I might be able to come up with a counter-example.
It has a dynamic programming approach, taken from here:
Let C[m] be the minimum number of coins of denominations d1,d2,...,dk needed to make change for m amount. In the optimal solution to making change for m amount, there must exist some first coin di, where di < m. Furthermore, the remaining coins in the solution must themselves be the optimal solution to making change for m - di.
Thus, if di is the first coin in the optimal solution to making change for m amount, then C[m] = 1 + C[m - di], i.e. one di coin plus C[m - di] coins to optimally make change for m - di amount. We don't know which coin di is the first coin; however, we may check all n such possibilities (subject to the constraint that di < m), and the value of the optimal solution must correspond to the minimum value of 1 + C[m - di], by definition.
Furthermore, when making change for 0, the value of the optimal solution is clearly 0 coins. We thus have the following recurrence.
C[p] = 0 if p = 0
min(i: di < p) {1 + C[p - di]} if p > 0
Pathfinding algorithms (Dijkstra, A*, meeting on the middle, etc.) could be suitable for this on graph like this:
0
1/|\5
/ |3\
/ | \
1 3 5
1/|\51/| ...
/ |3\/ |3
/ | /\ |
2 4 6
....
Other way is recursive bisection. Say, if we cannot get the sum S with one coin, we start to try to get amounts (S/2, S/2)...(S-1,1) recursively until we find suitable coin or reach S=1.

minimum distance between two arrays using dynamic programming

I have 2 arrays , A and B, not necessarily of the same size. The distance between 2 elements a( element from A ) and b( element from B ) is abs(a-b).
I am getting wrong answer on my test cases . what changes can i do?.
following is my solution :
for simplicity assume : arrays are indexed 1 based and size(A)>size(B)
dp[i][j] = minimum distance taking A[1..i] and B[1..j]
memset(dp,INFINITY)
for(j=1;j<=size(B);j++) // A is empty
dp[0][j] = B[j]
for(j=1;j<=size(A);j++) // B is empty
dp[j][0] = A[j]
dp[0][0] = 0
for(i=1;i<=size(A);i++)
for(j=1;j<=size(B);j++)
dp[i][j] = min( dp[i][j-1] , // we don't take A[i]
dp[i-1][j-1] + abs(A[i]-B[j]) // we take this pair and take difference
)
return dp[size(A)][size(B)]
edit : this is no homework . i just found it on internet and thought to give it a try.

inconsistent results using isreal

Take this simple example:
a = [1 2i];
x = zeros(1,length(a));
for n=1:length(a)
x(n) = isreal(a(n));
end
In an attempt to vectorize the code, I tried:
y = arrayfun(#isreal,a);
But the results are not the same:
x =
1 0
y =
0 0
What am I doing wrong?
This certainly appears to be a bug, but here's a workaround:
>> y = arrayfun(#(x) isreal(x(1)),a)
ans =
1 0
Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling ISREAL it removes the "complex" attribute from the array element if the imaginary component is zero. Try this in the Command Window:
>> a = [1 2i]; %# A complex array
>> b = a(1); %# Indexing element 1 removes the complex attribute...
>> c = complex(a(1)); %# ...but we can put that attribute back
>> whos
Name Size Bytes Class Attributes
a 1x2 32 double complex
b 1x1 8 double %# Not complex
c 1x1 16 double complex %# Still complex
Apparently, ARRAYFUN must internally maintain the "complex" attribute of the array elements it passes to ISREAL, thus treating them all as being complex numbers even if the imaginary component is zero.
It might help to know that MATLAB stores the real/complex parts of a matrix separately. Try the following:
>> format debug
>> a = [1 2i];
>> disp(a)
Structure address = 17bbc5b0
m = 1
n = 2
pr = 1c6f18a0
pi = 1c6f0420
1.0000 0 + 2.0000i
where pr is a pointer to the memory block containing the real part of all values, and pi pointer to the complex part of all values in the matrix. Since all elements are stored together, then in this case they all have a complex part.
Now compare these two approaches:
>> arrayfun(#(x)disp(x),a)
Structure address = 17bbcff8
m = 1
n = 1
pr = 1bb8a8d0
pi = 1bb874d0
1
Structure address = 17c19aa8
m = 1
n = 1
pr = 1c17b5d0
pi = 1c176470
0 + 2.0000i
versus
>> for n=1:2, disp(a(n)), end
Structure address = 17bbc930
m = 1
n = 1
pr = 1bb874d0
pi = 0
1
Structure address = 17bbd180
m = 1
n = 1
pr = 1bb874d0
pi = 1bb88310
0 + 2.0000i
So it seems that when you access a(1) in the for loop, the value returned (in the ans variable) has a zero complex-part (null pi), thus is considered real.
One the other hand, ARRAYFUN seems to be directly accessing the values of the matrix (without returning them in ANS variable), thus it has access to both pr and pi pointers which are not null, thus are all elements are considered non-real.
Please keep in mind this just my interpretation, and I could be mistaken...
Answering really late on this one... The MATLAB function ISREAL operates in a really rather counter-intuitive way for many purposes. It tells you if a given array taken as a whole has no complex part at all - it tells you about the storage, it doesn't really tell you anything about the values in the array. It's a bit like the ISSPARSE function in that regard. So, for example
isreal(complex(1)) % returns FALSE
What you'll find in MATLAB is that certain operations automatically trim any all-zero imaginary parts. So, for example
x = complex(1);
isreal(x); % FALSE, we just forced there to be an imaginary part
isreal(x(1)); % TRUE - indexing realised it could drop the zero imaginary part
isreal(x(:)); % FALSE - "(:)" indexing is just a reshape, not real indexing
In short, MATLAB really needs a function which answers the question "does this value have zero imaginary part", in an elementwise way on an array.

Resources