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?

log英語.png


深層学習の形式で対数関数のパーセプトロンを考えてみました。


# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from datetime import datetime
import time
import winsound

#開始
start = time.time()

#初期値
#入力層次元数
x_num = 2
#出力層次元数
y_num = 1
#層
layer = [x_num, 2, y_num]
#中間層数
H = len(layer) - 2
#バッチサイズ
batch_size = 100
#学習回数
N = 100000

w = [None for _ in range(H + 1)]
hidden = [None for _ in range(H)]

#乱数
np.random.seed(int(datetime.now().strftime('%H%M%S')))
tf.set_random_seed(int(datetime.now().strftime('%H%M%S')))   

#入力層
x = tf.placeholder(tf.complex64, [None, x_num])
#第1層
w[0] = tf.Variable(tf.truncated_normal([x_num, 2]), tf.float64)
hidden[0] = tf.log(tf.log(tf.matmul(x, tf.cast(w[0], tf.complex64))))
#出力層
w[1] = tf.Variable(tf.zeros([2, y_num]), tf.float64)
p = tf.exp(tf.matmul(hidden[0], tf.cast(w[1], tf.complex64)))

#逆伝搬
t = tf.placeholder(tf.complex64, [None, y_num])
loss = tf.reduce_sum(tf.square(tf.cast(p - t, tf.float64)))
#-tf.reduce_sum(t * tf.log(p))#tf.reduce_sum(tf.square(p - t), name='loss')
train_step = tf.train.AdamOptimizer().minimize(loss)
#train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)

#初期化
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#重み
w_save = [None for _ in range(N + 1)]
w_save[0] = sess.run(w)
print(w_save[0])

#学習
for n in range(N):
    train_x = np.random.uniform(np.e, 10.0, (batch_size, x_num))
    train_x = np.array(train_x, dtype=np.complex)
    train_t = np.log(train_x[:, 1]) / np.log(train_x[:, 0])
    train_t = train_t.reshape(batch_size, 1)
    #train_t = np.array(train_t, dtype=np.float64)
    sess.run(train_step, feed_dict={x:train_x, t:train_t})
    
    w_save[n + 1] = sess.run(w)

#出力
#値
print(w_save[N])
#図
#領域縦
py = np.amax(layer)
#領域横
px = (H + 1) * 2
#領域寸法
plt.figure(figsize = (16, 9))
#図横軸
x = np.arange(0, N + 1, 1) #0からNまで1刻み
#描画
for h in range(H + 1):
    for l in range(layer[h + 1]):
        #領域座標
        plt.subplot(py, px, px * l + h * 2 + 1)
        for m in range(layer[h]):                       
            #線(lとm転置)
            plt.plot(x, np.array([w_save[n][h][m][l] for n in range(N + 1)]), label = "w[" + str(h) + "][" + str(l) + "," + str(m) + "]")        
        #格子線
        plt.grid(True)
        #凡例
        plt.legend(bbox_to_anchor = (1, 1), loc = 'upper left', borderaxespad = 0, fontsize = 10)

#保存
plt.savefig('graph_log_tf.png') 
#図示
plt.show()
#終了#######################################################################
print (time.time() - start)
print(datetime.now().strftime('%Y%m%d%H%M%S'))
winsound.Beep(500,500)
############################################################################

重み

w[0]=
\begin{pmatrix}
△ & □\\
▲ & ■
\end{pmatrix},
w[1]=
\begin{pmatrix}
〇\\
●
\end{pmatrix}\\

入力値とw[0]の積

\begin{pmatrix}
a & b
\end{pmatrix}
\begin{pmatrix}
△ & ▲\\
□ & ■
\end{pmatrix}
=
\begin{pmatrix}
△a+□b & ▲a+■b
\end{pmatrix}

第1層入力

\begin{pmatrix}
log(log(△a+□b) & log(log(▲a+■b)
\end{pmatrix}

第1層出力とw[1]の積

\begin{align}
&\begin{pmatrix}
log(log(△a+□b)) & log(log(▲a+■b))
\end{pmatrix}
\begin{pmatrix}
〇\\
●
\end{pmatrix}\\
 \\

=&〇log(log(△a+□b))+●log(log(▲a+■b))\\
=&log(log(△a+□b))^{〇}-log(log(▲a+■b))^{-●}\\
=&log\frac{(log(△a+□b))^{〇}}{(log(▲a+■b))^{-●}}
\end{align}

出力層入力

\begin{pmatrix}
e^{log\frac{(log(△a+□b))^{〇}}{(log(▲a+■b))^{-●}}}
\end{pmatrix}
=\frac{(log(△a+□b))^{〇}}{(log(▲a+■b))^{-●}}
 \\
\left\{
\begin{array}{l}
△=0,□=1,〇=1 \\
▲=1,■=0,●=-1
\end{array}
\right.
\\
=\frac{logb}{loga}\\
=log_ab
最も簡単な場合、上記条件を満たせばlog_abを出力することができます。
初期値を乱数で決めてから学習を繰り返すと目標値に収束するか試してみました。

graph_log_tf.png


目標値\\
w[0]=
\begin{pmatrix}
1 & 0\\
0 & 1
\end{pmatrix}
,w[1]=
\begin{pmatrix}
1\\
-1
\end{pmatrix}\\
初期値\\
w[0]=
\begin{pmatrix}
1.655222 & -0.03430884\\
0.36429077 & 0.75053316
\end{pmatrix}
,w[1]=
\begin{pmatrix}
0.0\\
0.0
\end{pmatrix}\\
計算値\\
w[0]=
\begin{pmatrix}
1.0000112e+00 & -4.5090604e-07\\
1.8058329e-07 & 1.0000116e+00
\end{pmatrix}
,w[1]=
\begin{pmatrix}
-1.0000067\\
1.0000072
\end{pmatrix}
自作で組んだpythonのコードではオーバーフローばかり起してうまくいきませんでしたが
tensorflowを導入したら、あっさり成功しました。
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?