-
Notifications
You must be signed in to change notification settings - Fork 1
/
demon_curve.py
139 lines (109 loc) · 4.16 KB
/
demon_curve.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
# -*- coding: utf-8 -*-
"""
Created on 2019
@author: Tangmei [email protected]
Hypothesis transfer learning based on fuzzy residual
"""
import numpy as np
import matplotlib
matplotlib.rcParams['backend'] = 'SVG'
import matplotlib.pyplot as plt
import ResTL
from sklearn.model_selection import GridSearchCV
from sklearn.kernel_ridge import KernelRidge
'''
CURVE : which demon curve 1, 2, 3
RULES : visual the rules or not, only available when the basemodel is TSK
nc : the number of rules
C : regularization
width :the width of rules
'''
CURVE = 3
RULES = 1
nc = 6
C = 1e-8
width = 1.5
Title = 'Parameters: '+'nc='+str(nc)+' C='+str(C)+' width='+str(width)
# basemodel should be 'TSK' if you want to visual the basic rules
basemodel = GridSearchCV(KernelRidge(kernel='rbf', gamma=0.1), cv=5,
param_grid={"alpha": [1e0, 0.1, 1e-2, 1e-3],
"gamma": np.logspace(-2, 2, 5)})
'''Demon curve 1
Xs, Ys: source data
Xt, Yt: target data
X_test:test data
'''
if CURVE == 1:
n_source1, n_target_train = 100, 5
Xs_array = np.linspace(-10, 10, n_source1)
Xs = Xs_array.reshape(len(Xs_array),1)
Ys = np.cos(Xs[:,0])*Xs[:,0] + np.random.normal(0, 0.15, Xs.shape[0])
Ys = Ys[:, np.newaxis]
Xt = np.linspace(-10, -2, n_target_train)
Xt = Xt.reshape(len(Xt),1)
Yt = np.cos(Xt[:,0])*Xt[:,0] + Xt[:,0] + np.random.normal(0, 0.15, Xt.shape[0])
Yt = Yt[:, np.newaxis]
X_test = np.linspace(-10, 10, 100)[:, np.newaxis]
'''Demon curve 2'''
if CURVE == 2:
n_source1, n_target_train = 100, 8
Xs_array = np.linspace(-1, 1, n_source1)
Xs = Xs_array.reshape(len(Xs_array),1)
Ys = np.cos(10*Xs[:,0])*Xs[:,0] + np.random.normal(0, 0.0085, Xs.shape[0])
Ys = Ys[:, np.newaxis]
Xt = np.linspace(-0.4, 1, n_target_train)
Xt = Xt.reshape(len(Xt),1)
Yt = np.cos(10*Xt[:,0])*Xt[:,0] + Xt[:,0] + np.random.normal(0, 0.0085, Xt.shape[0]) +1
Yt = Yt[:, np.newaxis]
X_test = np.linspace(-1, 1, 100)[:, np.newaxis]
'''Demon curve 3'''
if CURVE == 3:
Xs = np.linspace(0, 1, 30)
Xs = Xs.reshape(len(Xs),1)
Ys = np.sin(7*Xs) + 1
# Ys = Ys[:, np.newaxis]
Xt = np.linspace(0.5, 0.8, 3)
Xt = Xt.reshape(len(Xt),1)
Yt = Xt*np.sin(7*Xt) + 0.1
# Yt = Yt[:, np.newaxis]
X_test = np.linspace(0, 1, 100)
X_test = X_test.reshape(len(X_test),1)
'''main'''
Y_RD_KRR, x_each, y_each = ResTL.model(Xs, Xt, Ys, Yt, X_test,
n_cluster=nc,
C=C,
width = width,
residual = 'RD' ,
basemodel = basemodel,
fit_with_target=False)
Y_LS_TSK, x_each, y_each = ResTL.model(Xs, Xt, Ys, Yt, X_test,
n_cluster=nc,
C=C,
width = width,
residual = 'LS' ,
basemodel = 'TSK',
fit_with_target=False)
'''fig'''
fig, ax = plt.subplots()
plt.plot(X_test, Y_LS_TSK, 'purple', lw=2, zorder=9, label='ResTL$_{LS}$')
plt.plot(X_test, Y_RD_KRR, 'g', lw=2, zorder=9, label='ResTL$_{RD}$')
'''visual rules'''
if RULES:
for i in range(nc):
plt.plot(x_each[:,i], y_each[:,i], 'p-', lw=2)
plt.plot(Xs, Ys, 'r-', lw=3, label='Source model')
plt.scatter(Xt, Yt, c='b', s=100, label='Target Data')
fontfamily = 'NSimSun'
font = {'family':fontfamily,
'size':12,
'weight':23}
ax.set_xlabel('X',fontproperties = fontfamily, size = 12)
ax.set_ylabel('Y',fontproperties = fontfamily, size = 12)
plt.yticks(fontproperties = fontfamily, size = 12)
plt.xticks(fontproperties = fontfamily, size = 12)
ax.set_title(Title, fontproperties = fontfamily, size = 12)
plt.legend(prop=font)
plt.tight_layout()
plt.ylim([-2,3])
plt.legend(prop=font)
plt.show()