Reputation: 133
How can I generate my model after training? I didn't use sklearn package for my fit and predict. My code looks like this:
class SVM(object):
def __init__(self, kernel=polynomial_kernel, C=None):
self.kernel = kernel
self.C = C
if self.C is not None: self.C = float(self.C)
def fit(self, X, y):
n_samples, n_features = X.shape
# Gram matrix
K = np.zeros((n_samples, n_samples))
for i in range(n_samples):
for j in range(n_samples):
K[i,j] = self.kernel(X[i], X[j])
P = cvxopt.matrix(np.outer(y,y) * K)
q = cvxopt.matrix(np.ones(n_samples) * -1)
A = cvxopt.matrix(y, (1,n_samples))
b = cvxopt.matrix(0.0)
if self.C is None:
G = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))
h = cvxopt.matrix(np.zeros(n_samples))
else:
tmp1 = np.diag(np.ones(n_samples) * -1)
tmp2 = np.identity(n_samples)
G = cvxopt.matrix(np.vstack((tmp1, tmp2)))
tmp1 = np.zeros(n_samples)
tmp2 = np.ones(n_samples) * self.C
h = cvxopt.matrix(np.hstack((tmp1, tmp2)))
# solve QP problem
solution = cvxopt.solvers.qp(P, q, G, h, A, b)
# Lagrange multipliers
a = np.ravel(solution['x'])
# Support vectors have non zero lagrange multipliers
sv = a > 1e-5
ind = np.arange(len(a))[sv]
self.a = a[sv]
self.sv = X[sv]
self.sv_y = y[sv]
print("%d support vectors out of %d points" % (len(self.a), n_samples))
# Intercept
self.b = 0
for n in range(len(self.a)):
self.b += self.sv_y[n]
self.b -= np.sum(self.a * self.sv_y * K[ind[n],sv])
self.b /= len(self.a)
# Weight vector
if self.kernel == linear_kernel:
self.w = np.zeros(n_features)
for n in range(len(self.a)):
self.w += self.a[n] * self.sv_y[n] * self.sv[n]
else:
self.w = None
def project(self, X):
if self.w is not None:
return np.dot(X, self.w) + self.b
else:
y_predict = np.zeros(len(X))
for i in range(len(X)):
s = 0
for a, sv_y, sv in zip(self.a, self.sv_y, self.sv):
s += a * sv_y * self.kernel(X[i], sv)
y_predict[i] = s
return y_predict + self.b
def predict(self, X):
return np.sign(self.project(X))
And I tried to display my model in my Test file:
self.clf = SVM(C=1000.1)
self.svm_model=self.clf.fit(X, Y)
print(self.svm_model)
The output shows:
None
Then I tried the model to save in Pickle:
SVM_pkl_filename=QtGui.QFileDialog.getSaveFileName(self,'Save File')
print ("SVM classifier :: ", self.svm_model)
#SVM_pkl_filename = 'SVM_model.pkl'
SVM_model_pkl = open(SVM_pkl_filename, 'wb')
# Dump the trained SVM classifier with Pickle
pickle.dump(self.svm_model, SVM_model_pkl)
# Close the pickle instances
SVM_model_pkl.close()
And I open the Saved file, it shows nothing. I compared it to my another Saved model file with Sklearn, and it has some random content.
The purpose of my model is to Save and Load it using Pickle. I used Pickle to Train my 4 datasets, which my Model will update every time I train my Datasets. And load the model for Testing.
Upvotes: 0
Views: 357
Reputation: 9081
The problem is in this line - self.svm_model=self.clf.fit(X, Y)
Notice here that you are trying to store the output of the function fit()
to self.svm_model
, but the fit()
function doesn't return anything.
Either you have to assign a return
statement to the fit function, or assign self.svm_model
object in-place in the fit()
function.
Also, I don't have a clear view on what you want the svm_model
to have. Do you want to save the weights or do you want a convenience object which has its own fit()
and predict()
method like scikit-learn
?
Assumin you just want to save the weights, change the fit()
function to return self.w
at the end.
The pickle
not working is just a consequence of this. Once you solve the svm_model
problem the pickling should get fixed by itself.
EDIT
There is another condition in your code -
else:
self.w = None
Another reason why it can return None
. Hard to tell without looking in to execution along with the data.
As for checking before pickling, you can do a simple null check.
if not self.svm_model: # check not None
# pickle here
Upvotes: 1