Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_pattern_recog_X.py
- # written by Kirk Lawrence + https://pastebin.com/pqyNaFQi
- import math
- import random
- import os, sys
- import ast
- from itertools import combinations
- try:
- # Python2
- from Tkinter import *
- from urllib2 import urlopen
- except ImportError:
- # Python3
- from tkinter import *
- from urllib.request import urlopen
- okay = 1
- root = Tk()
- root.withdraw()
- def cpbd():
- try:
- # t = root.selection_get(selection="CLIPBOARD")
- t = root.clipboard_get()
- return t
- except:
- return []
- # root.clipboard_clear()
- # root.destroy()
- 0
- def gym(): # for that pseudo neural exercise
- if prediction != yn:
- if yn:
- for v in nodes:
- if v not in lock:
- if weights[v] == 0:
- weights[v] = 99
- elif weights[v] == 20:
- weights[v] = -50
- elif weights[v] < 99:
- weights[v] += 1
- else:
- for v in nodes:
- if v not in lock:
- if weights[v] == 0:
- weights[v] = -99
- elif weights[v] == -20:
- weights[v] = 50
- elif weights[v] < -99:
- weights[v] -= 1
- elif gain > 99 and float(percent) > 98:
- for v in nodes:
- if abs(weights[v]) > 95:
- lock[v] = 1
- 0
- def guess(): # guess before training for this data
- ttt = []
- for z in enumerate(data):
- s = '%d:%s'%z
- ttt.append(s)
- nodes.extend(list(combinations(ttt, 5))) ### length @ 5 = 792
- # print len(nodes)
- ppp = 0
- for v in nodes:
- if v not in weights:
- weights[v] = 0
- else:
- ppp += weights[v]
- p = 0
- if ppp > -1:
- p = 1
- return p, ppp
- 0
- ###
- summary = 1
- rush = 1000
- # import numpy as np ### very much recommended 2018
- print ('Pattern Recognition Sequence Is Ready To Commence...')
- if 1: # for testing
- # random.seed(0)
- right = 0
- wrong = 0
- prev_gain = 0
- percent = '0.0'
- gain = 0.0
- lock = {}
- weights = {}
- rejected = {}
- go = 0
- while not go:
- full = cpbd()
- if '# yn -- combos -- at' in full:
- full = full.split('# yn -- combos -- at')[-1].strip().splitlines()
- if len(full) > 50000:
- random.shuffle(full)
- full = full[:70000]
- zzz = full*2
- phase = '::: Initial Phase'
- else:
- full = []
- while full:
- while zzz:
- z = zzz.pop(0)
- yn, data, at = ast.literal_eval(z)
- data = ''.join(data)
- at = ''.join(at)
- nodes = []
- prediction, ppp = guess()
- gym()
- if prediction == yn:
- right += 1
- else:
- wrong += 1
- if float(percent):
- zzz.append(z)
- rejected[data] = yn, ppp
- try:
- full.remove(z)
- except:
- 0
- if not (go+1)%rush:
- percent = '{:.1f}'.format(((right-prev_gain)*100.0)/rush)
- gain = max(gain,float(percent))
- if okay: ### for other tests
- z = str(go+1).zfill(8)
- print ('.')
- print ('.','>>> actual vs prediction =',[yn, prediction],ppp,' '*(11-len(str(ppp))),'::: remaining',len(zzz),phase)
- print ('.',data,':::','test#',z,'::: wrong =',wrong,'::: right =',right,':::','{:.1f}'.format(gain)+' / '+percent+'%')
- print ('.',at)
- print ('.')
- prev_gain = right
- if len(zzz) < rush:
- zzz = []
- break
- go += 1
- if 'F' in phase:
- break
- phase = '::: Final Phase *****'
- lock = {}
- right = 0
- wrong = 0
- prev_gain = 0
- gain = 0.0
- full *= 3
- zzz = full[:]
- print ('.','*'*10,'End Of Cycle','*'*10)
- print ('.')
- print ('Rejected...')
- for v in rejected[:500]:
- print ('.',v,rejected[v])
- print ('.')
- if summary:
- ttt = list(weights.items())
- ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
- for t in ttt[:1000]:
- print ('.',t)
- print ('.')
- for t in ttt[-1000:]:
- print ('.',t)
- print ('.')
- print ('.',len(ttt))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement