本文整理汇总了Python中sklearn.decomposition.MiniBatchDictionaryLearning类的典型用法代码示例。如果您正苦于以下问题:Python MiniBatchDictionaryLearning类的具体用法?Python MiniBatchDictionaryLearning怎么用?Python MiniBatchDictionaryLearning使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MiniBatchDictionaryLearning类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: dictionay_learning_MHOF_online
def dictionay_learning_MHOF_online(training_samples_num=400):
from MHOF_Extraction import MHOF_Extraction
from MHOF_histogram_block import MHOF_histogram_block
from sklearn.decomposition import MiniBatchDictionaryLearning
import numpy as np
import cv2
import video
cam=video.create_capture('Crowd-Activity-All.avi')
height_block_num=4
width_block_num=5
bin_num=16
ret,prev=cam.read()
ret,img=cam.read()
flow_H=MHOF_Extraction(prev,img)
flow_hist_H=MHOF_histogram_block(flow_H,height_block_num,width_block_num,bin_num)
flow_hist_H=np.reshape(flow_hist_H,[1,flow_hist_H.size])
# error!!!!
dico=MiniBatchDictionaryLearning(1,alpha=1,n_iter=500)
dic=dico.fit(flow_hist_H).components_
for i in range(training_samples_num):
ret,img=cam.read()
flow_H=MHOF_Extraction(prev,img)
flow_hist_H=MHOF_histogram_block(flow_H,height_block_num,width_block_num,bin_num)
dico=MiniBatchDictionaryLearing(i+1,alpha=1,n_iter=500,dict_init=dic)
dic=dico.fit(flow_hist_H).components
return dic
开发者ID:burness,项目名称:MHOF,代码行数:26,代码来源:dictionary_learning_MHOF_online.py
示例2: test_dict_learning_online_positivity
def test_dict_learning_online_positivity(transform_algorithm,
positive_code,
positive_dict):
rng = np.random.RandomState(0)
n_components = 8
dico = MiniBatchDictionaryLearning(
n_components, transform_algorithm=transform_algorithm, random_state=0,
positive_code=positive_code, positive_dict=positive_dict).fit(X)
code = dico.transform(X)
if positive_dict:
assert_true((dico.components_ >= 0).all())
else:
assert_true((dico.components_ < 0).any())
if positive_code:
assert_true((code >= 0).all())
else:
assert_true((code < 0).any())
code, dictionary = dict_learning_online(X, n_components=n_components,
alpha=1, random_state=rng,
positive_dict=positive_dict,
positive_code=positive_code)
if positive_dict:
assert_true((dictionary >= 0).all())
else:
assert_true((dictionary < 0).any())
if positive_code:
assert_true((code >= 0).all())
else:
assert_true((code < 0).any())
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:31,代码来源:test_dict_learning.py
示例3: main
def main(games_path = None):
if games_path == None:
games_path = 'specmine/data/go_games/2010-01.pickle.gz'
with specmine.util.openz(games_path) as games_file:
games = pickle.load(games_file)
boards = None # numpy array nx9x9
for game in games:
if boards == None:
boards = games[game].grids
else:
boards = numpy.vstack((boards,games[game].grids))
print 'boards shape: ', boards.shape
boards = boards.reshape((boards.shape[0],-1))
print 'boards reshaped: ', boards.shape
print 'Learning the dictionary... '
t0 = time()
dico = MiniBatchDictionaryLearning(n_atoms=100, alpha=1, n_iter=500)
V = dico.fit(boards).components_
dt = time() - t0
print 'done in %.2fs.' % dt
#pl.figure(figsize=(4.2, 4))
for i, comp in enumerate(V[:100]):
pl.subplot(10, 10, i + 1)
pl.imshow(comp, cmap=pl.cm.gray_r) # interpolation='nearest')
pl.xticks(())
pl.yticks(())
开发者ID:bsilverthorn,项目名称:spectral-mining,代码行数:34,代码来源:sparse_coding.py
示例4: scskl_dico_learning
def scskl_dico_learning(list_pickled_array,n_atoms,maxepoch=5,maxiter=100):
D = None
for e in range(maxepoch):
for a in list_pickled_array:
data = joblib.load(a)
dico = MiniBatchDictionaryLearning(n_components=n_atoms, n_iter=maxiter, dict_init=D)
D = dico.fit(data).components_.astype(np.float32)
return D
开发者ID:rousseau,项目名称:fbrain,代码行数:8,代码来源:sparsity.py
示例5: sklearn_check
def sklearn_check(img, patch_size, dic_size, T=1000):
patch_shape = (patch_size, patch_size)
patches = extract_patches_2d(img, patch_shape)
patches = patches.reshape(patches.shape[0], -1)
patches = center(patches)
dl = MiniBatchDictionaryLearning(dic_size, n_iter=T)
dl.fit(patches)
D = dl.components_.T
return D
开发者ID:Mandrathax,项目名称:sparse_coding,代码行数:9,代码来源:optim.py
示例6: to_sparse
def to_sparse(X,dim):
sparse_dict = MiniBatchDictionaryLearning(dim)
sparse_dict.fit(X)
sparse_vectors = sparse_encode(X, sparse_dict.components_)
for i in sparse_vectors:
print i
return sparse_vectors
开发者ID:tarekmehrez,项目名称:extended-word2vec,代码行数:10,代码来源:sparse_and_test.py
示例7: create_dictionaries
def create_dictionaries(n_codewords=20):
dataset_features = np.load('MSR_Features_hog-hof-skel1360423760.27.dat')
hogs = []
hofs = []
skels = []
for n in dataset_features.keys():
hogs += dataset_features[n]['hog']
hofs += dataset_features[n]['hof']
skels += [normalize_skeleton(dataset_features[n]['skel_world'])]
''' Input should be features[n_samples, n_features] '''
hogs = np.vstack(hogs)
hofs = np.vstack(hofs)
skels = np.vstack(skels)
hog_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
hog_dict.fit(hogs)
hof_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
hof_dict.fit(hofs)
skels_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
skels_dict.fit(skels)
feature_dictionaries = {'hog':hog_dict, 'hof':hof_dict, 'skel':skels_dict}
with open('MSR_Dictionaries_hog-hof-skel_%f.dat'%time.time(), 'wb') as outfile:
pickle.dump(feature_dictionaries, outfile, protocol=pickle.HIGHEST_PROTOCOL)
开发者ID:MerDane,项目名称:pyKinectTools,代码行数:26,代码来源:main_MSR-Dataset.py
示例8: BOW_sparsecoding
class BOW_sparsecoding(BOW):
def codebook(self):
self.mbdl = MiniBatchDictionaryLearning(self.N_codebook)
self.mbdl.fit(self.raw_features)
def bow_feature_extract(self, path):
des = self.raw_feature_extract(path)
out = sum(sparse_encode(des, self.mbdl.components_))
out = np.array([out])
return out
开发者ID:andreydung,项目名称:bagofwords,代码行数:12,代码来源:bow.py
示例9: buildmodel2
def buildmodel2():
"生成有眼镜-无眼镜pair模型"
modelrec = np.load('cut_rec.npy')
modelglass = np.load('glassline.npy')[:modelrec.shape[0]]
linkedmodel = np.empty((modelrec.shape[0],modelrec.shape[1]+modelglass.shape[1]),'f')
linkedmodel[:,:modelrec.shape[1]]=modelrec
linkedmodel[:,modelrec.shape[1]:]=modelglass
#Train
from sklearn.decomposition import MiniBatchDictionaryLearning
learning = MiniBatchDictionaryLearning(500,verbose=True)
learning.fit(linkedmodel)
import cPickle
cPickle.dump(learning,file('sparselinked','wb'),-1)
开发者ID:wzc11,项目名称:glasses-removal,代码行数:15,代码来源:model2.py
示例10: learning_sparse_coding
def learning_sparse_coding(X, components=None):
"""
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.DictionaryLearning.html
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.sparse_encode.html
"""
if components is None:
print('Learning the dictionary...')
t0 = time()
diclearner = MiniBatchDictionaryLearning(n_components=100, verbose=True)
components = diclearner.fit(X).components_
np.savetxt('components_of_convfeat.txt', components)
dt = time() - t0
print('done in %.2fs.' % dt)
codes = sparse_encode(X, components)
np.savetxt('sparse_codes_of_convfeat.txt', codes)
开发者ID:HunjaeJung,项目名称:imagenet2014-modified,代码行数:16,代码来源:model_sparse.py
示例11: extract_codes
def extract_codes(self, X, standardize=False):
self.standardize=standardize
self._extract_data_patches(X)
self.dico = MiniBatchDictionaryLearning(n_components=self.n_components, alpha=1, n_iter=500)
print "Dictionary learning from data..."
self.D = self.dico.fit(self.data)
return self
开发者ID:BinRoot,项目名称:BregmanToolkit,代码行数:7,代码来源:sparseapprox.py
示例12: fit
def fit(self, X, y=None):
# compute the codes
print 'Extracting patchs...'
patchs = []
num = self.patch_num // X.size
for x in X:
img = imread(str(x[0]))
tmp = extract_patches_2d(img, (self.patch_size,self.patch_size), \
max_patches=num, random_state=np.random.RandomState())
patchs.append(tmp)
data = np.vstack(patchs)
data = data.reshape(data.shape[0], -1)
data -= np.mean(data, axis=0)
data = data/np.std(data, axis=0)
print 'Learning codebook...'
if self.method == 'sc':
self.dico = MiniBatchDictionaryLearning(n_components=self.codebook_size, \
alpha=1, n_iter=100, batch_size =100, verbose=True)
self.dico.fit(data)
elif self.method=='km':
# self.dico = MiniBatchKMeans(n_clusters=self.codebook_size)
pass
return self
开发者ID:AI42,项目名称:CNN-detection-tracking,代码行数:26,代码来源:BOVW.py
示例13: test_dict_learning_online_partial_fit
def test_dict_learning_online_partial_fit():
# this test was not actually passing before!
raise SkipTest
n_components = 12
rng = np.random.RandomState(0)
V = rng.randn(n_components, n_features) # random init
V /= np.sum(V ** 2, axis=1)[:, np.newaxis]
dico1 = MiniBatchDictionaryLearning(n_components, n_iter=10, batch_size=1,
shuffle=False, dict_init=V,
random_state=0).fit(X)
dico2 = MiniBatchDictionaryLearning(n_components, n_iter=1, dict_init=V,
random_state=0)
for ii, sample in enumerate(X):
dico2.partial_fit(sample, iter_offset=ii * dico2.n_iter)
# if ii == 1: break
assert_true(not np.all(sparse_encode(X, dico1.components_, alpha=100) ==
0))
assert_array_equal(dico1.components_, dico2.components_)
开发者ID:2011200799,项目名称:scikit-learn,代码行数:18,代码来源:test_dict_learning.py
示例14: train_sparse_coding
def train_sparse_coding(feature_list, patch_list, dict_size=256, transform_alpha=0.5, n_iter=50):
"""
使用mini batch训练稀疏编码
#feature_list 表示要训练的特征的列表
#patch_list 表示结果patch的列表
:return sc_list
"""
sc_list = []
i = 0
for feature, patch in zip(feature_list, patch_list):
i = i + 1
'''
由于组合数值大小比例的问题,稀疏编码可能忽略较小的特征,下面的×10需要用别的特征归一化方法代替
相关性越大,则每个向量都是有用的,所以需要更长的时间进行训练。
'''
dico = None
X = np.concatenate((feature, patch), axis=1)
if len(X) > 100000:
np.random.shuffle(X)
X = X[:90000]
if len(X) < 5000:
print "进入DictionaryLearning状态"
dico = MiniBatchDictionaryLearning(batch_size=1000, transform_algorithm='lasso_lars', fit_algorithm='lars',
transform_n_nonzero_coefs=5, n_components=len(X)/50,
dict_init=X[:len(X)/50],
n_iter=n_iter, transform_alpha=transform_alpha, verbose=10, n_jobs=-1)
else:
print "进入MiniBatchDictionaryLearning状态"
dico = MiniBatchDictionaryLearning(batch_size=1000, transform_algorithm='lasso_lars', fit_algorithm='lars',
transform_n_nonzero_coefs=5, n_components=len(X)/50,
dict_init=X[:len(X)/50],
n_iter=n_iter, transform_alpha=transform_alpha, verbose=10, n_jobs=-1)
V = dico.fit(X).components_
sc_list.append(V)
file_name = "./tmp_file/_tmp_sc_list_new_clsd_raw_%d.pickle" % (i)
sc_file = open(file_name, 'wb')
cPickle.dump(sc_list, sc_file, 1)
sc_file.close()
return sc_list
开发者ID:liangz0707,项目名称:mySuperResolution,代码行数:44,代码来源:training_dict.py
示例15: test_dict_learning_online_partial_fit
def test_dict_learning_online_partial_fit():
n_components = 12
rng = np.random.RandomState(0)
V = rng.randn(n_components, n_features) # random init
V /= np.sum(V ** 2, axis=1)[:, np.newaxis]
dict1 = MiniBatchDictionaryLearning(n_components, n_iter=10 * len(X),
batch_size=1,
alpha=1, shuffle=False, dict_init=V,
random_state=0).fit(X)
dict2 = MiniBatchDictionaryLearning(n_components, alpha=1,
n_iter=1, dict_init=V,
random_state=0)
for i in range(10):
for sample in X:
dict2.partial_fit(sample[np.newaxis, :])
assert not np.all(sparse_encode(X, dict1.components_, alpha=1) == 0)
assert_array_almost_equal(dict1.components_, dict2.components_,
decimal=2)
开发者ID:hmshan,项目名称:scikit-learn,代码行数:19,代码来源:test_dict_learning.py
示例16: extract_codes
def extract_codes(self, X, n_components=16, zscore=True, log_amplitude=True, **mbl_args):
"""Given a spectrogram, learn a dictionary of 2D patch atoms from spectrogram data
inputs:
X - spectrogram data (frequency x time)
n_components - how many components to extract [16]
zscore - whether to zscore the ensemble of 2D patches [True]
log_amplitude - whether to apply log(1+X) scaling of spectrogram data [True]
**mbl_args - keyword arguments for MiniBatchDictionaryLearning.fit(...) [None]
outputs:
self.data - 2D patches of input spectrogram
self.D.components_ - dictionary of learned 2D atoms for sparse coding
"""
self._extract_data_patches(X, zscore, log_amplitude)
self.n_components = n_components
self.dico = MiniBatchDictionaryLearning(n_components=self.n_components, **mbl_args)
print "Dictionary learning from data..."
self.D = self.dico.fit(self.data)
开发者ID:getupyang,项目名称:AudioSpectrumPatchApproximation,代码行数:17,代码来源:AudioSpectrumPatchApproximation.py
示例17: __init__
def __init__(self, hierarchy, depth, patch_size, num_features, num_patches, multiplier):
"""
* depth - hierarchy level (1, 2, 3, etc.)
* patch_size - number of pixels representing side of the square patch.
like, 8 (8x8 patches)
* num_features - how many components to learn
* multiplier - num of subpatches we break patch into
(0 for the first level). if 3, patch will contant 3x3 subpatches.
"""
self.hierarchy = hierarchy
self.depth = depth
self.basement_size = patch_size
self.num_features = num_features
self.num_patches = num_patches
self.multiplier = multiplier
self.learning = MiniBatchDictionaryLearning(
n_components=num_features, n_iter=3000, transform_algorithm='lasso_lars', transform_alpha=0.5, n_jobs=2)
self.ready = False
开发者ID:alh92064,项目名称:deep_hierarchy,代码行数:18,代码来源:hierarchy.py
示例18: test_dict_learning_online_verbosity
def test_dict_learning_online_verbosity():
n_components = 5
# test verbosity
from cStringIO import StringIO
import sys
old_stdout = sys.stdout
sys.stdout = StringIO()
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=1)
dico.fit(X)
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=2)
dico.fit(X)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=1)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=2)
sys.stdout = old_stdout
assert_true(dico.components_.shape == (n_components, n_features))
开发者ID:Jetafull,项目名称:scikit-learn,代码行数:15,代码来源:test_dict_learning.py
示例19: test_dict_learning_online_verbosity
def test_dict_learning_online_verbosity():
n_components = 5
# test verbosity
from sklearn.externals.six.moves import cStringIO as StringIO
import sys
old_stdout = sys.stdout
sys.stdout = StringIO()
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=1,
random_state=0)
dico.fit(X)
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=2,
random_state=0)
dico.fit(X)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=1,
random_state=0)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=2,
random_state=0)
sys.stdout = old_stdout
assert_true(dico.components_.shape == (n_components, n_features))
开发者ID:2011200799,项目名称:scikit-learn,代码行数:19,代码来源:test_dict_learning.py
示例20: test_dict_learning_online_verbosity
def test_dict_learning_online_verbosity():
n_components = 5
# test verbosity
from io import StringIO
import sys
old_stdout = sys.stdout
try:
sys.stdout = StringIO()
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=1,
random_state=0)
dico.fit(X)
dico = MiniBatchDictionaryLearning(n_components, n_iter=20, verbose=2,
random_state=0)
dico.fit(X)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=1,
random_state=0)
dict_learning_online(X, n_components=n_components, alpha=1, verbose=2,
random_state=0)
finally:
sys.stdout = old_stdout
assert dico.components_.shape == (n_components, n_features)
开发者ID:hmshan,项目名称:scikit-learn,代码行数:23,代码来源:test_dict_learning.py
注:本文中的sklearn.decomposition.MiniBatchDictionaryLearning类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论