资源简介
多任务学习简单例子,包含多任务学习的数据创建,训练,作图的matlab代码
代码片段和文件信息
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“““
Created on Mon Jan 30 19:06:43 2017
@author: corbi
“““
import scipy.io
import numpy as np
from sklearn.base import baseEstimator
from sklearn.metrics import mean_squared_error
from scipy.optimize import fmin_l_bfgs_b
class AlternatingStructureOptimization(baseEstimator):
def __init__(self lbda m d h n_iter=5):
self.m=m
self.d=d
self.h=h
self.n_iter=n_iter
self.lbda = lbda
self.params={“h“: np.arange(3int(d/3))}
self.U = np.zeros((self.d self.m))
self.U0 = np.ones((self.d self.m))
self.V = np.zeros((self.h self.m))
self.W = np.zeros((self.d self.m))
self.theta = np.ones((self.h self.d))
def fit(self X y):
for it in range(self.n_iter):
if it%10==0:
print (“Iteration %d...“ %(it+1))
for l in range(1self.m):
idx=np.where(X[:self.d]==l)[0]
X_l = X[idx:self.d]
y_l = np.ravel(y[idx:1])
self.V[:l] = np.dot(self.thetaself.W)[:l]
model = optim_ASO( X=X_l y=y_l theta=self.theta v=self.V[:l]
lbda=self.lbda[:l])
self.U[:l] = l_bfgs_b(self.U0[:l] model n_iter=self.n_iter)
self.W[:l] = self.U[:l] + np.dot(self.theta.Tself.V[:l])
V1 D V2 = scipy.linalg.svd(np.sqrt(self.lbda)*self.W)
self.theta = V1.T[np.arange(self.h):]
self.V = np.dot(self.thetaself.W)
def predict(self X):
y_pred = np.zeros((X.shape[0]2))
for l in range(1self.m):
idx=np.where(X[:self.d]==l)[0]
X_l = X[idx:self.d]
y_pred[idx0]=np.dot(self.U[:l] + np.dot(self.theta.Tself.V)[:l]X_l.T)
y_pred[idx1]=l
return y_pred
def score(self X y):
y_pred = self.predict(X)
return 1.- np.sqrt(mean_squared_error(y[:0] y_pred[:0]))/(np.max(y[:0])-np.min(y[:0]))
def l_bfgs_b(x_init model n_iter=500 bounds=None callback=None **kwargs):
“““
l-BFGS-b algorithm
“““
x _ _ = fmin_l_bfgs_b(model.loss x_init model.grad bounds=bounds pgtol=1e-20 callback=callback)
return x
class optim_ASO():
def __init__(self X y theta v lbda):
# model param
self.X = X
self.y = y
self.theta = theta
self.v = v
self.n = X.shape[0]
self.lbda=lbda
def loss(self u):
““““
loss of the optim problem
“““
f = np.dot(u.T+np.dot(self.v.Tself.theta) self.X.T)
return (1./self.n)*np.sum((f-self.y)**2)+self.lbda*np.linalg.norm(u)**2
def grad(self u):
“““
gradient of the optim problem
““
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 228 2017-09-03 10:42 MultiTaskLearning\.idea\inspectionProfiles\profiles_settings.xm
文件 213 2017-09-03 10:42 MultiTaskLearning\.idea\misc.xm
文件 286 2017-09-03 10:42 MultiTaskLearning\.idea\modules.xm
文件 459 2017-09-03 10:43 MultiTaskLearning\.idea\MultiTaskLearning.iml
文件 32432 2017-09-03 19:53 MultiTaskLearning\.idea\workspace.xm
文件 3146 2017-09-03 15:49 MultiTaskLearning\AlternatingStructureOptimization.py
文件 499183 2017-09-03 10:32 MultiTaskLearning\cherkaoui_corbiere_multitasks_learning_final_report.pdf
文件 8431 2017-09-03 10:28 MultiTaskLearning\ClusteredRegression.py
文件 4224 2017-09-03 10:30 MultiTaskLearning\computeScores.py
文件 4622 2017-09-03 16:40 MultiTaskLearning\ConvexAlternatingStructureOptimization.py
文件 9964616 2017-09-03 10:21 MultiTaskLearning\data\sarcos_inv.mat
文件 996776 2017-09-03 10:22 MultiTaskLearning\data\sarcos_inv_test.mat
文件 44759 2017-09-03 14:54 MultiTaskLearning\data\school.mat
文件 4364 2017-09-03 15:15 MultiTaskLearning\loadData.py
文件 1296 2017-09-03 10:31 MultiTaskLearning\mult_ind_SVM.py
文件 4080 2017-09-03 14:16 MultiTaskLearning\plotResults.py
文件 1030 2017-09-03 10:30 MultiTaskLearning\RandomMTLRegressor.py
文件 1114 2017-09-03 10:33 MultiTaskLearning\README.md
目录 0 2017-09-03 10:42 MultiTaskLearning\.idea\inspectionProfiles
目录 0 2017-09-03 19:53 MultiTaskLearning\.idea
目录 0 2017-09-03 10:24 MultiTaskLearning\data
目录 0 2017-09-03 16:40 MultiTaskLearning
----------- --------- ---------- ----- ----
11571259 22
评论
共有 条评论