Keksike

2 лаба Нейроинформатика

Mar 15th, 2022
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | None | 0 0
  1. import random
  2.  
  3. def sum_weight(j, list_digital, list_weight, pomehi):
  4.   sum = 0
  5.   for j in range(9):
  6.     sum += list_digital[j] * list_weight[j]
  7.    
  8.   for j in range(pomehi):
  9.     sum += random.choice(list_digital) * random.choice(list_weight)
  10.   return sum
  11.  
  12. def nakazanie(list, list_digital): #функция для "наказания" - уменьшения определенных элементов списка "вес" на 1
  13.   j = 0
  14. #  print(list, "- до наказания")
  15.   for j in range(9):
  16.     if list_digital[j] == 1:
  17.       list[j] -= 1
  18.   return list
  19.  
  20. def pooshchreniye(list, list_digital): #функция для "поощерения" - увелечения определенных элементов списка "вес" на 1
  21.   j = 0
  22. #  print(list, "- до поощерения")
  23.   for j in range(9):
  24.     if list_digital[j] == 1:
  25.       list[j] += 1
  26.   return list
  27.  
  28. def proverka(sum, R_const, current_number, number):
  29.   if current_number == number:
  30.     if sum < R_const:
  31.       return 1
  32.     else:
  33.       return 0
  34.   else:
  35.     if sum >= R_const:
  36.       return 2
  37.     else:
  38.       return 0
  39.  
  40. def print_1(sum, R_const, n, i):
  41.   print("SUM",sum, "<",R_const, "- поощеряем(цифра -", n,",номер -", i+1, "в последовательности)")
  42.   return 0
  43.  
  44. def print_2(sum, R_const, n, i):
  45.   print("SUM", sum, ">=",R_const, "- наказываем(цифра -", n,",номер -", i+1, "в последовательности)")
  46.   return 0
  47.  
  48. d0=[1,1,0,1,0,1,0,1,1] #признак цифры 0
  49. d1=[0,0,1,1,0,0,0,1,0] #признак цифры 1
  50. d2=[0,1,0,1,0,0,1,0,1] #признак цифры 2
  51. d3=[0,1,1,0,1,0,1,0,0] #признак цифры 3
  52. d4=[1,0,0,1,1,0,0,1,0] #признак цифры 4
  53. d5=[1,1,0,0,1,0,0,1,1] #признак цифры 5
  54. d6=[0,0,1,0,1,1,0,1,1] #признак цифры 6
  55. d7=[0,1,1,0,0,1,0,0,0] #признак цифры 7
  56. d8=[1,1,0,1,1,1,0,1,1] #признак цифры 8
  57. d9=[1,1,0,1,1,0,1,0,0] #признак цифры 9
  58.  
  59. weight = [7,4,3,5,6,1,3,2,9]
  60. weight_old = [7,4,3,5,6,1,3,2,9]
  61.  
  62. num_posl = [6, 4, 3, 0, 5, 2, 9, 2, 4, 2, 3, 2, 3, 8, 4, 9, 4, 9, 8, 2, 6, 2, 0, 9, 6, 6, 9, 4, 2, 9, 7, 5, 7, 2, 4, 5, 9, 8, 5, 2, 2, 8, 8, 7, 3, 8, 1, 3, 6, 7, 4, 2, 2, 0, 4, 9, 0, 4, 9, 6, 7, 8, 4, 5, 9, 9, 6, 6, 9, 0, 9, 1, 9, 4, 7, 3, 0, 2, 8, 8, 8, 2, 3, 1, 5, 8, 0, 5, 9, 1, 9, 4, 6, 5, 8, 6, 0, 5, 4, 7, 9, 6, 9, 4, 1, 6, 1, 6, 4, 0, 4, 7, 8, 6, 1, 9, 2, 8, 6, 4, 7, 9, 5, 4, 2, 5, 9, 5, 5, 4, 2, 2, 3, 5, 7, 0, 4, 4, 1, 3, 1, 1, 6, 2, 3, 0, 7, 9, 3, 6, 1, 6, 6, 6, 6, 8, 8, 6, 1, 1, 9, 8, 1, 1, 3, 5, 2, 4, 1, 6, 5, 0, 5, 5, 0, 8, 3, 1, 2, 8, 3, 5, 6, 0, 5, 7, 4, 6, 9, 3, 8, 1, 8, 0, 9, 3, 3, 0, 0, 7, 1, 3, 3, 7, 8, 1, 3, 3, 3, 7, 3, 8, 0, 5, 6, 6, 5, 0, 8, 9, 1, 6, 7, 0, 5, 7, 0, 6, 0, 9, 8, 1, 4, 5, 2, 6, 8, 5, 7, 3, 5, 9, 3, 1, 8, 1, 9, 1, 9, 2, 7, 9, 2, 1, 3, 7, 8, 4, 7, 8, 1, 2, 3, 4, 8, 4, 3, 7, 9, 9, 5, 5, 6, 9, 0, 3, 0, 3, 0, 6, 0, 0, 9, 2, 1, 8, 7, 6, 7, 1, 1, 2, 2, 0, 1, 3, 2, 2, 3, 8, 1, 6, 2, 1, 6, 2, 4, 3, 5, 4, 5, 5, 5, 0, 1, 9, 2, 8, 8, 5, 4, 7, 9, 5, 8, 0, 3, 5, 5, 0, 5, 6, 3, 4, 6, 7, 2, 3, 2, 2, 6, 9, 5, 0, 7, 9, 4, 5, 7, 3, 3, 7, 7, 8, 4, 1, 3, 8, 7, 0, 9, 2, 1, 7, 4, 0, 2, 3, 5, 4, 5, 9, 6, 7, 8, 6, 9, 1, 0, 3, 6, 8, 8, 2, 2, 5, 7, 4, 6, 2, 7, 4, 1, 7, 3, 1, 3, 4, 9, 4, 3, 9, 8, 0, 5, 4, 4, 9, 4, 9, 6, 5, 3, 4, 6, 1, 4, 7, 0, 8, 7, 8, 3, 2, 0, 5, 9, 7, 6, 0, 4, 2, 1, 0, 2, 9, 1, 0, 4, 8, 5, 1, 0, 3, 9, 1, 1, 5, 0, 7, 8, 2, 0, 7, 6, 7, 5, 0, 7, 1, 6, 0, 1, 8, 7, 4, 2, 1, 5, 0, 4, 9, 1, 7, 5, 8, 7, 8, 3, 3, 0, 6, 6, 7, 7, 4, 7, 8, 2, 0, 3, 2, 1, 6, 0, 4, 2, 8, 7, 2]
  63.  
  64. i = 0
  65. R_const = 27
  66.  
  67. print("Введите количество помех для персептрона")
  68. pomehi = int(input())
  69.  
  70. print("Введите цифру, под которую будете обучать персептрон")
  71. number = int(input())
  72.  
  73. for i in range(len(num_posl)):
  74.   if num_posl[i] == 0: #определяем 0
  75.     sum = sum_weight(0, d0, weight, pomehi)
  76.     res = proverka(sum, R_const, 0, number)
  77.     if res == 1:
  78.       weight = pooshchreniye(weight, d0)
  79.       print_1(sum,R_const, num_posl[i], i)
  80.     elif res == 2:
  81.       weight = nakazanie(weight, d0)
  82.       print_2(sum, R_const, num_posl[i], i)
  83.      
  84.   elif num_posl[i] == 1: #определяем 1
  85.     sum = sum_weight(0, d1, weight, pomehi)
  86.     res = proverka(sum, R_const, 1, number)
  87.     if res == 1:
  88.       weight = pooshchreniye(weight, d1)
  89.       print_1(sum,R_const, num_posl[i], i)
  90.     elif res == 2:
  91.       weight = nakazanie(weight, d1)
  92.       print_2(sum, R_const, num_posl[i], i)
  93.      
  94.   elif num_posl[i] == 2: #определяем 2
  95.     sum = sum_weight(0, d2, weight, pomehi)
  96.     res = proverka(sum, R_const, 2, number)
  97.     if res == 1:
  98.       weight = pooshchreniye(weight, d2)
  99.       print_1(sum,R_const, num_posl[i], i)
  100.     elif res == 2:
  101.       weight = nakazanie(weight, d2)
  102.       print_2(sum, R_const, num_posl[i], i)
  103.      
  104.   elif num_posl[i] == 3: #определяем 3
  105.     sum = sum_weight(0, d3, weight, pomehi)
  106.     res = proverka(sum, R_const, 3, number)
  107.     if res == 1:
  108.       weight = pooshchreniye(weight, d3)
  109.       print_1(sum,R_const, num_posl[i], i)
  110.     elif res == 2:
  111.       weight = nakazanie(weight, d3)
  112.       print_2(sum, R_const, num_posl[i], i)
  113.      
  114.   elif num_posl[i] == 4: #определяем 4
  115.     sum = sum_weight(0, d4, weight, pomehi)
  116.     res = proverka(sum, R_const, 4, number)
  117.     if res == 1:
  118.       weight = pooshchreniye(weight, d4)
  119.       print_1(sum,R_const, num_posl[i], i)
  120.     elif res == 2:
  121.       weight = nakazanie(weight, d4)
  122.       print_2(sum, R_const, num_posl[i], i)
  123.      
  124.   elif num_posl[i] == 5: #определяем 5
  125.     sum = sum_weight(0, d5, weight, pomehi)
  126.     res = proverka(sum, R_const, 5, number)
  127.     if res == 1:
  128.       weight = pooshchreniye(weight, d5)
  129.       print_1(sum,R_const, num_posl[i], i)
  130.     elif res == 2:
  131.       weight = nakazanie(weight, d5)
  132.       print_2(sum, R_const, num_posl[i], i)
  133.      
  134.   elif num_posl[i] == 6: #определяем 6
  135.     sum = sum_weight(0, d6, weight, pomehi)
  136.     res = proverka(sum, R_const, 6, number)
  137.     if res == 1:
  138.       weight = pooshchreniye(weight, d6)
  139.       print_1(sum,R_const, num_posl[i], i)
  140.     elif res == 2:
  141.       weight = nakazanie(weight, d6)
  142.       print_2(sum, R_const, num_posl[i], i)
  143.      
  144.   elif num_posl[i] == 7: #определяем 7
  145.     sum = sum_weight(0, d7, weight, pomehi)
  146.     res = proverka(sum, R_const, 7, number)
  147.     if res == 1:
  148.       weight = pooshchreniye(weight, d7)
  149.       print_1(sum,R_const, num_posl[i], i)
  150.     elif res == 2:
  151.       weight = nakazanie(weight, d7)
  152.       print_2(sum, R_const, num_posl[i], i)
  153.      
  154.   elif num_posl[i] == 8: #определяем 8
  155.     sum = sum_weight(0, d8, weight, pomehi)
  156.     res = proverka(sum, R_const, 8, number)
  157.     if res == 1:
  158.       weight = pooshchreniye(weight, d8)
  159.       print_1(sum,R_const, num_posl[i], i)
  160.     elif res == 2:
  161.       weight = nakazanie(weight, d8)
  162.       print_2(sum, R_const, num_posl[i], i)
  163.      
  164.   elif num_posl[i] == 9: #определяем 9
  165.     sum = sum_weight(0, d9, weight, pomehi)
  166.     res = proverka(sum, R_const, 9, number)
  167.     if res == 1:
  168.       weight = pooshchreniye(weight, d9)
  169.       print_1(sum,R_const, num_posl[i], i)
  170.     elif res == 2:
  171.       weight = nakazanie(weight, d9)
  172.       print_2(sum, R_const, num_posl[i], i)
  173.  
  174.   #print(sum, " - сумма после", i, "итераций, когда на вход персептрона пришла", num_posl[i])
  175.   #print("Вес после", i, " итерации - ", weight)
  176.  
  177. print('\n',weight_old, "Начальный вес\n", weight, " - Итоговый вес после 500 итераций")
Advertisement
Add Comment
Please, Sign In to add comment