def __init__(self, steps=(1,), alpha=0.001, actValueAlpha=0.3, verbosity=0,
              callsPerSerialize=CALLS_PER_SERIALIZE):
   self._sdrClassifier = SDRClassifier(steps, alpha, actValueAlpha, verbosity)
   self._sdrClassifierCpp = SDRClassifierCpp(steps, alpha, actValueAlpha,
                                               verbosity)
   self._calls = 0
   self._callsPerSerialize = callsPerSerialize
Exemplo n.º 2
0
class SDRClassifierDiff(object):
  """Classifier-like object that diffs the output from different classifiers.

  Instances of each version of the SDR classifier are created and each call to
  compute is passed to each version of the classifier. The results are diffed
  to make sure the there are no differences.

  Optionally, the classifiers can be serialized and deserialized after a
  specified number of calls to compute to ensure that serialization does not
  cause discrepencies between the results.

  TODO: Check internal state as well.
  TODO: Provide option to write output to a file.
  TODO: Provide record differences without throwing an exception.
  """


  __VERSION__ = 'SDRClassifierDiffV1'


  def __init__(self, steps=(1,), alpha=0.001, actValueAlpha=0.3, verbosity=0,
               callsPerSerialize=CALLS_PER_SERIALIZE):
    self._sdrClassifier = SDRClassifier(steps, alpha, actValueAlpha, verbosity)
    self._sdrClassifierCpp = SDRClassifierCpp(steps, alpha, actValueAlpha,
                                                verbosity)
    self._calls = 0
    self._callsPerSerialize = callsPerSerialize


  def compute(self, recordNum, patternNZ, classification, learn, infer):
    result1 = self._sdrClassifier.compute(recordNum, patternNZ, classification,
                                          learn, infer)
    result2 = self._sdrClassifierCpp.compute(recordNum, patternNZ,
                                              classification, learn, infer)
    self._calls += 1
    # Check if it is time to serialize and deserialize.
    if self._calls % self._callsPerSerialize == 0:
      self._sdrClassifier = pickle.loads(pickle.dumps(self._sdrClassifier))
      self._sdrClassifierCpp = pickle.loads(pickle.dumps(
          self._sdrClassifierCpp))
    # Assert both results are the same type.
    assert type(result1) == type(result2)
    # Assert that the keys match.
    assert set(result1.keys()) == set(result2.keys()), "diff detected: " \
      "py result=%s, C++ result=%s" % (result1, result2)
    # Assert that the values match.
    for k, l in result1.iteritems():
      assert type(l) == type(result2[k])
      for i in xrange(len(l)):
        if isinstance(classification['actValue'], numbers.Real):
          assert abs(float(l[i]) - float(result2[k][i])) < 0.0000001, (
              'Python SDRClassifier has value %f and C++ SDRClassifierCpp has '
              'value %f.' % (l[i], result2[k][i]))
        else:
          assert l[i] == result2[k][i], (
              'Python SDRClassifier has value %s and C++ SDRClassifierCpp has '
              'value %s.' % (str(l[i]), str(result2[k][i])))
    return result1
 def read(proto):
     """
 proto: SDRClassifierRegionProto capnproto object
 """
     impl = proto.implementation
     if impl == "py":
         return SDRClassifier.read(proto.sdrClassifier)
     elif impl == "cpp":
         return FastSDRClassifier.read(proto.sdrClassifier)
     else:
         raise ValueError("Invalid classifier implementation (%r). Value must be " '"py" or "cpp".' % impl)
 def read(proto):
     """
 :param proto: SDRClassifierRegionProto capnproto object
 """
     impl = proto.implementation
     if impl == 'py':
         return SDRClassifier.read(proto.sdrClassifier)
     elif impl == 'cpp':
         return FastSDRClassifier.read(proto.sdrClassifier)
     else:
         raise ValueError(
             'Invalid classifier implementation (%r). Value must be '
             '"py" or "cpp".' % impl)
Exemplo n.º 5
0
 def read(proto):
   """
   :param proto: SDRClassifierRegionProto capnproto object
   """
   impl = proto.implementation
   if impl == 'py':
     return SDRClassifier.read(proto.sdrClassifier)
   elif impl == 'cpp':
     return FastSDRClassifier.read(proto.sdrClassifier)
   elif impl == 'diff':
     return SDRClassifierDiff.read(proto.sdrClassifier)
   else:
     raise ValueError('Invalid classifier implementation (%r). Value must be '
                      '"py", "cpp" or "diff".' % impl)
Exemplo n.º 6
0
    def compute(self, recordNum, patternNZ, classification, learn, infer):
        result1 = self._sdrClassifier.compute(recordNum, patternNZ,
                                              classification, learn, infer)
        result2 = self._sdrClassifierCpp.compute(recordNum, patternNZ,
                                                 classification, learn, infer)
        self._calls += 1
        # Check if it is time to serialize and deserialize.
        if self._calls % self._callsPerSerialize == 0:
            schemaPy = self._sdrClassifier.getSchema()
            protoPy = schemaPy.new_message()
            self._sdrClassifier.write(protoPy)
            protoPy = schemaPy.from_bytes(protoPy.to_bytes())
            self._sdrClassifier = SDRClassifier.read(protoPy)

            schemaCpp = self._sdrClassifierCpp.getSchema()
            protoCpp = schemaCpp.new_message()
            self._sdrClassifierCpp.write(protoCpp)
            protoCpp = schemaCpp.from_bytes(protoCpp.to_bytes())
            self._sdrClassifierCpp = SDRClassifierCpp.read(protoCpp)

        # Assert both results are the same type.
        assert type(result1) == type(result2)
        # Assert that the keys match.
        assert set(result1.keys()) == set(result2.keys()), "diff detected: " \
          "py result=%s, C++ result=%s" % (result1, result2)
        # Assert that the values match.
        for k, l in result1.items():
            assert type(l) == type(result2[k])
            for i in range(len(l)):
                if isinstance(classification['actValue'], numbers.Real):
                    assert abs(
                        float(l[i]) - float(result2[k][i])
                    ) < 0.0000001, (
                        'Python SDRClassifier has value %f and C++ SDRClassifierCpp has '
                        'value %f.' % (l[i], result2[k][i]))
                else:
                    assert l[i] == result2[k][i], (
                        'Python SDRClassifier has value %s and C++ SDRClassifierCpp has '
                        'value %s.' % (str(l[i]), str(result2[k][i])))
        return result1
Exemplo n.º 7
0
  def compute(self, recordNum, patternNZ, classification, learn, infer):
    result1 = self._sdrClassifier.compute(recordNum, patternNZ, classification,
                                          learn, infer)
    result2 = self._sdrClassifierCpp.compute(recordNum, patternNZ,
                                              classification, learn, infer)
    self._calls += 1
    # Check if it is time to serialize and deserialize.
    if self._calls % self._callsPerSerialize == 0:
      schemaPy = self._sdrClassifier.getSchema()
      protoPy = schemaPy.new_message()
      self._sdrClassifier.write(protoPy)
      protoPy = schemaPy.from_bytes(protoPy.to_bytes())
      self._sdrClassifier = SDRClassifier.read(protoPy)

      schemaCpp = self._sdrClassifierCpp.getSchema()
      protoCpp = schemaCpp.new_message()
      self._sdrClassifierCpp.write(protoCpp)
      protoCpp = schemaCpp.from_bytes(protoCpp.to_bytes())
      self._sdrClassifierCpp = SDRClassifierCpp.read(protoCpp)

    # Assert both results are the same type.
    assert type(result1) == type(result2)
    # Assert that the keys match.
    assert set(result1.keys()) == set(result2.keys()), "diff detected: " \
      "py result=%s, C++ result=%s" % (result1, result2)
    # Assert that the values match.
    for k, l in result1.iteritems():
      assert type(l) == type(result2[k])
      for i in xrange(len(l)):
        if isinstance(classification['actValue'], numbers.Real):
          assert abs(float(l[i]) - float(result2[k][i])) < 0.0000001, (
              'Python SDRClassifier has value %f and C++ SDRClassifierCpp has '
              'value %f.' % (l[i], result2[k][i]))
        else:
          assert l[i] == result2[k][i], (
              'Python SDRClassifier has value %s and C++ SDRClassifierCpp has '
              'value %s.' % (str(l[i]), str(result2[k][i])))
    return result1
class SDRClassifierDiff(object):
  """Classifier-like object that diffs the output from different classifiers.

  Instances of each version of the SDR classifier are created and each call to
  compute is passed to each version of the classifier. The results are diffed
  to make sure the there are no differences.

  Optionally, the classifiers can be serialized and deserialized after a
  specified number of calls to compute to ensure that serialization does not
  cause discrepencies between the results.

  TODO: Check internal state as well.
  TODO: Provide option to write output to a file.
  TODO: Provide record differences without throwing an exception.
  """


  __VERSION__ = 'SDRClassifierDiffV1'


  def __init__(self, steps=(1,), alpha=0.001, actValueAlpha=0.3, verbosity=0,
               callsPerSerialize=CALLS_PER_SERIALIZE):
    self._sdrClassifier = SDRClassifier(steps, alpha, actValueAlpha, verbosity)
    self._sdrClassifierCpp = SDRClassifierCpp(steps, alpha, actValueAlpha,
                                                verbosity)
    self._calls = 0
    self._callsPerSerialize = callsPerSerialize


  def compute(self, recordNum, patternNZ, classification, learn, infer):
    result1 = self._sdrClassifier.compute(recordNum, patternNZ, classification,
                                          learn, infer)
    result2 = self._sdrClassifierCpp.compute(recordNum, patternNZ,
                                              classification, learn, infer)
    self._calls += 1
    # Check if it is time to serialize and deserialize.
    if self._calls % self._callsPerSerialize == 0:
      schemaPy = self._sdrClassifier.getSchema()
      protoPy = schemaPy.new_message()
      self._sdrClassifier.write(protoPy)
      protoPy = schemaPy.from_bytes(protoPy.to_bytes())
      self._sdrClassifier = SDRClassifier.read(protoPy)

      schemaCpp = self._sdrClassifierCpp.getSchema()
      protoCpp = schemaCpp.new_message()
      self._sdrClassifierCpp.write(protoCpp)
      protoCpp = schemaCpp.from_bytes(protoCpp.to_bytes())
      self._sdrClassifierCpp = SDRClassifierCpp.read(protoCpp)

    # Assert both results are the same type.
    assert type(result1) == type(result2)
    # Assert that the keys match.
    assert set(result1.keys()) == set(result2.keys()), "diff detected: " \
      "py result=%s, C++ result=%s" % (result1, result2)
    # Assert that the values match.
    for k, l in result1.iteritems():
      assert type(l) == type(result2[k])
      for i in xrange(len(l)):
        if isinstance(classification['actValue'], numbers.Real):
          assert abs(float(l[i]) - float(result2[k][i])) < 0.0000001, (
              'Python SDRClassifier has value %f and C++ SDRClassifierCpp has '
              'value %f.' % (l[i], result2[k][i]))
        else:
          assert l[i] == result2[k][i], (
              'Python SDRClassifier has value %s and C++ SDRClassifierCpp has '
              'value %s.' % (str(l[i]), str(result2[k][i])))
    return result1