Advertisement
here2share

# b_pattern_recog_X.py

Jun 28th, 2022
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # b_pattern_recog_X.py
  2.  
  3. # written by Kirk Lawrence + https://pastebin.com/pqyNaFQi
  4.  
  5. import math
  6. import random
  7. import os, sys
  8. import ast
  9. from itertools import combinations
  10.  
  11. try:
  12.     # Python2
  13.     from Tkinter import *
  14.     from urllib2 import urlopen
  15. except ImportError:
  16.     # Python3
  17.     from tkinter import *
  18.     from urllib.request import urlopen
  19.  
  20. okay  = 1
  21.  
  22. root = Tk()
  23. root.withdraw()
  24.  
  25. def cpbd():
  26.     try:
  27.         # t = root.selection_get(selection="CLIPBOARD")
  28.         t = root.clipboard_get()
  29.         return t
  30.     except:
  31.         return []
  32.     # root.clipboard_clear()
  33.     # root.destroy()
  34. 0
  35.    
  36. def gym(): # for that pseudo neural exercise
  37.     if prediction != yn:
  38.         if yn:
  39.             for v in nodes:
  40.                 if v not in lock:
  41.                     if weights[v] == 0:
  42.                         weights[v] = 99
  43.                     elif weights[v] == 20:
  44.                         weights[v] = -50
  45.                     elif weights[v] < 99:
  46.                         weights[v] += 1
  47.         else:
  48.             for v in nodes:
  49.                 if v not in lock:
  50.                     if weights[v] == 0:
  51.                         weights[v] = -99
  52.                     elif weights[v] == -20:
  53.                         weights[v] = 50
  54.                     elif weights[v] < -99:
  55.                         weights[v] -= 1
  56.                        
  57.     elif gain > 99 and float(percent) > 98:
  58.         for v in nodes:
  59.             if abs(weights[v]) > 95:
  60.                 lock[v] = 1
  61.            
  62. 0
  63. def guess(): # guess before training for this data
  64.     ttt = []
  65.     for z in enumerate(data):
  66.         s = '%d:%s'%z
  67.         ttt.append(s)
  68.    
  69.     nodes.extend(list(combinations(ttt, 5))) ### length @ 5 = 792
  70.     # print len(nodes)
  71.    
  72.     ppp = 0
  73.     for v in nodes:
  74.         if v not in weights:
  75.             weights[v] = 0
  76.         else:
  77.             ppp += weights[v]
  78.            
  79.     p = 0
  80.     if ppp > -1:
  81.         p = 1
  82.     return p, ppp
  83. 0
  84.  
  85. ###
  86.  
  87. summary = 1
  88.  
  89. rush = 1000
  90.  
  91. # import numpy as np ### very much recommended 2018
  92.  
  93. print ('Pattern Recognition Sequence Is Ready To Commence...')
  94.  
  95. if 1: # for testing
  96.  
  97.     # random.seed(0)
  98.  
  99.     right = 0
  100.     wrong = 0
  101.    
  102.     prev_gain = 0
  103.            
  104.     percent = '0.0'
  105.    
  106.     gain = 0.0
  107.    
  108.     lock = {}
  109.            
  110.     weights = {}
  111.    
  112.     rejected = {}
  113.                
  114.     go = 0
  115.    
  116.     while not go:
  117.         full = cpbd()
  118.        
  119.         if '# yn -- combos -- at' in full:
  120.             full = full.split('# yn -- combos -- at')[-1].strip().splitlines()
  121.        
  122.             if len(full) > 50000:
  123.  
  124.                 random.shuffle(full)
  125.                
  126.                 full = full[:70000]
  127.                 zzz = full*2
  128.                
  129.                 phase = '::: Initial Phase'
  130.         else:
  131.             full = []
  132.        
  133.         while full:
  134.             while zzz:
  135.                 z = zzz.pop(0)
  136.                
  137.                 yn, data, at = ast.literal_eval(z)
  138.                
  139.                 data = ''.join(data)
  140.                 at = ''.join(at)
  141.                
  142.                 nodes = []
  143.                
  144.                 prediction, ppp = guess()
  145.                
  146.                 gym()
  147.                    
  148.                 if prediction == yn:
  149.                     right += 1
  150.                 else:
  151.                     wrong += 1
  152.                     if float(percent):
  153.                         zzz.append(z)
  154.                         rejected[data] = yn, ppp
  155.                         try:
  156.                             full.remove(z)
  157.                         except:
  158.                             0
  159.                 if not (go+1)%rush:
  160.                     percent = '{:.1f}'.format(((right-prev_gain)*100.0)/rush)
  161.                     gain = max(gain,float(percent))
  162.                     if okay: ### for other tests
  163.                        
  164.                         z = str(go+1).zfill(8)
  165.                         print ('.')
  166.                         print ('.','>>> actual vs prediction =',[yn, prediction],ppp,' '*(11-len(str(ppp))),'::: remaining',len(zzz),phase)
  167.                         print ('.',data,':::','test#',z,'::: wrong =',wrong,'::: right =',right,':::','{:.1f}'.format(gain)+' / '+percent+'%')
  168.                         print ('.',at)
  169.                         print ('.')
  170.                        
  171.                     prev_gain = right
  172.                    
  173.                     if len(zzz) < rush:
  174.                         zzz = []
  175.                         break
  176.                        
  177.                 go += 1
  178.            
  179.             if 'F' in phase:
  180.                 break
  181.             phase = '::: Final Phase *****'
  182.             lock = {}
  183.  
  184.             right = 0
  185.             wrong = 0
  186.            
  187.             prev_gain = 0
  188.            
  189.             gain = 0.0
  190.            
  191.             full *= 3
  192.             zzz = full[:]
  193.            
  194.  
  195. print ('.','*'*10,'End Of Cycle','*'*10)
  196. print ('.')
  197.  
  198. print ('Rejected...')
  199. for v in rejected[:500]:
  200.     print ('.',v,rejected[v])
  201. print ('.')
  202.        
  203. if summary:
  204.     ttt = list(weights.items())
  205.     ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
  206.     for t in ttt[:1000]:
  207.         print ('.',t)
  208.     print ('.')
  209.     for t in ttt[-1000:]:
  210.         print ('.',t)
  211.     print ('.')
  212.     print ('.',len(ttt))
  213.  
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement