Skip to content

Commit c7be783

Browse files
author
bezzazi abir
committed
conflict solving
2 parents 1eb9f71 + 915d2af commit c7be783

File tree

3 files changed

+54
-144
lines changed

3 files changed

+54
-144
lines changed

src/AI-SupportVectorMachines-Tests/AISupportVectorMachinesSoftSGDTest.class.st renamed to src/AI-SupportVectorMachines-Tests/AISupportVectorMachinesTest.class.st

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
An AISupportVectorMachinesSoftSGDTest is a test class for testing the behavior of AISupportVectorMachinesSoftSGD
33
"
44
Class {
5-
#name : #AISupportVectorMachinesSoftSGDTest,
5+
#name : #AISupportVectorMachinesTest,
66
#superclass : #TestCase,
77
#instVars : [
88
'model'
@@ -11,14 +11,14 @@ Class {
1111
}
1212

1313
{ #category : #running }
14-
AISupportVectorMachinesSoftSGDTest >> setUp [
14+
AISupportVectorMachinesTest >> setUp [
1515
super setUp.
1616

17-
model := AISupportVectorMachinesSoftSGD new
17+
model := AISupportVectorMachines new
1818
]
1919

2020
{ #category : #test }
21-
AISupportVectorMachinesSoftSGDTest >> testCalculateCostGradientXY [
21+
AISupportVectorMachinesTest >> testCalculateCostGradientXY [
2222

2323
| x y w res |
2424
x := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
@@ -31,7 +31,7 @@ AISupportVectorMachinesSoftSGDTest >> testCalculateCostGradientXY [
3131
]
3232

3333
{ #category : #test }
34-
AISupportVectorMachinesSoftSGDTest >> testCalculateDistancesForXY [
34+
AISupportVectorMachinesTest >> testCalculateDistancesForXY [
3535

3636
| x y w res |
3737
x := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
@@ -43,7 +43,7 @@ AISupportVectorMachinesSoftSGDTest >> testCalculateDistancesForXY [
4343
]
4444

4545
{ #category : #test }
46-
AISupportVectorMachinesSoftSGDTest >> testCalculateDistancesForXYForLittleNumbers [
46+
AISupportVectorMachinesTest >> testCalculateDistancesForXYForLittleNumbers [
4747

4848
| x y w res |
4949
x := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
@@ -55,7 +55,7 @@ AISupportVectorMachinesSoftSGDTest >> testCalculateDistancesForXYForLittleNumber
5555
]
5656

5757
{ #category : #test }
58-
AISupportVectorMachinesSoftSGDTest >> testComputeCostXY [
58+
AISupportVectorMachinesTest >> testComputeCostXY [
5959

6060
| x y w res |
6161
x := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
@@ -69,79 +69,41 @@ AISupportVectorMachinesSoftSGDTest >> testComputeCostXY [
6969
]
7070

7171
{ #category : #test }
72-
AISupportVectorMachinesSoftSGDTest >> testPredict [
73-
74-
| expectedOutput x testInput y actualOutput |
75-
x := AIMatrix from: #( #( 1 0 0 ) #( 1 1 1 ) #( 1 3 3 ) #( 1 4 4 ) ).
76-
y := #( 1 1 -1 -1 ).
77-
78-
testInput := AIMatrix from:
79-
#( #( 1 -1 -1 ) #( 1 5 5 ) #( 1 6 6 ) #( 1 7 7 ) ).
80-
expectedOutput := #( 1 -1 -1 -1 ).
81-
model regularizationStrenght: 10000.
82-
model learningRate: 1e-6.
83-
model maxEpochs: 5000.
84-
model sgdWithStoppageCriterionX: x y: y.
85-
86-
actualOutput := model predict: testInput.
87-
88-
actualOutput
89-
with: expectedOutput
90-
do: [ :actual :expected | self assert: actual equals: expected ]
91-
]
92-
93-
{ #category : #tests }
94-
AISupportVectorMachinesSoftSGDTest >> testSgdWithStoppageCriterionRealisticExampleXY [
72+
AISupportVectorMachinesTest >> testFitXY [
9573

9674
| x y res |
97-
x := AIMatrix from: #( #( 1 0 0 ) #( 1 1 1 ) #( 1 3 3 ) #( 1 4 4 ) ).
75+
x := #( #( 1 0 0 ) #( 1 1 1 ) #( 1 3 3 ) #( 1 4 4 ) ).
9876
y := #( 1 1 -1 -1 ).
9977

10078
model regularizationStrenght: 10000.
10179
model learningRate: 1e-6.
10280
model maxEpochs: 5000.
10381

104-
res := model sgdWithStoppageCriterionX: x y: y.
82+
res := model fitX: x y: y.
83+
10584

10685
model weights
10786
with: #( 2.03034695 -0.5100166 -0.5100166 )
10887
do: [ :real :expected | self assert: real closeTo: expected ]
10988
]
11089

111-
{ #category : #tests }
112-
AISupportVectorMachinesSoftSGDTest >> testSgdWithStoppageCriterionXY [
90+
{ #category : #test }
91+
AISupportVectorMachinesTest >> testPredict [
11392

114-
| x y res |
115-
x := AIMatrix from: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
116-
y := #( -1 1 1 ).
93+
| expectedOutput x testInput y actualOutput |
94+
x := #( #( 1 0 0 ) #( 1 1 1 ) #( 1 3 3 ) #( 1 4 4 ) ).
95+
y := #( 1 1 -1 -1 ).
11796

97+
testInput := #( #( 1 -1 -1 ) #( 1 5 5 ) #( 1 6 6 ) #( 1 7 7 ) ).
98+
expectedOutput := #( 1 -1 -1 -1 ).
11899
model regularizationStrenght: 10000.
119100
model learningRate: 1e-6.
120101
model maxEpochs: 5000.
102+
model fitX: x y: y.
121103

122-
res := model sgdWithStoppageCriterionX: x y: y.
123-
124-
model weights
125-
with: #( 1.44873055 0.23656344 -0.97560368 )
126-
do: [ :real :expected | self assert: real closeTo: expected ]
127-
]
128-
129-
{ #category : #tests }
130-
AISupportVectorMachinesSoftSGDTest >> testSgdXY [
131-
132-
"temporary"
133-
134-
| x y res |
135-
x := AIMatrix from: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
136-
y := #( -1 1 1 ).
137-
138-
model regularizationStrenght: 10000.
139-
140-
model learningRate: 1e-6.
141-
model maxEpochs: 5000.
142-
res := model sgdX: x y: y.
104+
actualOutput := model predict: testInput.
143105

144-
model weights
145-
with: #( 1.44873055 0.23656344 -0.97560368 )
146-
do: [ :real :expected | self assert: real closeTo: expected ]
106+
actualOutput
107+
with: expectedOutput
108+
do: [ :actual :expected | self assert: actual equals: expected ]
147109
]

src/AI-SupportVectorMachines/AIMatrix.class.st

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/AI-SupportVectorMachines/AISupportVectorMachinesSoftSGD.class.st renamed to src/AI-SupportVectorMachines/AISupportVectorMachines.class.st

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
I implement the soft margin of Support Vector Machines with Stochastic Gradient Descent.
33
"
44
Class {
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

Comments
 (0)