sigmoid.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # f(x) = 1/(1+e(-z))
  4. def sigmoid(x, min_value=0.3, max_value=5, right_offset=5, span=10):
  5. """
  6. min_value: y最小值
  7. max_value: y最大值
  8. right_offset: 曲线右移
  9. span: y从接近最小到接近最大时,自变量x的跨度
  10. """
  11. # 右移
  12. x -= right_offset
  13. # 缩放
  14. x *= 10 / span
  15. # 振幅
  16. return (max_value - min_value) / (1 + np.exp(-x)) + min_value
  17. def test():
  18. plt.rcParams["font.family"] = "SimHei"
  19. plt.rcParams["axes.unicode_minus"] = False
  20. plt.rcParams["font.size"] = 12
  21. z = np.linspace(-10, 10, 200)
  22. plt.plot(z, sigmoid(z))
  23. plt.axvline(x=0, ls="--", c="k")
  24. plt.axhline(ls=":", c="k")
  25. plt.axhline(y=0.5, ls=":", c="k")
  26. plt.axhline(y=1, ls=":", c="k")
  27. plt.xlabel("z值")
  28. plt.ylabel("sigmoid(z)值")
  29. if __name__ == "__main__":
  30. test()
  31. # for i in range(20):
  32. # x = i + 1
  33. # print(x, sigmoid(x, span=10))
  34. # cd grpc
  35. # mkdir -p cmake/build
  36. # pushd cmake/build
  37. # cmake -DgRPC_INSTALL=ON \
  38. # -DgRPC_BUILD_TESTS=OFF \
  39. # ../..
  40. # make -j 4
  41. # make install
  42. # popd