-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathregularization.py
More file actions
43 lines (30 loc) · 1.25 KB
/
regularization.py
File metadata and controls
43 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 3 09:32:58 2019
@author: DELL
"""
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from Preprocessing.SimpleProcessor import SimplePreprocessor
from Dataset.SimpleDatasetLoader import SimpleDatasetLoader
from imutils import paths
imagePaths = list(paths.list_images("./Dataset/train"))
print("[INFO]Load images")
processor = SimplePreprocessor(32, 32)
loader = SimpleDatasetLoader(processors=[processor])
data, label = loader.load(imagePaths, verbose=500)
data = data.reshape(data.shape[0], 3*32*32)
#encode the label cat,dog -> 0,1
le = LabelEncoder()
label = le.fit_transform(label)
#split train,test set
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.25, random_state=42)
for r in [None, 'l1', 'l2']:
print('[INFO] Train model with {} penalty'.format(r))
#model with softmax loss function and 10 epoches
model = SGDClassifier(loss='log', penalty=r, max_iter=10, learning_rate='constant', eta0=0.01, random_state=42)
model.fit(X_train, y_train)
#evaluate
acc = model.score(X_test, y_test)
print('[INFO] Penalty {} accuracy {:.1f}'.format(r, acc*100))