Advertisement
here2share

# calc_pi_by_nth_digit_pattern_recog_test_four.py

Mar 29th, 2023
724
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. # calc_pi_by_nth_digit_pattern_recog_test_four.py
  2.  
  3. # division
  4. # written by Kirk Lawrence
  5.  
  6. import pdb
  7. import math
  8. import random
  9. import os, sys
  10. import ast
  11. from itertools import combinations
  12.  
  13. try:
  14.     # Python2
  15.     from Tkinter import *
  16.     from urllib2 import urlopen
  17. except ImportError:
  18.     # Python3
  19.     from tkinter import *
  20.     from urllib.request import urlopen
  21.    
  22. def print_each_at_x(vars_list, nnn=22):
  23.     sss = ''
  24.     for var in vars_list:
  25.         n = nnn
  26.         if isinstance(var, list):
  27.             var = ' '.join([str(v) for v in var])
  28.         else:
  29.             var = str(var)
  30.         if len(var) > n - 4:
  31.             n *= 2
  32.         var = (var + ' ' * n)[:n-1] + ' '
  33.         sss += var
  34.     print(sss.rstrip())   # to print a new line at the end
  35.  
  36. okay  = 1
  37.  
  38. root = Tk()
  39. root.withdraw()
  40.  
  41. def cpbd():
  42.     try:
  43.         # t = root.selection_get(selection="CLIPBOARD")
  44.         t = root.clipboard_get()
  45.         return t
  46.     except:
  47.         return []
  48.     # root.clipboard_clear()
  49.     # root.destroy()
  50. 0
  51.    
  52. def gym(): # for that pseudo neural exercise
  53.     if prediction != yn:
  54.         ttt = []
  55.         t = 0
  56.         if yn:
  57.             incr = +1
  58.         else:
  59.             incr = -1
  60.         for node in nodes:
  61.             if weights[node] == '.':
  62.                 ttt += [node]
  63.             else:
  64.                 t += weights[node]
  65.                 weights[node] += incr
  66.                
  67.         if ttt:
  68.             t = ((20 - abs(t)) // len(ttt)) * incr
  69.             for node in ttt:
  70.                 weights[node] = t
  71. 0
  72.  
  73. def guess(data): # guess before training for this data
  74.     ttt = []
  75.     for z in enumerate(data):
  76.         s = '%d:%s'%z
  77.         ttt.append(s)
  78.    
  79.     nodes.extend(list(combinations(ttt, 3)))
  80.     # print (len(nodes))
  81.        
  82.     ttt = 0
  83.     for node in nodes:
  84.         try:
  85.             ttt += weights[node]
  86.         except:
  87.             weights[node] = '.'
  88.                        
  89.     p = 0
  90.     if ttt > -1:
  91.         p = 1
  92.    
  93.     return p, ttt
  94. 0
  95.  
  96. ###
  97.  
  98. summary = 1
  99.  
  100. # import numpy as np ### very much recommended 2018
  101.  
  102. # 5000 places of pi
  103. pi = '''31415926535897932...'''.strip()
  104. Lpi = len(pi)
  105.  
  106. print ("Gathering Data, Please Wait...")
  107.  
  108. right_answers = []
  109. wrong_answers = []
  110. for i in range(Lpi):
  111.     right_answers += [(1, str(i) + pi[i])]
  112.     wrong_answers += [(0, str(pi[i]))]
  113.    
  114. const_wrong_answers = {}
  115. for i in range(10):
  116.     const_wrong_answers[str(i)] = []
  117.     for j in range(1,10):
  118.         const_wrong_answers[str(i)] += [str((i + j) % 10)]
  119.  
  120. print ('Pattern Recognition Sequence Is Ready To Commence...')
  121.  
  122. if 1: # for testing
  123.  
  124.     random.seed(0)
  125.    
  126.     rush = 5000
  127.    
  128.     const_zzz = (right_answers + wrong_answers) * 10
  129.    
  130.     right = 0
  131.     wrong = 0
  132.            
  133.     percent = '0.0'
  134.    
  135.     gain = 0.0
  136.     prev_gain = gain
  137.    
  138.     weights = {}
  139.     weight_errors = {}
  140.        
  141.     bias = {}
  142.    
  143.     zzz = const_zzz[:]
  144.     random.shuffle(zzz)
  145.    
  146.     final = 0
  147.    
  148.     end = len(zzz)
  149.     count_to_end = 0
  150.    
  151.     go = 0
  152.     while zzz:
  153.         z = zzz.pop(0)
  154.        
  155.         yn, data = z
  156.         if not yn:
  157.             str_digit = const_wrong_answers[data].pop(0)
  158.             const_wrong_answers[data].append(str_digit)
  159.             data += str_digit
  160.        
  161.         data = data.zfill(5)
  162.                
  163.         nodes = []
  164.        
  165.         prediction, ppp = guess(data)
  166.        
  167.         gym()
  168.                    
  169.         if prediction == yn:
  170.             right += 1
  171.             count_to_end += 1
  172.         else:
  173.             wrong += 1
  174.             count_to_end = 0
  175.         if not (go+1)%rush:
  176.             if okay: ### for other tests
  177.                
  178.                 percent = '{:.1f}'.format((100.0/(right+wrong))*right)
  179.                 gain = max(gain,float(percent))
  180.                
  181.                 t = str(go+1).zfill(8)
  182.                 print ('.')
  183.                 print_each_at_x([['. >>> actual vs prediction =', [yn, prediction], ppp] ,['::: Remaining', max(0,end-count_to_end)], final])
  184.                 print_each_at_x([['.',data , ':::','test#',t], ['::: wrong =',wrong], ['::: right =',right], [':::','{:.1f}'.format(gain)+' / '+percent+'%']])
  185.                 print ('.')
  186.                
  187.                 right = 0
  188.                 wrong = 0
  189.            
  190.             if count_to_end >= end or int(t) > 500000:
  191.                 break                  
  192.        
  193.         zzz.append(z)
  194.                
  195.         go += 1
  196.            
  197.  
  198. print ('.','*'*10,'End Of Cycle','*'*10)
  199. print ('.')
  200.  
  201. '''
  202. if summary:
  203.     ttt = list(weights.items())
  204.     ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
  205.     for t in ttt[:1000]:
  206.         print ('.',t)
  207.     print ('.')
  208.     for t in ttt[-1000:]:
  209.         print ('.',t)
  210.     print ('.')
  211.     print ('.',len(ttt))
  212. '''
  213.  
  214. def calc_pi_digit(num):
  215.     for pi_digit in range(10):
  216.         del nodes[:]
  217.         data = str(num) + str(pi_digit)
  218.         data = data.zfill(5)
  219.         prediction, ppp = guess(data)
  220.         if prediction:
  221.             break
  222.     return pi_digit
  223.  
  224. for n in range(Lpi):
  225.     pi_digit = calc_pi_digit(n)
  226.     wrong = ''
  227.     sss = f"... the {n}th digit of pi is {pi[n]}"
  228.     if pi[n] == str(pi_digit):
  229.         s = "CORRECT !!!"
  230.     else:
  231.         s = "\t*** Wrong"
  232.         wrong = f", not {pi_digit}"
  233.     print(s + sss + wrong)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement