Example #1
0
class PerceptronBinary( Binary ):
	""" Linear classifier with online learning and perceptron update
	(in primal form)"""
	def __init__( self, bias=False ):
		Binary.__init__(self, bias=bias )
		self._learner = OnlineLearner(self)
		return

	def learn( self, data, epochs=1, start_iter=0, write_every_iter=False, modelpath=None, forget_first=False):
		instances = self._get_train_stream( data )
		self._learner.learn( instances, epochs, start_iter, write_every_iter, modelpath, forget_first )
		return

	def update( self, instance, prediction ):
		""" perceptron update rule:
		w = w + y * x
		"""
		w = self._weights
		fv = instance.get_fv()
		t_lab = int(instance.get_target_label())
		p_lab = int(prediction.get_label())
		error = (p_lab != t_lab)
		# ...
                if (error) :
                    self._weights = w + dot(t_lab, fv)
		return error
Example #2
0
class PerceptronBinary( Binary ):
	""" Linear classifier with online learning and perceptron update
	(in primal form)"""
	def __init__( self, bias=False ):
		Binary.__init__(self, bias=bias )
		self._learner = OnlineLearner(self)
		return

	def learn( self, data, epochs=1, start_iter=0, write_every_iter=False, modelpath=None, forget_first=False):
		instances = self._get_train_stream( data )
		self._learner.learn( instances, epochs, start_iter, write_every_iter, modelpath, forget_first )
		return

	def update( self, instance, prediction ):
		""" perceptron update rule:
		NOOOOOP!!! -> w = w + (y - y^) * x
		"""
		w = self._weights
		# x_t
		fv = instance.get_fv()
		# y_t
		t_lab = int(instance.get_target_label())
		# predict y_t
		p_lab = int(prediction.get_label())
		error = (p_lab != t_lab)

		if error:
			self._weights += t_lab * fv

		return error
Example #3
0
class PerceptronABinary(Binary):
    """ Linear classifier with online learning and perceptron update
	(in primal form)"""
    def __init__(self, bias=False):
        Binary.__init__(self, bias=bias)
        self._learner = OnlineLearner(self)
        self._w_average = self._weights

    def _decode(self, instance, weights):
        """ return prediction in {-1, 1}
		for current instance based on linear combination of given
		weight parameters """
        fv = instance.get_fv()
        score = dot(self._w_average, fv)
        return Prediction(score)

    def resize_weights(self, instance):
        if len(self._weights) != self._feature_alphabet.size():
            self._weights.resize(self._feature_alphabet.size())
            self._w_average.resize(self._feature_alphabet.size())
        return

    def learn(self,
              data,
              epochs=1,
              start_iter=0,
              write_every_iter=False,
              modelpath=None,
              forget_first=False):
        instances = self._get_train_stream(data)
        self._learner.learn(instances, epochs, start_iter, write_every_iter,
                            modelpath, forget_first)
        return

    def update(self, instance, prediction):
        """ perceptron update rule:
		NOOOOOP!!! -> w = w + (y - y^) * x
		"""
        w = self._weights
        self._w_average += w
        # x_t
        fv = instance.get_fv()
        # y_t
        t_lab = int(instance.get_target_label())
        # predict y_t
        p_lab = int(prediction.get_label())
        error = (p_lab != t_lab)

        if error:
            self._weights += t_lab * fv

        return error
Example #4
0
class PerceptronABinary( Binary ):
	""" Linear classifier with online learning and perceptron update
	(in primal form)"""
	def __init__( self, bias=False ):
		Binary.__init__(self, bias=bias )
		self._learner = OnlineLearner(self)
		self._w_average = self._weights

	def _decode( self, instance, weights ):
		""" return prediction in {-1, 1}
		for current instance based on linear combination of given
		weight parameters """
		fv = instance.get_fv()
		score = dot( self._w_average, fv )
		return Prediction( score )

	def resize_weights(self, instance):
		if len(self._weights) != self._feature_alphabet.size():
			self._weights.resize(self._feature_alphabet.size())
			self._w_average.resize(self._feature_alphabet.size())
		return

	def learn( self, data, epochs=1, start_iter=0, write_every_iter=False, modelpath=None, forget_first=False):
		instances = self._get_train_stream( data )
		self._learner.learn( instances, epochs, start_iter, write_every_iter, modelpath, forget_first )
		return

	def update( self, instance, prediction ):
		""" perceptron update rule:
		NOOOOOP!!! -> w = w + (y - y^) * x
		"""
		w = self._weights
		self._w_average += w
		# x_t
		fv = instance.get_fv()
		# y_t
		t_lab = int(instance.get_target_label())
		# predict y_t
		p_lab = int(prediction.get_label())
		error = (p_lab != t_lab)

		if error:
			self._weights += t_lab * fv

		return error
Example #5
0
class PerceptronBinary(Binary):
    """ Linear classifier with online learning and perceptron update
	(in primal form)"""
    def __init__(self, bias=False):
        Binary.__init__(self, bias=bias)
        self._learner = OnlineLearner(self)
        return

    def learn(self,
              data,
              epochs=1,
              start_iter=0,
              write_every_iter=False,
              modelpath=None,
              forget_first=False):
        instances = self._get_train_stream(data)
        self._learner.learn(instances, epochs, start_iter, write_every_iter,
                            modelpath, forget_first)
        return

    def update(self, instance, prediction):
        """ perceptron update rule:
		NOOOOOP!!! -> w = w + (y - y^) * x
		"""
        w = self._weights
        # x_t
        fv = instance.get_fv()
        # y_t
        t_lab = int(instance.get_target_label())
        # predict y_t
        p_lab = int(prediction.get_label())
        error = (p_lab != t_lab)

        if error:
            self._weights += t_lab * fv

        return error
Example #6
0
 def __init__(self, bias=False):
     Binary.__init__(self, bias=bias)
     self._learner = OnlineLearner(self)
     self._w_average = self._weights
Example #7
0
 def __init__(self, bias=False):
     Binary.__init__(self, bias=bias)
     self._learner = OnlineLearner(self)
     return
Example #8
0
	def __init__( self, bias=False ):
		Binary.__init__(self, bias=bias )
		self._learner = OnlineLearner(self)
		return
Example #9
0
	def __init__( self, bias=False ):
		Binary.__init__(self, bias=bias )
		self._learner = OnlineLearner(self)
		self._w_average = self._weights
Example #10
0
	def __init__( self, bias=False, avgmode=False ):
		Binary.__init__(self, bias=bias, avgmode=avgmode )
		self._learner = OnlineLearner(self)
		return