def trainRBM_SVM(features, Cparam, nComponents):
[X, Y] = listOfFeatures2Matrix(features)
rbm = BernoulliRBM(n_components = nComponents, n_iter = 30, learning_rate = 0.2, verbose = True)
rbm.fit(X,Y)
newX = rbm.transform(X)
# colors = ["r","g","b"]
# for i in range(1,Y.shape[0],5):
# plt.plot(newX[i,:], colors[int(Y[i])])
# plt.show()
classifier = {}
classifier["rbm"] = rbm
svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True)
svm.fit(newX,Y)
classifier["svm"] = svm
return classifier
python类BernoulliRBM()的实例源码
def build_model_rbm():
np.random.seed(12)
rbm_estimators = list()
# rbm = BernoulliRBM(random_state=12, verbose=0, n_components=in_dim)
rbm = BernoulliRBM(random_state=np.random.randint(1, 100), verbose=0)
lr = LogisticRegression()
rbm.learning_rate = 0.0001
# rbm.n_iter = 20
# rbm.n_components = 50
lr.C = 10.0
rbm_estimators.append(('rbm', rbm))
rbm_estimators.append(('lr', lr))
return rbm_estimators
def trainRBM_SVM(features, Cparam, nComponents):
[X, Y] = listOfFeatures2Matrix(features)
rbm = BernoulliRBM(n_components = nComponents, n_iter = 30, learning_rate = 0.2, verbose = True)
rbm.fit(X,Y)
newX = rbm.transform(X)
# colors = ["r","g","b"]
# for i in range(1,Y.shape[0],5):
# plt.plot(newX[i,:], colors[int(Y[i])])
# plt.show()
classifier = {}
classifier["rbm"] = rbm
svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True)
svm.fit(newX,Y)
classifier["svm"] = svm
return classifier
def test_small_sparse_partial_fit():
for sparse in [csc_matrix, csr_matrix]:
X_sparse = sparse(Xdigits[:100])
X = Xdigits[:100].copy()
rbm1 = BernoulliRBM(n_components=64, learning_rate=0.1,
batch_size=10, random_state=9)
rbm2 = BernoulliRBM(n_components=64, learning_rate=0.1,
batch_size=10, random_state=9)
rbm1.partial_fit(X_sparse)
rbm2.partial_fit(X)
assert_almost_equal(rbm1.score_samples(X).mean(),
rbm2.score_samples(X).mean(),
decimal=0)
def test_score_samples():
# Test score_samples (pseudo-likelihood) method.
# Assert that pseudo-likelihood is computed without clipping.
# See Fabian's blog, http://bit.ly/1iYefRk
rng = np.random.RandomState(42)
X = np.vstack([np.zeros(1000), np.ones(1000)])
rbm1 = BernoulliRBM(n_components=10, batch_size=2,
n_iter=10, random_state=rng)
rbm1.fit(X)
assert_true((rbm1.score_samples(X) < -300).all())
# Sparse vs. dense should not affect the output. Also test sparse input
# validation.
rbm1.random_state = 42
d_score = rbm1.score_samples(X)
rbm1.random_state = 42
s_score = rbm1.score_samples(lil_matrix(X))
assert_almost_equal(d_score, s_score)
# Test numerical stability (#2785): would previously generate infinities
# and crash with an exception.
with np.errstate(under='ignore'):
rbm1.score_samples([np.arange(1000) * 100])
def trainRBM_LR(features, paramC, nComponents):
[X, Y] = listOfFeatures2Matrix(features)
logistic = LogisticRegression(C = paramC)
rbm = BernoulliRBM(n_components = nComponents, n_iter = 100, learning_rate = 0.01, verbose = False)
# NORMALIZATION???
classifier = Pipeline([("rbm", rbm), ("logistic", logistic)])
classifier.fit(X, Y)
return classifier
def varius_classifiers():
# List of tuples of a classifier and its parameters.
clf_list = []
clf_linearsvm = LinearSVC()
params_linearsvm = {"C": [0.5, 1, 5, 10, 100, 10**10],"tol":[0.1, 0.0000000001],"class_weight":['balanced']}
clf_list.append( (clf_linearsvm, params_linearsvm) )
clf_tree = DecisionTreeClassifier()
params_tree = { "min_samples_split":[2, 5, 10, 20],"criterion": ('gini', 'entropy')}
clf_list.append( (clf_tree, params_tree) )
clf_random_tree = RandomForestClassifier()
params_random_tree = { "n_estimators":[2, 3, 5],"criterion": ('gini', 'entropy')}
clf_list.append( (clf_random_tree, params_random_tree) )
clf_adaboost = AdaBoostClassifier()
params_adaboost = { "n_estimators":[20, 30, 50, 100]}
clf_list.append( (clf_adaboost, params_adaboost) )
clf_knn = KNeighborsClassifier()
params_knn = {"n_neighbors":[2, 5], "p":[2,3]}
clf_list.append( (clf_knn, params_knn) )
clf_log = LogisticRegression()
params_log = {"C":[0.5, 1, 10, 10**2,10**10, 10**20],"tol":[0.1, 0.00001, 0.0000000001],"class_weight":['balanced']}
clf_list.append( (clf_log, params_log) )
clf_lda = LinearDiscriminantAnalysis()
params_lda = {"n_components":[0, 1, 2, 5, 10]}
clf_list.append( (clf_lda, params_lda) )
logistic = LogisticRegression()
rbm = BernoulliRBM()
clf_rbm = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)])
params_rbm = {"logistic__tol":[0.0000000001, 10**-20],"logistic__C":[0.05, 1, 10, 10**2,10**10, 10**20],"logistic__class_weight":['balanced'],"rbm__n_components":[2,3,4]}
clf_list.append( (clf_rbm, params_rbm) )
return clf_list
def trainRBM_LR(features, paramC, nComponents):
[X, Y] = listOfFeatures2Matrix(features)
logistic = LogisticRegression(C = paramC)
rbm = BernoulliRBM(n_components = nComponents, n_iter = 100, learning_rate = 0.01, verbose = False)
# NORMALIZATION???
classifier = Pipeline([("rbm", rbm), ("logistic", logistic)])
classifier.fit(X, Y)
return classifier
def temp(features):
[featuresNorm, MAX, MIN] = normalizeFeatures(features)
[X, Y] = listOfFeatures2Matrix(featuresNorm)
rbm = BernoulliRBM(n_components = 10, n_iter = 1000, learning_rate = 0.01, verbose = False)
X1 = X[0::2]
X2 = X[1::2]
Y1 = Y[0::2]
Y2 = Y[1::2]
rbm.fit(X1,Y1)
YY = rbm.transform(X1)
for i in range(10):plt.plot(YY[i,:],'r')
for i in range(10):plt.plot(YY[i+10,:],'g')
for i in range(10):plt.plot(YY[i+20,:],'b')
plt.show()
def test_fit():
X = Xdigits.copy()
rbm = BernoulliRBM(n_components=64, learning_rate=0.1,
batch_size=10, n_iter=7, random_state=9)
rbm.fit(X)
assert_almost_equal(rbm.score_samples(X).mean(), -21., decimal=0)
# in-place tricks shouldn't have modified X
assert_array_equal(X, Xdigits)
def test_partial_fit():
X = Xdigits.copy()
rbm = BernoulliRBM(n_components=64, learning_rate=0.1,
batch_size=20, random_state=9)
n_samples = X.shape[0]
n_batches = int(np.ceil(float(n_samples) / rbm.batch_size))
batch_slices = np.array_split(X, n_batches)
for i in range(7):
for batch in batch_slices:
rbm.partial_fit(batch)
assert_almost_equal(rbm.score_samples(X).mean(), -21., decimal=0)
assert_array_equal(X, Xdigits)
def test_transform():
X = Xdigits[:100]
rbm1 = BernoulliRBM(n_components=16, batch_size=5,
n_iter=5, random_state=42)
rbm1.fit(X)
Xt1 = rbm1.transform(X)
Xt2 = rbm1._mean_hiddens(X)
assert_array_equal(Xt1, Xt2)
def test_small_sparse():
# BernoulliRBM should work on small sparse matrices.
X = csr_matrix(Xdigits[:4])
BernoulliRBM().fit(X) # no exception
def test_fit_gibbs():
# Gibbs on the RBM hidden layer should be able to recreate [[0], [1]]
# from the same input
rng = np.random.RandomState(42)
X = np.array([[0.], [1.]])
rbm1 = BernoulliRBM(n_components=2, batch_size=2,
n_iter=42, random_state=rng)
# you need that much iters
rbm1.fit(X)
assert_almost_equal(rbm1.components_,
np.array([[0.02649814], [0.02009084]]), decimal=4)
assert_almost_equal(rbm1.gibbs(X), X)
return rbm1
def test_fit_gibbs_sparse():
# Gibbs on the RBM hidden layer should be able to recreate [[0], [1]] from
# the same input even when the input is sparse, and test against non-sparse
rbm1 = test_fit_gibbs()
rng = np.random.RandomState(42)
from scipy.sparse import csc_matrix
X = csc_matrix([[0.], [1.]])
rbm2 = BernoulliRBM(n_components=2, batch_size=2,
n_iter=42, random_state=rng)
rbm2.fit(X)
assert_almost_equal(rbm2.components_,
np.array([[0.02649814], [0.02009084]]), decimal=4)
assert_almost_equal(rbm2.gibbs(X), X.toarray())
assert_almost_equal(rbm1.components_, rbm2.components_)
def test_gibbs_smoke():
# Check if we don't get NaNs sampling the full digits dataset.
# Also check that sampling again will yield different results.
X = Xdigits
rbm1 = BernoulliRBM(n_components=42, batch_size=40,
n_iter=20, random_state=42)
rbm1.fit(X)
X_sampled = rbm1.gibbs(X)
assert_all_finite(X_sampled)
X_sampled2 = rbm1.gibbs(X)
assert_true(np.all((X_sampled != X_sampled2).max(axis=1)))
def test_rbm_verbose():
rbm = BernoulliRBM(n_iter=2, verbose=10)
old_stdout = sys.stdout
sys.stdout = StringIO()
try:
rbm.fit(Xdigits)
finally:
sys.stdout = old_stdout
def temp(features):
[featuresNorm, MAX, MIN] = normalizeFeatures(features)
[X, Y] = listOfFeatures2Matrix(featuresNorm)
rbm = BernoulliRBM(n_components = 10, n_iter = 1000, learning_rate = 0.01, verbose = False)
X1 = X[0::2]
X2 = X[1::2]
Y1 = Y[0::2]
Y2 = Y[1::2]
rbm.fit(X1,Y1)
YY = rbm.transform(X1)
for i in range(10):plt.plot(YY[i,:],'r')
for i in range(10):plt.plot(YY[i+10,:],'g')
for i in range(10):plt.plot(YY[i+20,:],'b')
plt.show()