コード例 #1
0
def forward(network, x):

    print("##### 順伝播開始 #####")

    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    # 1層の総入力
    u1 = np.dot(x, W1) + b1

    # 1層の総出力
    z1 = functions.relu(u1)

    # 2層の総入力
    u2 = np.dot(z1, W2) + b2

    # 2層の総出力
    z2 = functions.relu(u2)

    # 出力層の総入力
    u3 = np.dot(z2, W3) + b3

    # 出力層の総出力
    y = u3

    print_vec("総入力1", u1)
    print_vec("中間層出力1", z1)
    print_vec("総入力2", u2)
    print_vec("出力1", z1)
    print("出力合計: " + str(np.sum(z1)))

    return y, z1, z2
コード例 #2
0
def forward(network, x):
    print ("##### 順伝播開始 #####")

    W1, W2, W3 = network ['W1'], network ['W2'], network ['W3']
    b1, b2, b3 = network ['b1'], network ['b2'], network ['b3']

    # 隠れ層の総入力
    u1 = np.dot (x, W1) + b1
    # 隠れ層1の総出力
    z1 = functions.relu (u1)
    # 隠れ層2層への総入力
    u2 = np.dot (z1, W2) + b2
    # 隠れ層2の出力
    z2 = functions.relu (u2)

    u3 = np.dot (z2, W3) + b3
    z3 = functions.sigmoid (u3)
    y = z3
    print_vec ("総入力1", u1)
    print_vec ("中間層出力1", z1)
    print_vec ("総入力2", u2)
    print_vec ("出力1", y)
    print ("出力合計: " + str (np.sum (y)))

    return y, z1
コード例 #3
0
 def predict(self, x):
     W1, W2, W3, W4 = self.W1, self.W2, self.W3, self.W4
     s1 = np.dot(x, W1)
     r1 = relu(s1)
     s2 = np.dot(r1, W2)
     r2 = relu(s2)
     s3 = np.dot(r2, W3)
     r3 = relu(s3)
     s4 = np.dot(r3, W4)
     y = softmax(s4)
     return y
コード例 #4
0
def predict_relu(network, x):
    w1, w2, w3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    a1 = np.dot(x, w1) + b1
    z1 = relu(a1)
    a2 = np.dot(z1, w2) + b2
    z2 = relu(a2)
    a3 = np.dot(z2, w3) + b3
    y = softmax(a3)

    return y
コード例 #5
0
    def test_relu_vector(self):
        inputs = [np.array([-1, 0, 1])]
        expected = [np.array([0, 0, 1])]

        for i in range(len(inputs)):
            actual = relu(inputs[i])
            npt.assert_array_equal(actual, expected[i])
コード例 #6
0
def forward(network, x):

    print("##### 順伝播開始 #####")
    W1, W2 = network['W1'], network['W2']
    b1, b2 = network['b1'], network['b2']

    # 1層の総入力
    u1 = np.dot(x, W1) + b1

    # 1層の総出力
    z1 = functions.relu(u1)

    # 2層の総入力
    u2 = np.dot(z1, W2) + b2

    # 出力値
    y = functions.softmax(u2)

    print_vec("総入力1", u1)
    print_vec("中間層出力1", z1)
    print_vec("総入力2", u2)
    print_vec("出力1", y)
    print("出力合計: " + str(np.sum(y)))

    return y, z1
コード例 #7
0
    def test_relu_scalar(self):
        inputs = [-1, 0, 1]
        expected = [0, 0, 1]

        for i in range(len(inputs)):
            actual = relu(inputs[i])
            self.assertEqual(
                actual, expected[i],
                "expected=[%d] but got actual=[%d]" % (expected[i], actual))
コード例 #8
0
    def forward(self, network, x):
        W1, W2, W3 = network['W1'], network['W2'], network['W3']
        b1, b2, b3 = network['b1'], network['b2'], network['b3']

        # 勾配
        u1 = np.dot(x, W1) + b1
        # 活性化関数 Relu関数を使用
        z1 = functions.relu(u1)
        # 勾配
        u2 = np.dot(z1, W2) + b2
        # 活性化関数 Relu関数を使用
        z2 = functions.relu(u2)
        # 勾配
        u3 = np.dot(z2, W3) + b3
        # 誤差関数(恒等写像)
        y = u3

        return z1, z2, y
コード例 #9
0
 def forward(self, x):
     # x->N,(T,D)
     self.x1 = x
     W1, b1, W2, b2 = self.params
     out1 = np.dot(x, W1) + b1
     out1 = relu(out1)
     # x2->N,(T,D)
     self.x2 = out1
     out2 = np.dot(out1, W2) + b2
     return out2
コード例 #10
0
def forward(network, x):
    W1, W2 = network['W1'], network['W2']
    b1, b2 = network['b1'], network['b2']

    u1 = np.dot(x, W1) + b1
    z1 = functions.relu(u1)
    u2 = np.dot(z1, W2) + b2
    y = functions.softmax(u2)

    return z1, y
コード例 #11
0
def predict(network, x):

    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']
    #x는 2차원 매개변수W는 4차원

    x = x.reshape(1, 1, 28, 28)  #x 데이터 성형
    print(W1.shape)
    N, C, H, W = x.shape
    FN, C, FH, FW = W1.shape

    out_h = int(1 + (H + 2 * 0 - FH) / 2)
    out_w = int(1 + (W + 2 * 0 - FW) / 2)
    col = im2col(x, FH, FW, 1, 0)
    col_W = W1.reshape(FN, -1).T  # 필터 전개
    out = np.dot(col, col_W) + b1

    out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2)

    a1 = out

    z1 = relu(a1)

    N, C, H, W = z1.shape
    out_h = int(1 + (H - 2 * 0 - FH) / 2)
    out_w = int(1 + (W - 2 * 0 - FW) / 2)

    z1 = im2col(z1, 2, 2, stride=2, pad=0)
    z1 = z1.reshape(-1, 2 * 2)
    z1 = np.max(z1, axis=1)

    print(z1.shape)
    print(W2.shape)
    print(W3.shape)

    a2 = np.dot(z1, W2) + b2
    z2 = relu(a2)
    a3 = np.dot(z2, W3) + b3
    y = softmax(a3)

    return y
コード例 #12
0
def forward(network, x):
    # print("##### 順伝播開始 #####")
    
    W1, W2 = network['W1'], network['W2']
    b1, b2 = network['b1'], network['b2']
    u1 = np.dot(x, W1) + b1
    z1 = functions.relu(u1)
    
    ## 試してみよう
    #z1 = functions.sigmoid(u1)
    
    u2 = np.dot(z1, W2) + b2
    y = u2

    # print_vec("総入力1", u1)
    # print_vec("中間層出力1", z1)
    # print_vec("総入力2", u2)
    # print_vec("出力1", y)
    # print("出力合計: " + str(np.sum(y)))    
    
    return z1, y
コード例 #13
0
    def backward(self, dout):
        # dout->N,(T,D) / W1->(D,Df) / W2->(Df,D)
        W1, b1, W2, b2 = self.params
        N, T, _ = dout.shape
        dx2 = np.dot(dout, W2.T)
        # dx2->N,(T,Df)
        dx2 = relu(dx2)
        dW2 = np.matmul(np.transpose(self.x2, (0, 2, 1)), dout)
        dW2 = np.nansum(dW2, axis=0)
        dx1 = np.dot(dx2, W1.T)
        dW1 = np.matmul(np.transpose(self.x1, (0, 2, 1)), dx2)
        dW1 = np.nansum(dW1, axis=0)

        db2 = np.nansum(np.sum(dout, axis=0), axis=0)
        db1 = np.nansum(np.sum(dx2, axis=0), axis=0)

        self.grads[0][...] = dW1
        self.grads[1][...] = db1
        self.grads[2][...] = dW2
        self.grads[3][...] = db2

        return dx1
コード例 #14
0
    out_bin = np.zeros_like(d_bin)

    # 時系列全体の誤差
    all_loss = 0

    # 時系列ループ
    for t in range(binary_dim):
        # 入力値
        X = np.array([a_bin[-t - 1], b_bin[-t - 1]]).reshape(1, -1)
        # 時刻tにおける正解データ
        dd = np.array([d_bin[binary_dim - t - 1]])

        u[:, t + 1] = np.dot(X, W_in) + np.dot(z[:, t].reshape(1, -1), W)
        # 中間層の活性化関数を変更してみよう
        # z[:,t+1] = functions.sigmoid(u[:,t+1])
        z[:, t + 1] = functions.relu(u[:, t + 1])

        # y[:,t] = functions.sigmoid(np.dot(z[:,t+1].reshape(1, -1), W_out))
        y[:, t] = functions.relu(np.dot(z[:, t + 1].reshape(1, -1), W_out))

        # 誤差
        loss = functions.mean_squared_error(dd, y[:, t])

        # delta_out[:,t] = functions.d_mean_squared_error(dd, y[:,t]) * functions.d_sigmoid(y[:,t])
        delta_out[:, t] = functions.d_mean_squared_error(
            dd, y[:, t]) * functions.d_relu(y[:, t])

        all_loss += loss

        out_bin[binary_dim - t - 1] = np.round(y[:, t])
コード例 #15
0
#画像テスト用データインポート
#--------------------------------
import sys, os
from dataset.mnist import load_mnist
from PIL import Image
#--------------------------------

a = np.array([10000.3,2.9,4.0])
y = softmax(a)
print(np.sum(y))


x = np.arange(-5.0, 5.0, 0.1)
y1 = sigmoid(x)
y2 = step_function(x)
y3 = relu(x)

plt.plot(x, y1)
plt.plot(x, y2, 'k--')
plt.plot(x, y3)
plt.ylim(-0.1, 1.1) #図で描画するy軸の範囲を指定

#画像出力
#--------------------------------
plt.savefig('../../../../var/www/html/images/graph.png')

#--------------------------------
# 活性化関数による3層ニューラルネットワークのプロセス
#--------------------------------
def init_network():
    network = {}
コード例 #16
0
## 試してみよう_数値の初期化
#b = np.random.rand() # 0~1のランダム数値
#b = np.random.rand() * 10 -5  # -5~5のランダム数値

print_vec("バイアス", b)

# 入力値
x = np.array([2, 3])
print_vec("入力", x)

# 総入力
u = np.dot(x, W) + b
print_vec("総入力", u)

# 中間層出力
z = functions.relu(u)
print_vec("中間層出力", z)

# In[ ]:

# 順伝播(単層・複数ユニット)

# 重み
W = np.array([[0.1, 0.2, 0.3], [0.2, 0.3, 0.4], [0.3, 0.4, 0.5],
              [0.4, 0.5, 0.6]])

## 試してみよう_配列の初期化
#W = np.zeros((4,3))
#W = np.ones((4,3))
#W = np.random.rand(4,3)
#W = np.random.randint(5, size=(4,3))
コード例 #17
0
import numpy as np
import numpy.testing as npt
from common import functions as function
from common import gradient
from common import optimizer

#test sigmoid
X = np.array([-1.0, 1.0, 2.0])
Y = function.sigmoid(X)
expected_Y = np.array([0.26894142, 0.73105858, 0.88079708])
npt.assert_array_almost_equal(Y, expected_Y)

#test ReLU
X = np.array([-0.2, 0.3, 10.0, -2.0])
Y = function.relu(X)
expected_Y = np.array([0, 0.3, 10.0, 0])
npt.assert_array_equal(Y, expected_Y)

#test softmax
a = np.array([0.3, 2.9, 4.0])
Y = function.softmax(a)
expected_Y = np.array([0.01821127, 0.24519181, 0.73659691])
npt.assert_array_almost_equal(Y, expected_Y)

#test MSE
#3번째가 정답
T = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])
#3번째일 확률이 가장 높을 것이라고 추정
Y = np.array([0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0])
MSE = function.MSE(Y, T)
expected_MSE = 0.0975