def test_relu(): x = sym.Variable("x") y = sym.relu(sym.leaky_relu(x, alpha=0.3) - 0.2) def forward(x): x = (x < 0) * x * 0.3 + (x > 0) * x - 0.2 return (x > 0) * x dtype = "float32" dshape = (1, 3, 32, 32) inputs = {'x': (dshape, x)} helper(y, inputs, dtype, forward)
def compile(self, **kwargs): if kwargs['op'] == 'dense': return sym.dense(data=kwargs['data'], weight=kwargs['weight'], bias=kwargs['bias'], units=kwargs['units']) elif kwargs['op'] == 'relu': return sym.relu(data=kwargs['data']) elif kwargs['op'] == 'leaky_relu': return sym.leaky_relu(data=kwargs['data'], alpha=kwargs['alpha']) elif kwargs['op'] == 'sigmoid': return sym.sigmoid(data=kwargs['data']) else: raise RuntimeError('invalid operator')
def test_relu(): x = sym.Variable("x") y = sym.relu(sym.leaky_relu(x, alpha=0.3) - 0.2) def forward(x): x = (x < 0) * x * 0.3 + (x > 0) * x - 0.2 return (x > 0) * x def backward(head_grads, x): sub = (x < 0) * x * 0.3 + (x > 0) * x - 0.2 return [(sub > 0).astype("float") * \ ((x > 0).astype("float") + 0.3 * (x < 0).astype("float")) * head_grads] shape = {'x': (1, 3, 32, 32)} check_function(y, forward, backward, shape=shape)
def test_relu(): x = sym.Variable("x") y = sym.leaky_relu(x, alpha=0.3) - 0.2 y = sym.relu(y) dtype = "float32" dshape = (1, 3, 32, 32) oshape = dshape for target, ctx in ctx_list(): graph, lib, _ = nnvm.compiler.build(y, target, {"x": dshape}) m = graph_runtime.create(graph, lib, ctx) data = np.random.uniform(size=dshape).astype(dtype) m.run(x=data) data = (data < 0) * data * 0.3 + (data>0) * data - 0.2 data = (data > 0) * data out = m.get_output(0, tvm.nd.empty(oshape, dtype)) np.testing.assert_allclose(out.asnumpy(), data, atol=1e-5, rtol=1e-5)
def test_relu(): x = sym.Variable("x") y = sym.relu(sym.leaky_relu(x, alpha=0.3) - 0.2) def forward(x): x = (x < 0) * x * 0.3 + (x > 0) * x - 0.2 return (x > 0) * x def backward(head_grads, x): sub = (x < 0) * x * 0.3 + (x > 0) * x - 0.2 return [(sub > 0).astype("float") * \ ((x > 0).astype("float") + 0.3 * (x < 0).astype("float")) * head_grads] dtype = "float32" dshape = (1, 3, 32, 32) inputs = [('x', dshape, x)] helper(y, inputs, dtype, forward, backward)
def test_leaky_relu(): x = sym.Variable("x") y = sym.leaky_relu(x, alpha=0.1) assert(y.list_input_names() == ["x"])