def test_simple_conv(self): bs = 10 r = 2 n_channels = 3 n_filters = 5 w, h = 32, 32 # define a simple model x = nn.Tensor((bs, n_channels, h, w)) W = nn.Constant( np.random.randn(n_filters, n_channels, 2 * r + 1, 2 * r + 1).astype(np.float32)) y = nn.conv(x, W) # check shapes self.assertEqual(y.output.shape, (bs, n_filters, h, w)) # compile model mdl = nn.compile_model(x, y) # emulate model with numpy npx = np.random.randn(bs, n_channels, h, w).astype(np.float32) npy = np_pad(npx, r) npy = np_conv(npy, W.value) # results must be very similar res = mdl(npx) self.assertLess(np.max(np.abs(npy - res)), 1e-4)
def main(): tot_images = 50000 bs = 200 n_filters = 1024 x = nn.Tensor((bs, 3, 32, 32)) w_1 = nn.Constant(np.random.randn(n_filters, 3, 5, 5).astype(np.float32)) y = nn.conv(x, w_1) # y = nn.activation(y, lambda x: "exp(-({x}))") mdl = nn.compile_model(x, y) # s = time.time() npx = np.random.randn(bs, 3, 32, 32).astype(np.float32) # npy = np_pad(npx, 2) # npy = np_conv(npy, w_1.value) # e = time.time() # print("Numpy time", e - s) s = time.time() res = mdl(npx) e = time.time() print("OpenCL time", e - s) print( f"Expected time for {tot_images} is {(tot_images / bs) * (e - s)} seconds ({(tot_images / (bs * 60)) * (e - s)} minutes)" )
def test_variable_with_tensor(self, size, U): a = U.random(size) p = tf.convert_to_tensor(a) x = nn.variable(a) assert U.equal(x, p) assert nn.tf(x) is x.tf() x = nn.variable(p) assert U.equal(x, p) assert x.tf() is not p y = nn.variable(x) assert y is x y = nn.Tensor(x) assert y is not x assert U.equal(y, x) assert y.tf() is x.tf() self.assert_properties(y, x)