def __demo__ () : from nlplib.exterior.util import plot from nlplib.general.math import avg def fitness (individual) : return individual.traits['value'] def mutate (individual) : individual.traits['value'] += round(random.triangular(-10, 10)) selection = Selection('foo', fitness, mutate) for _ in range(100) : individual = selection.individual() individual.traits['value'] = 0 mutate(individual) selection.population.add(individual) fitnesses = [selection.generation() for _ in range(100)] # Fitness increases with each successive generation. plot([avg(fitness) for fitness in fitnesses])
def __demo__ (ut) : from nlplib.data import builtin_db from nlplib.core.model import Word, NeuralNetwork from nlplib.exterior.train import usable from nlplib.exterior.util import plot top = 10 with builtin_db() as session : top_words = list(session.access.most_common(Word, top=top)) + [None] nn = NeuralNetwork(top_words, int(len(top_words)*2), top_words) with builtin_db() as session : patterns = usable(nn, list(session.access.all_documents())[:], gram_size=2) errors = [nn.train(input_words, output_words, rate=0.1) for input_words, output_words in patterns] from nlplib.general.math import avg print(avg(errors[:])) plot(errors, sample_size=100) def ask (string) : with builtin_db() as session : words = session.access.words(string) scored = Scored(nn.predict(words)).sorted() print('\n'.join(repr(score) for score in list(scored)[:12])) return ask