-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release/1.0.0: Add Go templates for the
LinearSVC
estimator
- Loading branch information
Showing
4 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
sklearn_porter/estimator/LinearSVC/templates/go/exported.class.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{% extends 'base.exported.class' %} | ||
|
||
{% block content %} | ||
package main | ||
|
||
{% if is_binary %} | ||
import ( | ||
{% if is_test or to_json %} | ||
"encoding/json" | ||
{% endif %} | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
type {{ class_name }} struct { | ||
Coeffs []float64 `json:"coeffs"` | ||
Inters float64 `json:"inters"` | ||
} | ||
{% else %} | ||
import ( | ||
{% if is_test or to_json %} | ||
"encoding/json" | ||
{% endif %} | ||
"fmt" | ||
"io/ioutil" | ||
"math" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
type {{ class_name }} struct { | ||
Coeffs [][]float64 `json:"coeffs"` | ||
Inters []float64 `json:"inters"` | ||
} | ||
{% endif %} | ||
|
||
{% if is_test or to_json %} | ||
type Response struct { | ||
Predict int `json:"predict"` | ||
} | ||
{% endif %} | ||
|
||
{% if is_binary %} | ||
func (svc {{ class_name }}) Predict(features []float64) int { | ||
var prob float64 | ||
for i := 0; i < len(svc.Coeffs); i++ { | ||
prob = prob + svc.Coeffs[i] * features[i] | ||
} | ||
if (prob + svc.Inters) > 0 { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
{% else %} | ||
func (svc {{ class_name }}) Predict(features []float64) int { | ||
classIdx := 0 | ||
classVal := math.Inf(-1) | ||
outerCount, innerCount := len(svc.Coeffs), len(svc.Coeffs[0]) | ||
for i := 0; i < outerCount; i++ { | ||
var prob float64 | ||
for j := 0; j < innerCount; j++ { | ||
prob = prob + svc.Coeffs[i][j] * features[j] | ||
} | ||
if prob + svc.Inters[i] > classVal { | ||
classVal = prob + svc.Inters[i] | ||
classIdx = i | ||
} | ||
} | ||
return classIdx | ||
} | ||
{% endif %} | ||
|
||
func main() { | ||
|
||
// Features: | ||
var features []float64 | ||
for _, arg := range os.Args[2:] { | ||
if n, err := strconv.ParseFloat(arg, 64); err == nil { | ||
features = append(features, n) | ||
} | ||
} | ||
|
||
// Model data: | ||
jsonFile, err := os.Open(os.Args[1]) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
defer jsonFile.Close() | ||
byteValue, _ := ioutil.ReadAll(jsonFile) | ||
|
||
// Estimator: | ||
var clf {{ class_name }} | ||
json.Unmarshal(byteValue, &clf) | ||
|
||
{% if is_test or to_json %} | ||
// Get JSON: | ||
prediction := clf.Predict(features) | ||
res, _ := json.Marshal(&Response{Predict: prediction}) | ||
fmt.Println(string(res)) | ||
{% else %} | ||
// Get class prediction: | ||
prediction := clf.Predict(features) | ||
fmt.Printf("Predicted class: #%d\n", prediction) | ||
{% endif %} | ||
|
||
} | ||
{% endblock %} |