本文整理汇总了Python中shogun.Kernel.CombinedKernel.get_subkernel_weights方法的典型用法代码示例。如果您正苦于以下问题:Python CombinedKernel.get_subkernel_weights方法的具体用法?Python CombinedKernel.get_subkernel_weights怎么用?Python CombinedKernel.get_subkernel_weights使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shogun.Kernel.CombinedKernel
的用法示例。
在下文中一共展示了CombinedKernel.get_subkernel_weights方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: statistics_linear_time_mmd_kernel_choice
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
def statistics_linear_time_mmd_kernel_choice():
from shogun.Features import RealFeatures, CombinedFeatures
from shogun.Kernel import GaussianKernel, CombinedKernel
from shogun.Statistics import LinearTimeMMD
from shogun.Statistics import BOOTSTRAP, MMD1_GAUSSIAN
# note that the linear time statistic is designed for much larger datasets
n=50000
dim=5
difference=2
# data is standard normal distributed. only one dimension of Y has a mean
# shift of difference
(X,Y)=gen_data.create_mean_data(n,dim,difference)
# concatenate since MMD class takes data as one feature object
# (it is possible to give two, but then data is copied)
Z=concatenate((X,Y), axis=1)
print "dimension means of X", [mean(x) for x in X]
print "dimension means of Y", [mean(x) for x in Y]
# create kernels/features to choose from
# here: just a bunch of Gaussian Kernels with different widths
# real sigmas are 2^-5, ..., 2^10
sigmas=array([pow(2,x) for x in range(-5,10)])
# shogun has a different parametrization of the Gaussian kernel
shogun_sigmas=array([x*x*2 for x in sigmas])
# We will use multiple kernels
kernel=CombinedKernel()
# two separate feature objects here, could also be one with appended data
features=CombinedFeatures()
# all kernels work on same features
for i in range(len(sigmas)):
kernel.append_kernel(GaussianKernel(10, shogun_sigmas[i]))
features.append_feature_obj(RealFeatures(Z))
mmd=LinearTimeMMD(kernel,features, n)
print "start learning kernel weights"
mmd.set_opt_regularization_eps(10E-5)
mmd.set_opt_low_cut(10E-5)
mmd.set_opt_max_iterations(1000)
mmd.set_opt_epsilon(10E-7)
mmd.optimize_kernel_weights()
weights=kernel.get_subkernel_weights()
print "learned weights:", weights
#pyplot.plot(array(range(len(sigmas))), weights)
#pyplot.show()
print "index of max weight", weights.argmax()
示例2: statistics_linear_time_mmd_kernel_choice
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
def statistics_linear_time_mmd_kernel_choice():
from shogun.Features import RealFeatures, CombinedFeatures
from shogun.Features import DataGenerator
from shogun.Kernel import GaussianKernel, CombinedKernel
from shogun.Statistics import LinearTimeMMD
from shogun.Statistics import BOOTSTRAP, MMD1_GAUSSIAN
# note that the linear time statistic is designed for much larger datasets
n=50000
dim=5
difference=2
# use data generator class to produce example data
# in pratice, this generate data function could be replaced by a method
# that obtains data from a stream
data=DataGenerator.generate_mean_data(n,dim,difference)
print "dimension means of X", mean(data.T[0:n].T)
print "dimension means of Y", mean(data.T[n:2*n+1].T)
# create kernels/features to choose from
# here: just a bunch of Gaussian Kernels with different widths
# real sigmas are 2^-5, ..., 2^10
sigmas=array([pow(2,x) for x in range(-5,10)])
# shogun has a different parametrization of the Gaussian kernel
shogun_sigmas=array([x*x*2 for x in sigmas])
# We will use multiple kernels
kernel=CombinedKernel()
# two separate feature objects here, could also be one with appended data
features=CombinedFeatures()
# all kernels work on same features
for i in range(len(sigmas)):
kernel.append_kernel(GaussianKernel(10, shogun_sigmas[i]))
features.append_feature_obj(RealFeatures(data))
mmd=LinearTimeMMD(kernel,features, n)
print "start learning kernel weights"
mmd.set_opt_regularization_eps(10E-5)
mmd.set_opt_low_cut(10E-5)
mmd.set_opt_max_iterations(1000)
mmd.set_opt_epsilon(10E-7)
mmd.optimize_kernel_weights()
weights=kernel.get_subkernel_weights()
print "learned weights:", weights
#pyplot.plot(array(range(len(sigmas))), weights)
#pyplot.show()
print "index of max weight", weights.argmax()
示例3: StratifiedCrossValidationSplitting
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
c2=ModelSelectionParameters("C2");
c2.build_values(-4.0, 4.0, R_EXP);
param_tree_root.append_child(c2)
splitting_strategy = StratifiedCrossValidationSplitting(labels, 50)
evaluation_criterium = ContingencyTableEvaluation(ACCURACY)
cross_validation = CrossValidation(classifier, feats_train, labels, splitting_strategy, evaluation_criterium)
model_selection = GridSearchModelSelection(param_tree_root, cross_validation)
best_parameters = model_selection.select_model(True)
print "Best parameters: ",
best_parameters.print_tree()
best_parameters.apply_to_machine(classifier)
classifier.train()
w=kernel.get_subkernel_weights()
kernel.set_subkernel_weights(w)
# Plot ROC curve
subplot(111)
ROC_evaluation=ROCEvaluation()
ROC_evaluation.evaluate(classifier.apply(feats_train),Labels(trainlab))
roc = ROC_evaluation.get_ROC()
plot(roc[0], roc[1])
fill_between(roc[0],roc[1],0,alpha=0.1)
grid(True)
xlabel('FPR')
ylabel('TPR')
title('Train ROC (Width=%.3f, C1=%.3f, C2=%.3f) ROC curve = %.3f' % (10, classifier.get_C1(), classifier.get_C2(), ROC_evaluation.get_auROC()),size=10)
savefig("data/iri/mkl.png")
"""
示例4: _train
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
#.........这里部分代码省略.........
# for task_id_1 in data.get_task_ids():
# for task_id_2 in data.get_task_ids():
# hack_block_vec.push_back(Pairii(task_id_1, task_id_2))
#
# hack_block_vec.push_back(Pairii(data.name_to_id("B_2705"), data.name_to_id("B_4001")))
# other_group = ["B_0702", "B_1501", "B_5801"]
# for task_id_1 in other_group:
# for task_id_2 in other_group:
# hack_block_vec.push_back(Pairii(data.name_to_id(task_id_1), data.name_to_id(task_id_2)))
#
#
#
# # create mask-based normalizer
# normalizer_hack = MultitaskKernelMaskPairNormalizer(data.task_vector_nums, hack_block_vec)
# kernel_hack = shogun_factory.create_empty_kernel(param)
# kernel_hack.set_normalizer(normalizer_hack)
#
# # append current kernel to CombinedKernel
# combined_kernel.append_kernel(kernel_hack)
#
# # append features
# combined_features.append_feature_obj(base_features)
##################################################
# init combined kernel
combined_kernel.init(combined_features, combined_features)
#combined_kernel.precompute_subkernels()
self.additional_information["mkl weights before"] = combined_kernel.get_subkernel_weights()
print "subkernel weights:", combined_kernel.get_subkernel_weights()
svm = None
print "using MKL:", (param.flags["mkl_q"] >= 1.0)
if param.flags["mkl_q"] >= 1.0:
svm = MKLClassification()
svm.set_mkl_norm(param.flags["mkl_q"])
svm.set_kernel(combined_kernel)
svm.set_labels(lab)
else:
# create SVM (disable unsupported optimizations)
combined_kernel.set_cache_size(500)
svm = SVMLight(param.cost, combined_kernel, lab)
num_threads = 8
svm.io.enable_progress()
svm.io.set_loglevel(shogun.Classifier.MSG_INFO)
svm.parallel.set_num_threads(num_threads)
svm.set_linadd_enabled(False)
svm.set_batch_computation_enabled(False)
svm.set_epsilon(0.03)
示例5: _train
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
def _train(self, train_data, param):
"""
training procedure using training examples and labels
@param train_data: Data relevant to SVM training
@type train_data: dict<str, list<instances> >
@param param: Parameters for the training procedure
@type param: ParameterSvm
"""
# merge data sets
data = PreparedMultitaskData(train_data, shuffle=False)
# create shogun data objects
base_wdk = shogun_factory.create_kernel(data.examples, param)
kernel_matrix = base_wdk.get_kernel_matrix()
lab = shogun_factory.create_labels(data.labels)
# fetch taxonomy from parameter object
taxonomy = param.taxonomy.data
# create name to leaf map
nodes = taxonomy.get_all_nodes()
########################################################
print "creating a kernel for each node:"
########################################################
# assemble combined kernel
from shogun.Kernel import CombinedKernel, CustomKernel
combined_kernel = CombinedKernel()
# indicator to which task each example belongs
task_vector = data.task_vector_names
for node in nodes:
print "creating kernel for ", node.name
# fetch sub-tree
leaf_names = [leaf.name for leaf in node.get_leaves()]
print "masking all entries other than:", leaf_names
# init matrix
kernel_matrix_node = numpy.zeros(kernel_matrix.shape)
# fill matrix for node
for (i, task_lhs) in enumerate(task_vector):
for (j, task_rhs) in enumerate(task_vector):
# only copy values, if both tasks are present in subtree
if task_lhs in leaf_names and task_rhs in leaf_names:
kernel_matrix_node[i,j] = kernel_matrix[i,j]
# create custom kernel
kernel_node = CustomKernel()
kernel_node.set_full_kernel_matrix_from_full(kernel_matrix_node)
# append custom kernel to CombinedKernel
combined_kernel.append_kernel(kernel_node)
print "------"
print "subkernel weights:", combined_kernel.get_subkernel_weights()
svm = None
print "using MKL:", (param.transform >= 1.0)
if param.transform >= 1.0:
num_threads = 4
svm = MKLClassification()
svm.set_mkl_norm(param.transform)
svm.set_solver_type(ST_GLPK) #DIRECT) #NEWTON)#ST_CPLEX)
svm.set_C(param.cost, param.cost)
svm.set_kernel(combined_kernel)
svm.set_labels(lab)
svm.parallel.set_num_threads(num_threads)
#svm.set_linadd_enabled(False)
#svm.set_batch_computation_enabled(False)
svm.train()
#.........这里部分代码省略.........
示例6: _train
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
#.........这里部分代码省略.........
print "creating empty kernel"
kernel_pos = shogun_factory.create_empty_kernel(param)
print "setting normalizer"
kernel_pos.set_normalizer(normalizer)
print "appending kernel"
# append current kernel to CombinedKernel
combined_kernel.append_kernel(kernel_pos)
print "appending features"
# append features
combined_features.append_feature_obj(base_features)
print "done constructing combined kernel"
##################################################
# init combined kernel
# init weights
# combined_kernel.set_subkernel_weights([1.0/2.85]*combined_kernel.get_num_subkernels())
combined_kernel.init(combined_features, combined_features)
print "subkernel weights:", combined_kernel.get_subkernel_weights()
svm = None
print "using MKL:", (param.transform >= 1.0)
if param.transform >= 1.0:
svm = MKLClassification()
svm.set_mkl_norm(param.transform)
#svm.set_solver_type(ST_CPLEX) #ST_GLPK) #DIRECT) #NEWTON)#ST_CPLEX) #auto
svm.set_C(param.cost, param.cost)
svm.set_kernel(combined_kernel)
svm.set_labels(lab)
else:
# create SVM (disable unsupported optimizations)
combined_kernel.set_cache_size(500)
svm = SVMLight(param.cost, combined_kernel, lab)
# set up SVM
num_threads = 8
svm.io.enable_progress()
#svm.io.set_loglevel(shogun.Classifier.MSG_INFO)
svm.io.set_loglevel(shogun.Classifier.MSG_DEBUG)
示例7: _train
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
#.........这里部分代码省略.........
if similarity == 1:
tmp_pair = Pairii(data.name_to_id(task_name_lhs), data.name_to_id(task_name_rhs))
pos_block_vec.push_back(tmp_pair)
print "creating normalizer"
normalizer_pos = MultitaskKernelMaskPairNormalizer(data.task_vector_nums, pos_block_vec)
print "creating empty kernel"
kernel_pos = shogun_factory.create_empty_kernel(param)
print "setting normalizer"
kernel_pos.set_normalizer(normalizer_pos)
print "appending kernel"
# append current kernel to CombinedKernel
combined_kernel.append_kernel(kernel_pos)
print "appending features"
# append features
combined_features.append_feature_obj(base_features)
print "done constructing combined kernel"
##################################################
# init combined kernel
combined_kernel.init(combined_features, combined_features)
print "subkernel weights:", combined_kernel.get_subkernel_weights()
svm = None
print "using MKL:", (param.transform >= 1.0)
if param.transform >= 1.0:
svm = MKLClassification()
svm.set_mkl_norm(param.transform)
#svm.set_solver_type(ST_CPLEX) #ST_GLPK) #DIRECT) #NEWTON)#ST_CPLEX) #auto
svm.set_C(param.cost, param.cost)
svm.set_kernel(combined_kernel)
svm.set_labels(lab)
else:
# create SVM (disable unsupported optimizations)
combined_kernel.set_cache_size(500)
svm = SVMLight(param.cost, combined_kernel, lab)
# set up SVM
num_threads = 8
svm.io.enable_progress()
#svm.io.set_loglevel(shogun.Classifier.MSG_INFO)
svm.io.set_loglevel(shogun.Classifier.MSG_DEBUG)
示例8: _train
# 需要导入模块: from shogun.Kernel import CombinedKernel [as 别名]
# 或者: from shogun.Kernel.CombinedKernel import get_subkernel_weights [as 别名]
def _train(self, train_data, param):
"""
training procedure using training examples and labels
@param train_data: Data relevant to SVM training
@type train_data: dict<str, list<instances> >
@param param: Parameters for the training procedure
@type param: ParameterSvm
"""
#numpy.random.seed(1337)
numpy.random.seed(666)
# merge data sets
data = PreparedMultitaskData(train_data, shuffle=True)
# create shogun label
lab = shogun_factory.create_labels(data.labels)
# assemble combined kernel
combined_kernel = CombinedKernel()
combined_kernel.io.set_loglevel(shogun.Kernel.MSG_DEBUG)
# set kernel cache
if param.flags.has_key("cache_size"):
combined_kernel.set_cache_size(param.flags["cache_size"])
# create features
base_features = shogun_factory.create_features(data.examples)
combined_features = CombinedFeatures()
########################################################
print "creating a masked kernel for each node:"
########################################################
# fetch taxonomy from parameter object
taxonomy = param.taxonomy.data
# create name to leaf map
nodes = taxonomy.get_all_nodes()
for node in nodes:
print "creating kernel for ", node.name
# fetch sub-tree
active_task_ids = [data.name_to_id(leaf.name) for leaf in node.get_leaves()]
print "masking all entries other than:", active_task_ids
# create mask-based normalizer
normalizer = MultitaskKernelMaskNormalizer(data.task_vector_nums, data.task_vector_nums, active_task_ids)
# normalize trace
if param.flags.has_key("normalize_trace") and param.flags["normalize_trace"]:
norm_factor = len(node.get_leaves()) / len(active_task_ids)
normalizer.set_normalization_constant(norm_factor)
# create kernel
kernel = shogun_factory.create_empty_kernel(param)
kernel.set_normalizer(normalizer)
# append current kernel to CombinedKernel
combined_kernel.append_kernel(kernel)
# append features
combined_features.append_feature_obj(base_features)
print "------"
combined_kernel.init(combined_features, combined_features)
#combined_kernel.precompute_subkernels()
print "subkernel weights:", combined_kernel.get_subkernel_weights()
svm = None
print "using MKL:", (param.flags["mkl_q"] >= 1.0)
if param.flags["mkl_q"] >= 1.0:
# set up MKL
svm = MKLClassification()
# set the "q" in q-norm MKL
svm.set_mkl_norm(param.flags["mkl_q"])
# set interleaved optimization
#.........这里部分代码省略.........