sigmoid_test.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from fpu_test_gen import reverse_endian, dtype_size
  4. def generate(fname, samples, dtype=np.float32):
  5. dsize = dtype_size(dtype)
  6. numbers = np.linspace(-4, 4, samples, dtype=dtype)
  7. data = numbers.tobytes()
  8. with open(fname, 'w') as f:
  9. for i in range(samples):
  10. f.write(f"{reverse_endian(data[i*dsize:i*dsize+dsize]).hex()} // {numbers[i]:0.6f}\n")
  11. def view_result(fname, dtype=np.float32):
  12. x_bytes = b''
  13. y_bytes = b''
  14. timing = []
  15. with open(fname, 'r') as f:
  16. for line in f.readlines():
  17. parts = line.split()
  18. x_bytes += reverse_endian(bytes.fromhex(parts[0]))
  19. y_bytes += reverse_endian(bytes.fromhex(parts[1]))
  20. timing.append(int(parts[2]))
  21. x = np.frombuffer(x_bytes, dtype=dtype)
  22. y = np.frombuffer(y_bytes, dtype=dtype)
  23. t = np.array(timing) / 10
  24. fig, ax = plt.subplots()
  25. plt.title('Digital circuit sigmoid function test')
  26. ax2 = ax.twinx()
  27. ax.plot(x, y, '.', markersize=0.95)
  28. ax2.plot(x, t, '.', markersize=0.95, color='m')
  29. ax.set_xlabel('Function input')
  30. ax.set_ylabel('Function output')
  31. ax2.set_ylabel('Timing in cycles', color='m')
  32. plt.grid()
  33. plt.show()
  34. if __name__ == '__main__':
  35. # generate('sigmoid_test.hex', 5001)
  36. view_result('sigmoid_result.hex')