How to write summation with limits for below Lagrange function
조회 수: 2 (최근 30일)
이전 댓글 표시
Suppose we are given a training dataset
{yᵢ, xᵢ}, for i = 1, ..., n,
X = [8 ,7; 4,10; 9 , 7; 7,10; 9,6; 4 ,8; 10, 10; 2, 7; 8 , 3; 7,5; 4 ,4; 4,6; 1,3; 2,5]; %14x2 matrix
Y=[1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1]; %14x1 matrix
where yᵢ can either be -1 or 1 and xᵢ can be e.g. a 2D or 3D point.
suppose Yi is 14*1 matrix and Xi =(X1,X2) is 14*2 matrix
In general, when the input points are linearly separable, the SVM model can be defined as follows
min 1/2*||w||²
to find w,b
subject to the constraints (for i = 1, ..., n)
yᵢ*(w*xᵢ - b) >= 1
This is often called the hard-margin SVM model, which is thus a constrained minimization problem, where the unknowns are w and b. We have using Lagrange primal and dual method to calculate w and b.
Below is the Lagrange equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/418843/image.png)
I am just trying to apply the math here but i am not sure what i am doing wrong. Please help me with how to implement this correctly?
syms w
x = sym('x', [14 2]) ;
x = subs(x, x, X);
y = sym('y', [14 1]) ;
y = subs(y, y, Y);
F = w*w/2;
V = Y(i)*(w'.*X(i,:)+b)-1>= 0;
L = F - symsum(lambda(i)*V,i,1,m); % Langrange function
I am getting error "Unable to compute sum with respect to '14'. Summation index must be a symbolic variable."
different error for below one:
symsum(y(i,1),i,1,2)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
댓글 수: 0
답변 (1개)
Walter Roberson
2020년 11월 19일
You did not assign to i so it will have its default value sqrt(-1)
The second parameter for symsum must always be a symbolic variable. As you symsum with i as the second parameter you would have needed to use
syms i
You do not tell us what the data type is for Y. Context suggests Y is your 14x1 matrix.
Your symsum has Y(i) but if i is symbolic then you would be indexing an array with a symbolic variable. You can never do that. Arrays must be indexed at concrete numeric or logical values.
You do not show us lambda but it seems unlikely to be a function that accepts symbolic variables. It seems much more likely to be a vector, in which case you have the same issue about indexing with a symbolic variable.
Do not try to index with symbolic variables and symsum that. Instead form the definite sum such a
sum(lambda .* V)
Also
V = Y(i)*(w'.*X(i,:)+b)-1>= 0;
That involves comparing a symbolic expression in w to a value using >= and you are obviously expecting the result to be 0 or 1 to select lambda in the symsum. You can form symbolic inequalities but they do not have value 0 or 1. For example
V = w > x
V*lambda
would evaluate to the symbolic expression
w*lambda > x*lambda
and you would be trying to add those kinds of things, which would be done by adding the sides, so like
w*lambda1 + w*lambda2 > x1*lambda1 + x2*lambda2
This is seldom what you want.
If you want to translate between a symbolic condition and a 0 or 1 value then use piecewise
piecewise(w > x, 1, 0)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!