0
0

内積と勾配

Last updated at Posted at 2024-08-26

内積

ベクトルaとベクトルbの内積は、

a・b = (1, 3)・(4, 2) = 1×4 + 3×2 = 10

のように計算する。

このベクトルの成分がDまであるとすると、

a・b = a0×b0 + a1×b1 + … + aD×bD = Σai×bi

iは0からDの値で和記号に直せる。

逆に、和記号Σは内積として計算できる。

Pythonではfor文の和よりも内積として計算した方が処理として高速になる。

import numpy as np 

a = np.ones(1000)
b = np.arange(1, 1001)
print(a.dot(b))
500500.0

勾配

f = w0^2 + 2×w0×w1 + 3の勾配を表示する。
image.png

上記のグラフの傾斜具合を求める際は関数を偏微分する。
このとき求められる式はfのwに関する勾配、または勾配ベクトルと呼ぶ。

実際に偏微分した式を使って勾配を等高線と勾配ベクトルで表示する。

import numpy as np 
import matplotlib.pyplot as plt

def f(w0, w1):          # fの定義
    return w0**2 + 2 * w0 * w1 + 3

def df_dw0(w0, w1):     # fのw0に関する偏微分
    return 2 * w0 + 2 * w1

def df_dw1(w0, w1):     # fのw1に関する偏微分
    return 2 * w0 + 0 * w1

w_range = 2
dw = 0.25
w0 = np.arange(-w_range, w_range + dw, dw)
w1 = np.arange(-w_range, w_range + dw, dw)
wn = w0.shape[0]

ww0, ww1 = np.meshgrid(w0, w1)

ff = np.zeros((len(w0), len(w1)))
dff_dw0 = np.zeros((len(w0), len(w1)))
dff_dw1 = np.zeros((len(w0), len(w1)))

for i0 in range(wn):
    for i1 in range(wn):
        ff[i1, i0] = f(w0[i0], w1[i1])
        dff_dw0[i1, i0] = df_dw0(w0[i0], w1[i1])
        dff_dw1[i1, i0] = df_dw1(w0[i0], w1[i1])

plt.figure(figsize=(9, 4))

# 等高線
plt.subplots_adjust(wspace=0.3)
plt.subplot(1, 2, 1)
cont = plt.contour(ww0, ww1, ff, 10, colors='k')
cont.clabel(fmt='%2.0f', fontsize=8)
plt.xticks(range(-w_range, w_range + 1, 1))
plt.yticks(range(-w_range, w_range + 1, 1))
plt.xlim(-w_range - 0.5, w_range + .5)
plt.ylim(-w_range - .5, w_range + .5)
plt.xlabel('$w_0$', fontsize=14)
plt.ylabel('$w_1$', fontsize=14)

# 勾配ベクトル
plt.subplot(1, 2, 2)
plt.quiver(ww0, ww1, dff_dw0, dff_dw1)
plt.xlabel('$w_0$', fontsize=14)
plt.ylabel('$w_1$', fontsize=14)
plt.xticks(range(-w_range, w_range + 1, 1))
plt.yticks(range(-w_range, w_range + 1, 1))
plt.xlim(-w_range - 0.5, w_range + .5)
plt.ylim(-w_range - .5, w_range + .5)

plt.show()

image.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