if '__file__' in globals(): import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np from my_dezero import Variable import my_dezero.functions as F x = Variable(np.array([[1,2,3], [4,5,6]])) y = F.sum(x) #y.backward() print(y) #print(x.grad) x = np.array(([[1,2,3],[4,5,6]])) y = np.sum(x, axis=0) print(y) print(x.shape, '->', y.shape) x = np.array([[1,2,3], [4,5,6]]) y = np.sum(x, keepdims=True) print(y) print(y.shape) # 2次元のまま # Dezeroのsum関数でも2つの引数を指定できるように修正 x = Variable(np.array([[1,2,3], [4,5,6]])) y = x.sum(axis=0) print(y) x = Variable(np.random.randn(2,3,4,5)) y = x.sum(keepdims=True)
def softmax1d(x): c = as_variable(as_array(np.max(x))) # over flow対策 x = as_variable(x) y = F.exp(x-c) sum_y = F.sum(y) return y / sum_y
def mean_square_error(x0, x1): diff = x0 - x1 return F.sum(diff**2) / len(diff)
if '__file__' in globals(): import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np import matplotlib.pyplot as plt from my_dezero import Variable import my_dezero.functions as F x = Variable(np.array([[1,2,3], [4,5,6]])) y = F.sin(x) print(y) c = Variable(np.array([[10,20,30], [40,50,60]])) y = x + c print(y) x = Variable(np.array([[1,2,3], [4,5,6]])) c = Variable(np.array([[10,20,30], [40,50,60]])) t = x + c y = F.sum(t) y.backward(retain_grad=True) print(y.grad) print(t.grad) print(x.grad) print(c.grad)
def f(x): t = x ** 2 y = F.sum(t) return t