22I implement the soft margin of Support Vector Machines with Stochastic Gradient Descent.
33"
44Class {
5- #name : #AISupportVectorMachinesSoftSGD ,
5+ #name : #AISupportVectorMachines ,
66 #superclass : #Object ,
77 #instVars : [
88 ' weights' ,
@@ -14,7 +14,7 @@ Class {
1414}
1515
1616{ #category : #accessing }
17- AISupportVectorMachinesSoftSGD >> calculateCostGradientX: inputMatrix y: outputVector [
17+ AISupportVectorMachines >> calculateCostGradientX: inputMatrix y: outputVector [
1818
1919 | distance dw di numberOfRows output input |
2020 numberOfRows := inputMatrix size.
@@ -40,15 +40,15 @@ AISupportVectorMachinesSoftSGD >> calculateCostGradientX: inputMatrix y: outputV
4040]
4141
4242{ #category : #' as yet unclassified' }
43- AISupportVectorMachinesSoftSGD >> calculateDistancesForX: inputMatrix y: outputVector [
43+ AISupportVectorMachines >> calculateDistancesForX: inputMatrix y: outputVector [
4444
4545 | xDotWeights |
4646 xDotWeights := inputMatrix collect: [ :row | (row * weights) sum ].
4747 ^ 1 - (outputVector * xDotWeights)
4848]
4949
5050{ #category : #accessing }
51- AISupportVectorMachinesSoftSGD >> computeCostX: inputMatrix y: outputVector [
51+ AISupportVectorMachines >> computeCostX: inputMatrix y: outputVector [
5252
5353 | numberOfRows distances hingeLoss cost |
5454 numberOfRows := inputMatrix size.
@@ -63,38 +63,19 @@ AISupportVectorMachinesSoftSGD >> computeCostX: inputMatrix y: outputVector [
6363 ^ cost
6464]
6565
66- { #category : #accessing }
67- AISupportVectorMachinesSoftSGD >> learningRate: aNumber [
68- learningRate := aNumber
69- ]
70-
71- { #category : #accessing }
72- AISupportVectorMachinesSoftSGD >> maxEpochs: aNumber [
73- maxEpochs := aNumber
74- ]
75-
7666{ #category : #' as yet unclassified' }
77- AISupportVectorMachinesSoftSGD >> predict: inputMatrix [
78-
79- ^ self signFunction: inputMatrix
67+ AISupportVectorMachines >> fitX: inputMatrix y: outputVector [
8068
81-
82- ]
83-
84- { #category : #accessing }
85- AISupportVectorMachinesSoftSGD >> regularizationStrenght: aNumber [
86- regularizationStrenght := aNumber
87- ]
88-
89- { #category : #' as yet unclassified' }
90- AISupportVectorMachinesSoftSGD >> sgdWithStoppageCriterionX: inputMatrix y: outputVector [
69+ " Stochastic Gradient Descent With Stoppage Criterion"
9170
9271 | ascent nth prev_cost cost cost_threshold weightsAllAlong |
9372 weightsAllAlong := OrderedCollection new .
9473 nth := 0 .
9574 prev_cost := Float infinity.
9675 cost_threshold := 0.01 .
97- weights := OrderedCollection new : inputMatrix numberOfColumns withAll: 0 .
76+ weights := OrderedCollection
77+ new : inputMatrix first size
78+ withAll: 0 .
9879 1 to: maxEpochs do: [ :epoch |
9980 inputMatrix shuffled.
10081 outputVector shuffled.
@@ -111,31 +92,41 @@ AISupportVectorMachinesSoftSGD >> sgdWithStoppageCriterionX: inputMatrix y: outp
11192 ^ weightsAllAlong
11293]
11394
95+ { #category : #accessing }
96+ AISupportVectorMachines >> learningRate: aNumber [
97+ learningRate := aNumber
98+ ]
99+
100+ { #category : #accessing }
101+ AISupportVectorMachines >> maxEpochs: aNumber [
102+ maxEpochs := aNumber
103+ ]
104+
114105{ #category : #' as yet unclassified' }
115- AISupportVectorMachinesSoftSGD >> sgdX : inputMatrix y: outputVector [
106+ AISupportVectorMachines >> predict : inputMatrix [
116107
117- | ascent |
118- weights := OrderedCollection new : inputMatrix numberOfColumns withAll: 0 .
119- 1 to: maxEpochs do: [ :epoch |
120- inputMatrix shuffled.
121- outputVector shuffled.
122- inputMatrix doWithIndex: [ :x :ind |
123- ascent := self calculateCostGradientX: x y: (outputVector at: ind).
124- weights := weights - (learningRate * ascent) ] ]
108+ ^ self signFunction: inputMatrix
109+
110+
111+ ]
112+
113+ { #category : #accessing }
114+ AISupportVectorMachines >> regularizationStrenght: aNumber [
115+ regularizationStrenght := aNumber
125116]
126117
127118{ #category : #' as yet unclassified' }
128- AISupportVectorMachinesSoftSGD >> signFunction: inputMatrix [
119+ AISupportVectorMachines >> signFunction: inputMatrix [
129120
130121 ^ inputMatrix collect: [ :row | ((row * weights) sum) sign ]
131122]
132123
133124{ #category : #accessing }
134- AISupportVectorMachinesSoftSGD >> weights [
125+ AISupportVectorMachines >> weights [
135126 ^ weights
136127]
137128
138129{ #category : #accessing }
139- AISupportVectorMachinesSoftSGD >> weights: aCollection [
130+ AISupportVectorMachines >> weights: aCollection [
140131 weights := aCollection
141132]
0 commit comments