[mlpack-svn] r15488 - mlpack/conf/jenkins-conf/benchmark/methods/scikit

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Jul 17 13:08:46 EDT 2013


Author: marcus
Date: Wed Jul 17 13:08:46 2013
New Revision: 15488

Log:
Add scikit sparse coding benchmark script.

Added:
   mlpack/conf/jenkins-conf/benchmark/methods/scikit/sparse_coding.py

Added: mlpack/conf/jenkins-conf/benchmark/methods/scikit/sparse_coding.py
==============================================================================
--- (empty file)
+++ mlpack/conf/jenkins-conf/benchmark/methods/scikit/sparse_coding.py	Wed Jul 17 13:08:46 2013
@@ -0,0 +1,85 @@
+'''
+  @file sparse_coding.py
+  @author Marcus Edel
+
+  Sparse Coding with scikit.
+'''
+
+import os
+import sys
+import inspect
+
+# Import the util path, this method even works if the path contains symlinks to
+# modules.
+cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(
+  os.path.split(inspect.getfile(inspect.currentframe()))[0], "../../util")))
+if cmd_subfolder not in sys.path:
+  sys.path.insert(0, cmd_subfolder)
+
+from log import *
+from timer import *
+
+import numpy as np
+from sklearn.decomposition import SparseCoder
+
+'''
+This class implements the Sparse Coding benchmark.
+'''
+class SparseCoding(object):
+
+  ''' 
+  Create the Sparse Coding benchmark instance.
+  
+  @param dataset - Input dataset to perform Sparse Coding on.
+  @param verbose - Display informational messages.
+  '''
+  def __init__(self, dataset, verbose=True): 
+    self.verbose = verbose
+    self.dataset = dataset
+
+  '''
+  Destructor to clean up at the end.
+  '''
+  def __del__(self):
+    pass
+
+  '''
+  Use the scikit libary to implement Sparse Coding.
+
+  @param options - Extra options for the method.
+  @return - Elapsed time in seconds or -1 if the method was not successful.
+  '''
+  def SparseCodingScikit(self, options):
+    totalTimer = Timer()
+
+    # Load input dataset.
+    inputData = np.genfromtxt(self.dataset[0], delimiter=',')
+    dictionary = np.genfromtxt(self.dataset[1], delimiter=',')
+
+    # Get all the parameters.
+    l = re.search("-l (\d+)", options)
+    l = 0 if not l else int(l.group(1))
+
+    with totalTimer:
+      # Perform Sparse Coding.
+      model = SparseCoder(dictionary=dictionary, transform_algorithm='lars',
+          transform_alpha=l)
+      code = model.transform(inputData)
+
+    return totalTimer.ElapsedTime()
+
+  '''
+  Perform Sparse Coding. If the method has been successfully completed 
+  return the elapsed time in seconds.
+
+  @param options - Extra options for the method.
+  @return - Elapsed time in seconds or -1 if the method was not successful.
+  '''
+  def RunMethod(self, options):
+    Log.Info("Perform Sparse Coding.", self.verbose)
+
+    if len(self.dataset) != 2:
+      Log.Fatal("The method need two datasets.")
+      return -1
+
+    return self.SparseCodingScikit(options)



More information about the mlpack-svn mailing list