-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathlbfgs_cr_entr_loss.py
114 lines (99 loc) · 3.25 KB
/
lbfgs_cr_entr_loss.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# ===============================================================================
# Copyright 2014 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
# daal4py LBFGS (limited memory Broyden-Fletcher-Goldfarb-Shanno)
# example for shared memory systems
# using cross entropy loss function
from pathlib import Path
import numpy as np
from readcsv import pd_read_csv
import daal4py as d4p
def main(readcsv=pd_read_csv):
nFeatures = 6
nClasses = 5
nIterations = 1000
stepLength = 1.0e-4
data_path = Path(__file__).parent / "data" / "batch"
infile = data_path / "logreg_train.csv"
# Read the data
data = readcsv(infile, range(nFeatures))
dep_data = readcsv(infile, range(nFeatures, nFeatures + 1))
nVectors = data.shape[0]
# configure a function
func = d4p.optimization_solver_cross_entropy_loss(
nClasses, nVectors, interceptFlag=True
)
func.setup(data, dep_data)
# configure a algorithm
stepLengthSequence = np.array([[stepLength]], dtype=np.double)
alg = d4p.optimization_solver_lbfgs(
func, stepLengthSequence=stepLengthSequence, nIterations=nIterations
)
# do the computation
nParameters = nClasses * (nFeatures + 1)
initialPoint = np.full((nParameters, 1), 0.001, dtype=np.double)
res = alg.compute(initialPoint)
# result provides minimum and nIterations
assert res.minimum.shape == (nParameters, 1)
assert res.nIterations[0][0] <= nIterations
return res
if __name__ == "__main__":
res = main()
print(
"\nExpected coefficients:\n",
np.array(
[
[-2.277],
[2.836],
[14.985],
[0.511],
[7.510],
[-2.831],
[-5.814],
[-0.033],
[13.227],
[-24.447],
[3.730],
[10.394],
[-10.461],
[-0.766],
[0.077],
[1.558],
[-1.133],
[2.884],
[-3.825],
[7.699],
[2.421],
[-0.135],
[-6.996],
[1.785],
[-2.294],
[-9.819],
[1.692],
[-0.725],
[0.069],
[-8.41],
[1.458],
[-3.306],
[-4.719],
[5.507],
[-1.642],
],
dtype=np.double,
),
)
print("\nResulting coefficients:\n", res.minimum)
print("\nNumber of iterations performed:\n", res.nIterations[0][0])
print("All looks good!")