def gradient(self, x, t):
        """Calculate gradient to weight params using backpropagation.
           This can calculate faster than numerical gradient function.

        Args:
            x (numpy.ndarray): image data which mean input to NN
            t (numpy.ndarray): labels

        Return:
            dictionary: dictionary of gradient to each param.
        """

        W1, W2 = self.params['W1'], self.params['W2']
        b1, b2 = self.params['b1'], self.params['b2']
        grads = {}

        batch_num = x.shape[0]

        # forward
        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)

        # backward
        dy = (y - t) / batch_num
        grads['W2'] = np.dot(z1.T, dy)
        grads['b2'] = np.sum(dy, axis=0)

        dz1 = np.dot(dy, W2.T)
        da1 = sigmoid_grad(a1) * dz1
        grads['W1'] = np.dot(x.T, da1)
        grads['b1'] = np.sum(da1, axis=0)

        return grads
Beispiel #2
0
    def backward(self, dz):
        # 出力部の逆伝搬(シグモイド版)
        dy = fn.sigmoid_grad(self.z) * dz
        #print("dy:", dy)
        #print("x.T:", self.x.T)
        self.b_grad = dy
        self.W_grad = np.dot(self.x.T, dy)
        dx = np.dot(dy, self.W.T)
        #print("dw:", self.W_grad)
        #print("W:", self.W)
        W_tmp = self.W
        #print("dW", np.sum(self.W_grad))
        # オプティマイザーによりself.W、self.bの値を更新
        self.W = self.W - self.lr * self.W_grad
        self.b = self.b - self.lr * self.b_grad

        #print("Wn-W", np.sum(self.W - Wn))

        #print("W:", self.W)

        return dx
    def gradient(self, x, t):
        w1, w2 = self.dict['w1'], self.dict['w2']
        b1, b2 = self.dict['b1'], self.dict['b2']
        grads = {}

        a1 = np.dot(x, w1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, w2) + b2
        y = softmax(a2)

        num = x.shape[0]
        dy = (y - t) / num
        grads['w2'] = np.dot(z1.T, dy)
        grads['b2'] = np.sum(dy, axis=0)

        da1 = np.dot(dy, w2.T)
        dz1 = sigmoid_grad(a1) * da1
        grads['w1'] = np.dot(x.T, dz1)
        grads['b1'] = np.sum(dz1, axis=0)

        return grads
Beispiel #4
0
    def gradient(self, x, t):
        W1, W2 = self.params['W1'], self.params['W2']
        b1, b2 = self.params['b1'], self.params['b2']
        grads = {}

        batch_num = x.shape[0]

        # forward
        a1 = np.dot(x, W1) + b1
        z1 = f.sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = f.softmax(a2)

        # backward
        dy = (y - t) / batch_num
        grads['W2'] = np.dot(z1.T, dy)
        grads['b2'] = np.sum(dy, axis=0)

        dz1 = np.dot(dy, W2.T)
        da1 = f.sigmoid_grad(a1) * dz1
        grads['W1'] = np.dot(x.T, da1)
        grads['b1'] = np.sum(da1, axis=0)

        return grads
Beispiel #5
0
 def backward(self, dout):
     return dout * fun.sigmoid_grad(self.x)