236 | func (l *LeastSquares) Learn() error { |
237 | if l.trainingSet == nil || l.expectedResults == nil { |
238 | err := fmt.Errorf("ERROR: Attempting to learn with no training examples!\n") |
239 | fmt.Fprintf(l.Output, err.Error()) |
240 | return err |
241 | } |
242 | |
243 | examples := len(l.trainingSet) |
244 | if examples == 0 || len(l.trainingSet[0]) == 0 { |
245 | err := fmt.Errorf("ERROR: Attempting to learn with no training examples!\n") |
246 | fmt.Fprintf(l.Output, err.Error()) |
247 | return err |
248 | } |
249 | if len(l.expectedResults) == 0 { |
250 | err := fmt.Errorf("ERROR: Attempting to learn with no expected results! This isn't an unsupervised model!! You'll need to include data before you learn :)\n") |
251 | fmt.Fprintf(l.Output, err.Error()) |
252 | return err |
253 | } |
254 | |
255 | fmt.Fprintf(l.Output, "Training:\n\tModel: Logistic (Binary) Classification\n\tOptimization Method: %v\n\tTraining Examples: %v\n\tFeatures: %v\n\tLearning Rate α: %v\n\tRegularization Parameter λ: %v\n...\n\n", l.method, examples, len(l.trainingSet[0]), l.alpha, l.regularization) |
256 | |
257 | var err error |
258 | if l.method == base.BatchGA { |
259 | err = base.GradientAscent(l) |
260 | } else if l.method == base.StochasticGA { |
261 | err = base.StochasticGradientAscent(l) |
262 | } else { |
263 | err = fmt.Errorf("Chose a training method not implemented for LeastSquares regression") |
264 | } |
265 | |
266 | if err != nil { |
267 | fmt.Fprintf(l.Output, "\nERROR: Error while learning –\n\t%v\n\n", err) |
268 | return err |
269 | } |
270 | |
271 | fmt.Fprintf(l.Output, "Training Completed.\n%v\n\n", l) |
272 | return nil |
273 | } |