Beispiel #1
0
 def __getitem__(self, index) -> Tuple[Data, ...]:
     if isinstance(index, int):
         size = self.size[self.column] // self.batch_size
         start = np.arange(self.batch_size) * size + self.start[self.column]
         start = start.reshape(-1, 1) + np.arange(self.time_size)
         start += index * self.time_size
         index = start
     else:
         raise NotImplementedError
     return tuple(x[index] for x in self.data)
Beispiel #2
0
 def forward(self):
     y = np.exp(self.x.d - self.x.d.max(axis=-1, keepdims=True))
     y /= y.sum(axis=-1, keepdims=True)
     self.y.d = y
     self.y_2d = self.y.d.reshape(-1, self.y.d.shape[-1])
     self.t_1d = self.t.d.reshape(-1)
     self.size = self.y_2d.shape[0]
     loss = self.y_2d[np.arange(self.size), self.t_1d]
     self.loss.d = -np.sum(np.log(loss + 1e-7)) / self.size
Beispiel #3
0
 def backward(self):
     PH, PW = self.shape[3:5]
     dy = self.y.g.transpose(0, 2, 3, 1)
     pool_size = self.shape[3] * self.shape[4]
     dmax = np.zeros((dy.size, pool_size))
     dmax[np.arange(self.arg_max.size), self.arg_max.flatten()] = dy.flatten()
     dmax = dmax.reshape(dy.shape + (pool_size,))
     dx_2d = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
     self.x.g = col2im(dx_2d, self.x.d.shape, PH, PW, self.stride.d, self.padding.d)
Beispiel #4
0
# ## TimeDataset

# 時系列データは`TimeDataset`クラスで作成できます。

from ivory.common.context import np
from ivory.common.dataset import TimeDataset

x = np.arange(128).reshape(-1, 4)
t = x[:, 0]
data = TimeDataset((x, t), batch_size=2, time_size=4)
data
for x, t in data:
    print(data.state)
    print(t)
print(data.shape, x.shape, t.shape)
Beispiel #5
0
 def forward(self):
     self.y.d = 1 / (1 + np.exp(-self.x.d))
     y = self.y.d.reshape(-1)
     self.size = y.shape[0]
     loss = np.c_[1 - y, y][np.arange(self.size), self.t.d.reshape(-1)]
     self.loss.d = -np.sum(np.log(loss + 1e-7)) / self.size
Beispiel #6
0
 def backward(self):
     self.y_2d[np.arange(self.size), self.t_1d] -= 1
     self.x.g = self.y_2d.reshape(*self.x.d.shape) / self.size
Beispiel #7
0

max_grad = 5.0

optimizer = Adam()
optimizer.set_model(model)

batch_size = 128
data_size = len(x_train)
max_iters = data_size // batch_size

total_loss = 0.0
count = 0

for epoch in range(25):
    idx = np.random.permutation(np.arange(data_size))
    x_train = x_train[idx]
    t_train = t_train[idx]

    for iters in range(max_iters):
        batch_x = x_train[iters * batch_size : (iters + 1) * batch_size]
        batch_t = t_train[iters * batch_size : (iters + 1) * batch_size]

        model.set_data(batch_x, batch_t[:, :-1], batch_t[:, 1:])
        model.forward()
        total_loss += model.loss
        count += 1
        model.backward()
        model.clip_grads(max_grad)
        optimizer.update()
Beispiel #8
0
# 実際に使ってみます。
import cupy as cp

a = cp.arange(10)
print(type(a))
print(a.dtype)
print(a.device)


# Ivoryライブラリでは、`ivory.common.context`モジュールを使ってCPUとCUDAを切り替えます。
from ivory.common.context import np  # isort:skip

# コンテキスを確認します。
np.context
# NumPyと同じように使います。
a = np.arange(10, dtype="f")
print(type(a))
print(a.dtype)
# コンテキストを変更して、同じコードを試してみましょう。
np.context = "cuda"
a = np.arange(10, dtype="f")
print(type(a))
print(a.dtype)
# 今度はCuPyのアレイが得られました。このように同じコードで切り替えが可能です。参考までに
# 、`context`モジュールをソースを記載しておきます。

# ##File <code>ivory.common.context</code>モジュールファイル
# {%=/ivory/common/context.py%}

# {{ np.context = "cpu" }}