コード例 #1
0
def dropout(x, dropout_ratio=0.5):
    x = as_variable(x)

    if dezero.Config.train:
        xp = cuda.get_array_module(x)
        mask = xp.random.rand(*x.shape) > dropout_ratio
        scale = xp.array(1.0 - dropout_ratio).astype(x.dtype)
        y = x * mask / scale
        return y
    else:
        return x
コード例 #2
0
ファイル: functions.py プロジェクト: rrbb014/rrbb-playground
    def backward(self, gy):
        x, t = self.inputs
        N, CLS_NUM = x.shape

        gy *= 1 / N
        y = softmax(x)
        # Convert to one-hot
        #xp = cuda.get_array_module(t.data)
        xp = as_variable(x)
        t_onehot = xp.eye(CLS_NUM, dtype=t.dtype)[t.data]
        y = (y - t_onehot) * gy
        return y
コード例 #3
0
ファイル: functions_conv.py プロジェクト: tokuma09/DeZero
def pooling_simple(x, kernel_size, stride=1, pad=0):
    x = as_variable(x)
    N, C, H, W = x.shape
    KH, KW = pair(kernel_size)
    PH, PW = pair(pad)
    SH, SW = pair(stride)
    OH = get_conv_outsize(H, KH, SH, PH)
    OW = get_conv_outsize(W, KW, SW, PW)

    col = im2col(x, kernel_size, stride, pad, to_matrix=True)
    col = col.reshape(-1, KH * KW)
    y = col.max(axis=1)
    y = y.reshape(N, OH, OW, C).transpose(0, 3, 1, 2)
    return y
コード例 #4
0
def pooling_simple(x, kernel_size, stride=1, pad=0):
    x = as_variable(x)

    n, c, h, w = x.shape
    kh, kw = _pair(kernel_size)
    ph, pw = _pair(pad)
    sh, sw = _pair(stride)
    out_h = (h + ph * 2 - kh) // sh + 1
    out_w = (h + pw * 2 - kw) // sw + 1

    col = im2col(x, kernel_size, stride, pad)

    col = col.transpose((0, 4, 5, 1, 2, 3)).reshape((-1, kh * kw))

    y = col.max(axis=1)

    y = y.reshape((n, out_h, out_w, c))
    y = y.transpose((0, 3, 1, 2))
    return y
コード例 #5
0
def sigmoid_simple(x):
    x = as_variable(x)
    y = 1 / (1 + exp(-x))
    return y
コード例 #6
0
def broadcast_to(x, shape):
    if x.shape == shape:
        return as_variable(x)
    return BroadcastTo(shape)(x)
コード例 #7
0
def sum_to(x, shape):
    if x.shape == shape:
        return as_variable(x)
    return SumTo(shape)(x)
コード例 #8
0
def reshape(x, shape):
    if x.shape == shape:
        return as_variable(x)
    return Reshape(shape)(x)
コード例 #9
0
def average(x, axis=None, keepdims=False):
    x = as_variable(x)
    y = sum(x, axis, keepdims)
    return y * (y.data.size / x.data.size)
コード例 #10
0
def expand_dims(x, axis):
    x = as_variable(x)
    shape = list(x.shape)
    shape.insert(axis, 1)
    return reshape(x, tuple(shape))
コード例 #11
0
ファイル: functions.py プロジェクト: rrbb014/rrbb-playground
 def forward(self, x):
     x = as_variable(x)
     y = 1 / (1 + exp(-x))
     return y
コード例 #12
0
def accuracy(y, t):
    y, t = as_variable(y), as_variable(t)
    pred = y.data.argmax(axis=1).reshape(t.shape)
    result = (pred == t.data)
    acc = result.mean()
    return Variable(as_array(acc))
コード例 #13
0
 def test_forward(self):
     a = as_variable(6.0)
     b = as_variable(4.0)
     c = div(a, b)
     self.assertEqual(c.data, np.array(1.5))
コード例 #14
0
ファイル: functions.py プロジェクト: rrbb014/rrbb-playground
 def forward(self, x):
     #xp = cuda.get_array_module(x)
     xp = as_variable(x)
     y = xp.clip(x, self.x_min, self.x_max)
     return y
コード例 #15
0
ファイル: functions.py プロジェクト: rrbb014/rrbb-playground
 def forward(self, x):
     x = as_variable(x)
     y = x - x.max(axis=self.axis, keepdims=True)
     y = x.exp(y)
     y /= y.sum(axis=self.axis, keepdims=True)
     return y
コード例 #16
0
def softmax_simple(x, axis=1):
    x = as_variable(x)
    y = exp(x)
    sum_y = sum(y, axis=axis, keepdims=True)
    return y / sum_y
コード例 #17
0
def mean_squared_error_simple(x0, x1):
    x0, x1 = as_variable(x0), as_variable(x1)
    diff = x0 - x1
    y = sum(diff**2) / len(diff)
    return y
コード例 #18
0
def mean_squared_error_simple(x0, x1):
    x0, x1 = as_variable(x0), as_variable(x1)
    diff = x0 - x1
    return sum(diff**2) / diff.size