def __init__(self, genome = None): # network = FFNeuralNetwork(INPUT_NODES, NODES_PER_HIDDEN_LAYER, OUTPUT_NODES) network = Perceptron(INPUT_NODES, OUTPUT_NODES) if not genome: genome = network.getWeights() else: network.setWeights(genome) self.genome = genome self.network = network
def main(): try: load_bot() except: pass p = Perceptron() p.load() newsubs = read_submissions() check_submissions(newsubs, p) p.save() save_bot()
def __init__(self, feature_template, moves, tagger=None): """ Parameters ---------- - feature_template, a callable the method to extract features for the dependency parser """ self.moves = moves self.model = Perceptron(self.moves) self.tagger = tagger self.extract_features = feature_template
def startFunctionTest(): #methodName,epsilon,eta,maxCount,maxWords,testDir,trainDir inputs=[] results=[] inputFile='./Input/inputPerceptron.txt' lines=[] with open(inputFile) as f: for line in f: arr=[] arr=line.split() inputs.append(arr) for input in inputs: if input[0]=='test10Fold': results.append(Perceptron.main(input[0],float(input[1]), float(input[2]), input[3],float(input[4]))) if input[0]=='classifyDir': results.append(Perceptron.main(input[0], float(input[1]), input[2], input[3], float(input[4]))) #test10Fold takes only training directory and uses 10 fold to solve it #maxent.main('classifyDir', 0.1, 2, 50, 10000, '../data/imdb1', '../data/imdb1') createOutputFile('./Output/outputPerceptron.txt',inputs,results)
class AbstractParser(with_metaclass(abc.ABCMeta, object)) : """An abstract dependency parser that implements a transition-based strategy (either arc-hybrid or arc-eager). The parser can only predict the dependency tree structure and not the label of the different edges. """ def __init__(self, feature_template, moves, tagger=None): """ Parameters ---------- - feature_template, a callable the method to extract features for the dependency parser """ self.moves = moves self.model = Perceptron(self.moves) self.tagger = tagger self.extract_features = feature_template def parse(self, words, pos_tags=None): """ Predict the dependencies of a sentence Parameters ---------- - words, a sequence of strings the (tokenized) sentence - tags, a sequence of strings or None POS tags of the sentence; if no POS tags are given, POS are automatically predicted. """ for _, _, _, parse in self._infer(words, None, pos_tags): pass return None, parse.heads def train_online(self, words, gold_heads, pos_tags=None): """ Online training of the dependency parser with one sentence The training procedure infere the dependency tree of a sentence and update the weight vector each time a decision is made. Parameters ---------- - words, a sequence of strings the words of the sentence - gold_heads, a sequence of integers the gold dependency tree """ for guess, best, features, parse in self._infer(words, gold_heads, pos_tags): self.model.update(best, guess, features) n = len(words) return len([i for i in range(n - 1) if parse.heads[i] == gold_heads[i]]) def _infer(self, words, gold_heads=None, pos_tags=None): """Apply the current policy to build the dependency tree of the given word sequence. Parameters ---------- - words, a list of strings the (tokenized) sxsentence - gold_heads, a list of integers the gold heads. If `None`, the best move at each position is also computed. - pos_tags, a list of strings the PoS tags of the sentence. If `None`, the PoS tags are automatically predicted """ n = len(words) # Description of the state of the parser # -------------------------------------- # # Index describing where we are in the input sentence. This # index allows us to access to the “buffer” used in the # dependency litterature (buffer = sentence[idx:]) idx = 2 # Words that that occurs before idx but for which we have not # predict a head yet (--> partially processed words) stack = [1] # Structure to hold the partially predicted tree parse = Parse(n) # the decisions we made history = [] if pos_tags is None: assert self.tagger is not None, "pos_tags can not be set to None" pos_tags = self.tagger.tag(words) while stack or (idx + 1) < n: features = self.extract_features(words, pos_tags, idx, stack, parse, history) scores = self.model.score(features) valid_moves = self.get_valid_moves(idx, n, stack, parse.heads) guess = max(valid_moves, key=lambda move: scores[move]) history.append(guess) if gold_heads is not None: gold_moves = self.get_gold_moves(idx, n, stack, parse.heads, gold_heads) # gold_moves is empty iif the tree is not projective assert gold_moves best = max(gold_moves, key=lambda move: scores[move]) else: best = None yield guess, best, features, parse idx = self.transition(guess, idx, stack, parse) @abc.abstractmethod def transition(self, move, i, stack, parse): """ Make the action `move` by modifying the buffer (i.e. the position in the sentence `i`), the `stack` and updating the (partial) dependency tree accordingly. """ pass @abc.abstractmethod def get_valid_moves(self, i, n, stack, heads): """ Returns the authorized moves """ pass @abc.abstractmethod def get_gold_moves(self, n0, n, stack, heads, gold): pass
import pandas as pd df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', headers=None) df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) df.tail() import matplotlib.pyplot as plt import numpy as np y = df.iloc[0:100, 4].values y = np.where( y== 'Iris-setosa', -1,1) y[0] X = df.iloc[0:100, [0,2]].values plt.scatter(X[:50, 0], X[:50,1], color = 'red', marker = 'o', label='setosa') plt.scatter(X[50:100, 0], X[50:100,1], color = 'blue', marker = 'x', label='versicolor') plt.xlabel('sepal length') plt.ylabel('petal length') plt.legend(loc='upper left') plt.show() import Percepton import Perceptron ppn = Perceptron(eta=0.1, n_iter=10) ppn = Perceptron.Perceptron(eta=0.1, n_iter=10) ppn.fit(X,y) plt.plot(range(1, len(ppn.errors_)+1),ppn.errors_,marker='o') plt.xlabel('Epochs') plt.ylabel('Number of Misclassifications') plt.show() import readline readline.write_history_file("PerceptronTrain.py")
tagm1 = Tag_Displacement_Feature_Template(-1) tag0 = Tag_Displacement_Feature_Template(-0) tag1 = Tag_Displacement_Feature_Template(1) tag2 = Tag_Displacement_Feature_Template(2) suffix2 = Suffix_Feature_Template(2) suffix3 = Suffix_Feature_Template(3) suffix4 = Suffix_Feature_Template(4) suffix5 = Suffix_Feature_Template(5) #Load them all into a feature set feature_set = Feature_Set([bigram]) #Create the iterators and writer keyfile = MyKeyFileIterator(file1) devfile = MyDevFileIterator(file2) devwriter = DevFileWriter("eval_tester") #Create the scheme gen = NGramHistoryGenerator(2) enc = NGramHistoryEncoder(2) dec = NGramHistoryDecoder(2) scheme = NGramHistoryScheme(gen,enc,dec) #Create the percepton and train. p = Perceptron(scheme,feature_set) p.train(5,keyfile) p.save("2G.model") p.load("2G.model") p.test(devfile,devwriter)
for perceptron in perceptron_list: if perceptron.current_output > max_value: max_value = perceptron.current_output lang = perceptron.name return lang if __name__ == '__main__': os.chdir('training') directories = os.listdir('.') perceptrons = [] alpha = 0.5 error = 3 flag = True for d in directories: perceptrons.append(Perceptron(d, alpha)) print( '================================== Training ==================================' ) while flag: # for i in range(10): # print('iteration {}'.format(i)) for d in reversed(directories): # print(d) files_list = os.listdir(os.path.abspath(d)) os.chdir(d) error_sum = 0 for f in files_list: # print('Directory {} File {}'.format(d, f)) for p in perceptrons: p.process(f)
#!/usr/bin/python3 # -*-encoding:utf8-*- import time import Dataset import Perceptron import CrossValScore acc = [] dt = Dataset.Dataset('hill.csv') inicio = time.time() model = Perceptron.Perceptron(epochs=268, threshold=-0.6, activation='deg') accuracy = CrossValScore.cross_val_score(model, dt, 10).mean() * 100 fim = time.time() acc.append((accuracy, 'deg', fim - inicio)) print(fim - inicio) incio = time.time() model = Perceptron.Perceptron(epochs=224, threshold=-1.8, activation='sig') accuracy = CrossValScore.cross_val_score(model, dt, 10).mean() * 100 fim = time.time() acc.append((accuracy, 'sig', fim - inicio)) print(fim - inicio) inicio = time.time() model = Perceptron.Perceptron(epochs=100, threshold=-2.0, activation='relu') accuracy = CrossValScore.cross_val_score(model, dt, 10).mean() * 100 fim = time.time() acc.append((accuracy, 'relu', fim - inicio)) print(fim - inicio)
#!/usr/bin/python3 # -*-encoding:utf8-*- import time import Dataset import Perceptron import CrossValScore resultados = [] for i in range(300): dt = Dataset.Dataset('hill.csv') model = Perceptron.Perceptron(epochs=i, activation='sig') # value = CrossValScore.cross_val_score(model, dt, 10).mean()*100 dt, test = dt.split_dados(0.8) inicio = time.time() model.train(dt) value = model.predict(test) * 100 fim = time.time() tempo = fim - inicio resultados.append((i, value, tempo)) print(">> Concluidos: ", i) with open('resultados_epochs_sig.txt', 'w') as f: f.write( "Testes de variação de epocas de treino para a rede neural com a base de dados Hill.\nFormato (Qt. epocas, Accuracy, Tempo de execução)\n" ) for linha in resultados: f.write(str(linha) + '\n')
for j in range(dimensions): pointsum += w_init[j + 1] * pointslist[j][i] if pointsum >= 0: pointlabel.append(1) temp += 1 else: pointlabel.append(0) print(temp) randarray = [] for i in range(points): randarray.append(i) if temp >= 250 and temp <= 750: f = open("histogram_data.txt", "w") for _ in range(100): random.shuffle(randarray) temppointlabel = [] temppointslist = [] for i in range(dimensions): temppointslist.append([]) for i in range(points): temppointlabel.append(pointlabel[randarray[i]]) for j in range(dimensions): temppointslist[j].append(pointslist[j][randarray[i]]) p = Perceptron(lr=0.5, max_iterations=10000) p = p.fit(temppointslist, temppointlabel, max) print(p.updates) f.write(str(p.updates) + "\n") f.close()
# -- Read data if len(sys.argv) < 6: print("Usage:", sys.argv[0], " csv_file ppm_file epsilon alpha lambda") sys.exit(1) # end if e = float(sys.argv[3]) a = float(sys.argv[4]) l = float(sys.argv[5]) cost = BinaryLabelingCost.ReadFromFile(sys.argv[1], ActivationFunctions.LogisticSigmoid(), e) # -- Train weights and bias w0 = cost.W0("random") b0 = cost.B0("random") print("-- Gradient descent --") print("Starting parameters:", w0, b0) [w, b, J, n_iter] = cost.gradient_descent(w0, b0, a, l) print("w =", w) print("b =", b) print("J =", J) print("Iterations =", n_iter) p = Perceptron(w, b, ActivationFunctions.LogisticSigmoid()) p.create_example_image(sys.argv[2], cost.X(), cost.Y()) p.confussion_matrix(cost.X(), cost.Y()) ## eof - $RCSfile$