mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
Merge pull request #56 from albrow/master
Use a user-configurable log.Logger to print to console instead of fmt.Println
This commit is contained in:
commit
a3f53c8fde
34
base/logger.go
Normal file
34
base/logger.go
Normal file
@ -0,0 +1,34 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Logger is the default logger for the entire golearn package. It writes
|
||||
// to stdout and has no prefix and no flags.
|
||||
var Logger *log.Logger = log.New(os.Stdout, "", 0)
|
||||
|
||||
// SetLogger sets the base logger for the entire golearn package.
|
||||
func SetLogger(logger *log.Logger) {
|
||||
Logger = logger
|
||||
}
|
||||
|
||||
// SetLoggerOut creates a new base logger for the entire golearn
|
||||
// package using the given out instead of the default, os.Stdout.
|
||||
// The other log options are set to the default, i.e. no prefix and no
|
||||
// flags.
|
||||
func SetLoggerOut(out io.Writer) {
|
||||
Logger = log.New(out, "", 0)
|
||||
}
|
||||
|
||||
// Silent turns off logging throughout the golearn package by setting
|
||||
// the logger to write to dev/null.
|
||||
func Silent() {
|
||||
if out, err := os.Open(os.DevNull); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
Logger = log.New(out, "", 0)
|
||||
}
|
||||
}
|
14
linear_models/cfuncs.go
Normal file
14
linear_models/cfuncs.go
Normal file
@ -0,0 +1,14 @@
|
||||
// +build go1.2
|
||||
|
||||
package linear_models
|
||||
|
||||
/*
|
||||
|
||||
void libLinearPrintFunc(char *);
|
||||
|
||||
void golearn_liblinear_print_func_cgo(char *c) {
|
||||
libLinearPrintFunc(c);
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
@ -59,6 +59,7 @@ func NewProblem(X [][]float64, y []float64, bias float64) *Problem {
|
||||
}
|
||||
|
||||
func Train(prob *Problem, param *Parameter) *Model {
|
||||
libLinearHookPrintFunc() // Sets up logging
|
||||
return &Model{C.train(&prob.c_prob, ¶m.c_param)}
|
||||
}
|
||||
|
||||
|
27
linear_models/liblinear_print.go
Normal file
27
linear_models/liblinear_print.go
Normal file
@ -0,0 +1,27 @@
|
||||
// +build go1.2
|
||||
|
||||
package linear_models
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -llinear
|
||||
#cgo CFLAGS:
|
||||
#include <linear.h>
|
||||
|
||||
typedef void (*print_func)(char *);
|
||||
void golearn_liblinear_print_func_cgo(char *);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"github.com/sjwhitworth/golearn/base"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//export libLinearPrintFunc
|
||||
func libLinearPrintFunc(str *C.char) {
|
||||
base.Logger.Println(C.GoString(str))
|
||||
}
|
||||
|
||||
func libLinearHookPrintFunc() {
|
||||
C.set_print_string_function((C.print_func)(unsafe.Pointer(C.golearn_liblinear_print_func_cgo)))
|
||||
}
|
16
linear_models/liblinear_print_11.go
Normal file
16
linear_models/liblinear_print_11.go
Normal file
@ -0,0 +1,16 @@
|
||||
// +build go1.1
|
||||
// +build !go1.2
|
||||
// +build !go1.3
|
||||
|
||||
package linear_models
|
||||
|
||||
import "C"
|
||||
|
||||
//export libLinearPrintFunc
|
||||
func libLinearPrintFunc(str *C.char) {
|
||||
// Stubbed
|
||||
}
|
||||
|
||||
func libLinearHookPrintFunc() {
|
||||
// Stubbed
|
||||
}
|
@ -40,7 +40,7 @@ func convertInstancesToProblemVec(X *base.Instances) [][]float64 {
|
||||
problemVecCounter++
|
||||
}
|
||||
}
|
||||
fmt.Println(problemVec, X)
|
||||
base.Logger.Println(problemVec, X)
|
||||
return problemVec
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (lr *LogisticRegression) Predict(X *base.Instances) *base.Instances {
|
||||
rowCounter++
|
||||
}
|
||||
}
|
||||
fmt.Println(Predict(lr.model, row), row)
|
||||
base.Logger.Println(Predict(lr.model, row), row)
|
||||
ret.Set(i, 0, Predict(lr.model, row))
|
||||
}
|
||||
return ret
|
||||
|
@ -25,7 +25,7 @@ func (r *RandomTreeRuleGenerator) GenerateSplitAttribute(f *base.Instances) base
|
||||
break
|
||||
}
|
||||
selectedAttribute := rand.Intn(maximumAttribute)
|
||||
fmt.Println(selectedAttribute, attrCounter, consideredAttributes, len(consideredAttributes))
|
||||
base.Logger.Println(selectedAttribute, attrCounter, consideredAttributes, len(consideredAttributes))
|
||||
if selectedAttribute != f.ClassIndex {
|
||||
matched := false
|
||||
for _, a := range consideredAttributes {
|
||||
|
Loading…
x
Reference in New Issue
Block a user