Skip to content

Instantly share code, notes, and snippets.

@ikegami-yukino
Last active September 13, 2017 08:51
Show Gist options
  • Save ikegami-yukino/6102212 to your computer and use it in GitHub Desktop.
Save ikegami-yukino/6102212 to your computer and use it in GitHub Desktop.
OLL client is a client for using OLL on Python. OLL is a library supporting several for online-learning algorithms.
from subprocess import Popen, PIPE
import time, sys, os
"""
oll_client 0.0.3 - Online Machine Learning Module
OLL client is a client for using OLL, which is a machine learning library have implemented several online-learning algorithms, on Python.
Currently, Oll supports following algorithms:
- Perceptron
- Averaged Perceptron
- Passive Agressive (PA, PA-I, PA-II)
- ALMA (modified slightly from original)
- Confidence Weighted Linear-Classification.
When using this module, please install OLL at following Web site:
https://code.google.com/p/oll/
Author:
Yukino Ikegami
Lisence:
MIT License
Usage:
import oll_client
oll = oll_client.OLL('PA1', debug=True)
oll.train(1, {0:1.0, 1:2.0, 2:-1.0}) # Add training example
oll.train(-1, {0:-0.5, 1:1.0, 2:-0.5})
oll.test(0, {0:1.0, 1:1.0}) # Test Example
oll.save('model1') # Save current model to modelfile
oll.load('model1') # Load a model from modelfile
oll.test(0, {0:1.0, 1:1.0})
"""
class OLL:
def __init__(self, algorythm='P', tempfile='tempfile.txt', oll_check=True, debug=False):
if oll_check:
test = Popen('which oll_line', shell=True, stdin=PIPE, stdout=PIPE)
if len(test.communicate()[0]) < 1:
print 'Please install oll https://code.google.com/p/oll/'
sys.exit()
self.algorythm = algorythm
self.tempfile = tempfile
initialize_file(tempfile)
command = 'oll_line %s > %s' % (algorythm, tempfile)
self.oll = Popen(command, shell=True, stdin=PIPE, stdout=PIPE)
self.debug = debug
def __del__(self):
os.remove(self.tempfile)
def train(self, label, data):
query = 'A %d ' % (label)
for feature, value in data.items():
query += '%d:%s ' % (feature, value)
query = query[:-1]+'\n'
return self._send(query)
def test(self, label, data):
query = 'T %d ' % (label)
for feature, value in data.items():
query += '%d:%s ' % (feature, value)
query = query[:-1]+'\n'
return self._send(query)
def save(self, model):
query = 'S %s\n' % (model)
return self._send(query)
def load(self, model):
query = 'L %s\n' % (model)
return self._send(query)
def _send(self, query):
if self.debug:
print query[:-1]
self.oll.stdin.write(query)
self.oll.stdin.flush()
if query[0] == 'T':
return self._get_result_from_file()
return True
def _get_result_from_file(self):
while True:
time.sleep(0.0001)
with open(self.tempfile,'r') as out:
result = out.read().decode('utf8').strip()
if len(result) > 0:
break
initialize_file(self.tempfile)
result = result.replace(u'\x00','')
if self.debug:
print float(result)
return float(result)
def initialize_file(filename):
open(filename,'w')
if __name__ == '__main__':
o = OLL('PA1', debug=True)
o.train(1, {0:1.0, 1:2.0, 2:-1.0})
o.train(-1, {0:-0.5, 1:1.0, 2:-0.5})
o.test(0, {0:1.0, 1:1.0})
o.save('model1')
o.load('model1')
o.test(0, {0:1.0, 1:1.0})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment