neuron_net_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from fpu_test_gen import reverse_endian, dtype_size
  4. WEIGTHS = [
  5. [
  6. [0.7095146, -0.41895103, -0.08075078, -0.5218736],
  7. [-0.4351325, 1.0214638, 0.14494987, -0.78134096],
  8. [0.38553882, -1.0607314, 0.01327306, -0.28972188],
  9. [0.84955347, 0.32464203, 0.8879888, 0.00756884],
  10. [-0.8693629, 0.8418823, 0.60206324, -0.78290594],
  11. [0.1586302, 0.01737848, 0.75329006, -0.57819647],
  12. [-0.16126093, 0.5317601, 0.34316933, -0.7074082],
  13. [0.09219088, -0.624525, -0.61903083, -0.87057704]
  14. ],
  15. [
  16. [0.36770403, -0.78046024, 0.3979908, 0.5494289, -0.13859335, 0.40053025, 0.08249452, -0.32528356],
  17. [-0.17659009, 0.13901198, -0.45248222, -0.7894139, -0.81092286, -0.521815, 0.30632392, -0.3143816],
  18. [-0.04314173, 0.14361085, 0.6259473, 0.3571782, -0.38011226, 0.01378736, 0.05794358, 0.09667788],
  19. [-0.46864474, 0.36618456, -0.45595396, -0.39789405, 0.73964316, -0.30294785, 0.2482118, -0.2127953],
  20. [-0.37941265, 0.45330787, -0.12066315, 0.5636705, 0.68990386, 0.6543718, 0.86367106, -0.5707757],
  21. [-0.78606385, 0.24032554, -0.4472755, -0.24661142, -0.2698564, -0.8365823, -0.13674814, -0.39799848],
  22. [0.11138931, 0.48950365, 0.12998834, 0.4947537, 0.516593, 0.82281274, 0.04789656, 0.30206403],
  23. [0.23097174, 0.30290592, -0.596446, -0.40108407, 0.12246455, -0.47260976, -0.55030185, 0.44481543]
  24. ],
  25. [
  26. [0.5724262, 0.5853241, 0.3748752, -0.892384, -1.0270239, 0.2170913, -0.07271451, 0.14661156],
  27. [0.30391088, -0.92324615, 0.8088594, -1.0522624, 0.07374455, -0.550893, 0.8194236, -0.62796086]
  28. ]
  29. ]
  30. BIAS = [
  31. # L1
  32. [0.01425434, -0.06219335, -0.0201127, -0.04791382, -0.04360008, -0.05311861, -0.01731363, -0.00014839],
  33. # L2
  34. [0.03480967, 0.06208326, -0.01576317, -0.00037753, -0.03940378, 0.05157978, -0.02775403, 0.04540931],
  35. # L3
  36. [0.03787775, -0.03655371],
  37. ]
  38. RESULT = [
  39. 0x00000000, 0x3f800000,
  40. 0x3f800000, 0x00000000,
  41. 0x00000000, 0x00000000,
  42. 0x3f800000, 0x3f800000,
  43. # 0x3f030126, 0x3f800000,
  44. # 0x3e652010, 0x00000000,
  45. # 0x3d2d25da, 0x3efea470,
  46. # 0x3f1a4267, 0x3f06d0f4,
  47. ]
  48. def generate_nn_values(dtype=np.float32):
  49. dsize = dtype_size(dtype)
  50. def sigm(x):
  51. ex = 2.7182818 ** x
  52. return ex / (ex + 1)
  53. def linr(x):
  54. return x
  55. def process(value):
  56. if value > 3 or value < 0:
  57. raise ValueError("Value not in between 0 and 3")
  58. onehot_mat = [
  59. [0, 0, 0, 1],
  60. [0, 0, 1, 0],
  61. [0, 1, 0, 0],
  62. [1, 0, 0, 0],
  63. ]
  64. L0 = onehot_mat[value]
  65. L1_mult = [[L0[j] * WEIGTHS[0][i][j] for j in range(len(L0))] for i in range(8)]
  66. L1 = [linr(sum(L1_mult[i]) + BIAS[0][i]) for i in range(8)]
  67. L2 = [linr(sum([L1[j] * WEIGTHS[1][i][j] for j in range(len(L1))]) + BIAS[1][i]) for i in range(8)]
  68. L3 = [sum([L2[j] * WEIGTHS[2][i][j] for j in range(len(L2))]) + BIAS[2][i] for i in range(2)]
  69. # print(f"L0: {L0} \n\tL1mult: {L1_mult}\n\tL1: {L1} \n\tL2: {L2} \n\tL3: {L3}")
  70. return [sigm(v) for v in L3]
  71. def show_results():
  72. sim_data = np.array([process(0), process(1), process(2), process(3)]).T
  73. hdl_data = np.frombuffer(b''.join([i.to_bytes(4, 'little') for i in RESULT]), dtype=np.float32).reshape(4, 2).T
  74. print(hdl_data)
  75. plt.plot(sim_data[0], sim_data[1], 'x', color='b')
  76. plt.plot(hdl_data[0], hdl_data[1], 'x', color='r')
  77. plt.xlabel('I')
  78. plt.ylabel('Q')
  79. # plt.xlim([0, 1])
  80. # plt.ylim([0, 1])
  81. plt.grid()
  82. plt.show()
  83. if __name__ == '__main__':
  84. show_results()
  85. # generate_nn_values()