0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

シグモイド関数をmatplotlibで描画してみた

Posted at

概要

今日はディープラーニングの勉強の手始めということで、シグモイド関数をmatplotlibで描画してみました。
以下のページを参考にしました。
https://zero2one.jp/learningblog/deep-learning-activation-function/

基本形

シグモイド関数の基本形を描画してみました。ソースコードは上記のページを参考にしました。

simoid.py
import numpy as np
import matplotlib.pyplot as plt 

#sigmoid関数を定義
def sigmoid(x):
    return 1/(1+np.exp(-x))

x = np.linspace(-8,8,1000)
y = sigmoid(x)

#sigmoid関数を描画
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.grid()
plt.show()

実行すると以下のようなウィンドウが表示されました。
Screenshot from 2023-05-03 13-00-38.png

ゲインを変更

シグモイド関数のゲインを変更してみました。ソースコードは上記のページを参考にしました。

sigmoid2.py
import numpy as np
import matplotlib.pyplot as plt 

#一般sigmoid関数を定義
def sigmoid(x, a):
    return 1/(1+np.exp(-a*x))

#x軸
x = np.linspace(-8,8,1000)

#aの値(0.1から1まで約0.1間隔で変化させる)
a = np.linspace(0.1, 1.0, 10)

#描画
fig = plt.figure(figsize = (12, 12))
for i in range(len(a)):
    text = "a = "+str(a[i])
    ax = fig.add_subplot(5, 5, i+1, title = text)
    ax.plot(x, sigmoid(x, a[i]))

fig.subplots_adjust(wspace=0.5, hspace=0.5)
plt.show()

実行すると以下のようなウィンドウが表示されました。
Screenshot from 2023-05-03 13-02-34.png

微分

シグモイド関数を微分した結果を表示してみました。上記のページを参考にしました。

sigmoid3.py
import numpy as np
import matplotlib.pyplot as plt 

#微分後のsigmoid関数を定義
def grad_sigmoid(x):
    return np.exp(-x)/(1+np.exp(-x))**2

x = np.linspace(-5,5,100)
y = grad_sigmoid(x)

#描画
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.grid()
plt.show()

実行すると以下のようなウィンドウが表示されました。
Screenshot from 2023-05-03 13-04-34.png

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?