-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSVR.py
172 lines (131 loc) · 5.37 KB
/
MSVR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Multi-output Support Vector Regression
https://github.com/Analytics-for-Forecasting/msvr
"""
# Copyright (C) 2020 Xinze Zhang, Kaishuai Xu, Siyue Yang, Yukun Bao
# This program is free software: you can redistribute it and/or modify
# it under the terms of the Apache.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
import numpy as np
from sklearn.metrics.pairwise import pairwise_kernels
class MSVR():
def __init__(self, kernel='rbf', degree=3, gamma=None, coef0=0.0, tol=0.001, C=1.0, epsilon=0.1):
super(MSVR, self).__init__()
self.kernel = kernel
self.degree = degree
self.gamma = gamma
self.coef0 = coef0
self.tol = tol
self.C = C
self.epsilon = epsilon
self.Beta = None
self.NSV = None
self.xTrain = None
def fit(self, x, y):
self.xTrain = x.copy()
C = self.C
epsi = self.epsilon
tol = self.tol
n_m = np.shape(x)[0] # num of samples
n_d = np.shape(x)[1] # input data dimensionality
n_k = np.shape(y)[1] # output data dimensionality (output variables)
# H = kernelmatrix(ker, x, x, par)
H = pairwise_kernels(x, x, metric=self.kernel, filter_params=True,
degree=self.degree, gamma=self.gamma, coef0=self.coef0)
self.Beta = np.zeros((n_m, n_k))
#E = prediction error per output (n_m * n_k)
E = y - np.dot(H, self.Beta)
#RSE
u = np.sqrt(np.sum(E**2, 1, keepdims=True))
#RMSE
RMSE = []
RMSE_0 = np.sqrt(np.mean(u**2))
RMSE.append(RMSE_0)
#points for which prediction error is larger than epsilon
i1 = np.where(u > epsi)[0]
#set initial values of alphas a (n_m * 1)
a = 2 * C * (u - epsi) / u
#L (n_m * 1)
L = np.zeros(u.shape)
# we modify only entries for which u > epsi. with the sq slack
L[i1] = u[i1]**2 - 2 * epsi * u[i1] + epsi**2
#Lp is the quantity to minimize (sq norm of parameters + slacks)
Lp = []
BetaH = np.dot(np.dot(self.Beta.T, H), self.Beta)
Lp_0 = np.sum(np.diag(BetaH), 0) / 2 + C * np.sum(L)/2
Lp.append(Lp_0)
eta = 1
k = 1
hacer = 1
val = 1
while(hacer):
Beta_a = self.Beta.copy()
E_a = E.copy()
u_a = u.copy()
i1_a = i1.copy()
M1 = H[i1][:, i1] + \
np.diagflat(1/a[i1]) + 1e-10 * np.eye(len(a[i1]))
#compute betas
# sal1 = np.dot(np.linalg.pinv(M1),y[i1]) #求逆or广义逆(M-P逆)无法保证M1一定是可逆的?
sal1 = np.dot(np.linalg.inv(M1), y[i1])
eta = 1
self.Beta = np.zeros(self.Beta.shape)
self.Beta[i1] = sal1.copy()
#error
E = y - np.dot(H, self.Beta)
#RSE
u = np.sqrt(np.sum(E**2, 1)).reshape(n_m, 1)
i1 = np.where(u >= epsi)[0]
L = np.zeros(u.shape)
L[i1] = u[i1]**2 - 2 * epsi * u[i1] + epsi**2
#%recompute the loss function
BetaH = np.dot(np.dot(self.Beta.T, H), self.Beta)
Lp_k = np.sum(np.diag(BetaH), 0) / 2 + C * np.sum(L)/2
Lp.append(Lp_k)
#Loop where we keep alphas and modify betas
while(Lp[k] > Lp[k-1]):
eta = eta/10
i1 = i1_a.copy()
self.Beta = np.zeros(self.Beta.shape)
#%the new betas are a combination of the current (sal1)
#and of the previous iteration (Beta_a)
self.Beta[i1] = eta*sal1 + (1-eta)*Beta_a[i1]
E = y - np.dot(H, self.Beta)
u = np.sqrt(np.sum(E**2, 1)).reshape(n_m, 1)
i1 = np.where(u >= epsi)[0]
L = np.zeros(u.shape)
L[i1] = u[i1]**2 - 2 * epsi * u[i1] + epsi**2
BetaH = np.dot(np.dot(self.Beta.T, H), self.Beta)
Lp_k = np.sum(np.diag(BetaH), 0) / 2 + C * np.sum(L)/2
Lp[k] = Lp_k
#stopping criterion 1
if(eta < 1e-16):
Lp[k] = Lp[k-1] - 1e-15
self.Beta = Beta_a.copy()
u = u_a.copy()
i1 = i1_a.copy()
hacer = 0
#here we modify the alphas and keep betas
a_a = a.copy()
a = 2 * C * (u - epsi) / u
RMSE_k = np.sqrt(np.mean(u**2))
RMSE.append(RMSE_k)
if((Lp[k-1]-Lp[k])/Lp[k-1] < tol):
hacer = 0
k = k + 1
#stopping criterion #algorithm does not converge. (val = -1)
if(len(i1) == 0):
hacer = 0
self.Beta = np.zeros(self.Beta.shape)
val = -1
self.NSV = len(i1)
def predict(self, x):
H = pairwise_kernels(x, self.xTrain, metric=self.kernel, filter_params=True,
degree=self.degree, gamma=self.gamma, coef0=self.coef0)
yPred = np.dot(H, self.Beta)
return yPred
# def score(self,x):