def test_layer_normalize(node, use_gpu): node = Variable(node * 50) assert_cuda_active(use_gpu) layer = LayerNormalize() layer2 = Dense(4) layer3 = Conv2d(channel=3) def func(node): ret = layer(node) if len(ret.shape) > 2: return sum(layer3(ret)) else: return sum(layer2(ret)) a = 1e-5 r = 1e-3 if use_gpu: a = 1e-2 r = 1e-3 for trial in range(3): try: compare(func, node, node, atol=a, rtol=r) compare(func, layer.params["gain"], node) compare(func, layer.params["bias"], node) return except: node = Variable(rand(node.shape)) assert False
def test_max_unpoolnd(node, use_gpu): assert_cuda_active(use_gpu) node = Variable(node) l0 = MaxPoolNd(kernel=2, padding=1, stride=1) l1 = MaxUnPoolNd() l2 = Dense(2) np.set_printoptions(suppress=True) def func(node): ret = node reta = l0(node) ret = l1(reta, reta) ret = l2(ret.reshape(ret.shape[0], -1)) ret = sum(ret) return ret for trial in range(3): try: compare(func, node, node) return except AssertionError as e: print(e) node = Variable(rand(node.shape)) raise AssertionError("Failed all attempts.")
def test_dense(node, use_gpu): node = Variable(node) set_cuda_active(use_gpu) layer = Dense(output_size=2) def func(node): return sum(layer(node)) compare(func, node, node) compare(func, layer.params["w"], node) compare(func, layer.params["b"], node)
def test_weight_normalize(node, use_gpu): node = Variable(node) assert_cuda_active(use_gpu) layer = WeightNormalize(4) layer2 = Dense(3) # This is important to ensure that dy is properly transferred backwards def func(node): return sum(layer2(layer(node))) compare(func, node, node) compare(func, layer.params["gain"], node) compare(func, layer.params["w"], node, atol=1e-4) compare(func, layer.params["bias"], node)
def test_dense(node, use_gpu, ignore_bias): node = Variable(node) assert_cuda_active(use_gpu) layer = Dense(output_size=2, ignore_bias=ignore_bias) def func(node): return sum(layer(node)) compare(func, node, node) compare(func, layer.params["w"], node) try: compare(func, layer.params["b"], node) except Exception: assert ignore_bias
def test_average_unpoolnd(node, use_gpu): assert_cuda_active(use_gpu) node = Variable(node) l0 = AveragePoolNd(kernel=2) l1 = AverageUnPoolNd() l2 = Dense(2) np.set_printoptions(suppress=True) def func(node): ret = node reta = l0(node) ret = l1(reta, reta) ret = l2(ret.reshape(ret.shape[0], -1)) return sum(ret) compare(func, node, node)
def test_average_unpool2d(node, use_gpu): assert_cuda_active(use_gpu) node = Variable(node) l0 = AveragePool2d(filter=2) l1 = AverageUnPool2d() l2 = Dense(2) np.set_printoptions(suppress=True) def func(node): ret = l0(node) ret = l1(ret) ret = l2(ret.reshape(ret.shape[0], -1)) return sum(ret) for trial in range(1): try: compare(func, node, node) return except AssertionError as e: print(e) node = Variable(rand(node.shape)) raise AssertionError("Failed all three attempts.")