def test_forward(self): x_val = np.random.random((3, 4)) x = ad.variable(x_val) y = ad.shape(x) actual = y.forward() expect = np.array([3, 4]) self.assertEqual(expect.shape, y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual))
def call(self, inputs, **kwargs): if 0.0 < self.rate < 1.0: if self.noise_shape is not None: noise_shape = self.noise_shape else: noise_shape = ad.shape(inputs) return ad.where(ad.in_train_phase(), inputs * (ad.random(noise_shape) > self.rate), inputs) return inputs
def call(self, inputs, **kwargs): padded = ad.pad(inputs, ((0, ), (self.pad_width[0], ), (self.pad_width[1], ), (0, ))) batch_size = ad.shape(inputs)[0] reshaped = ad.map_fn(lambda i: self.call_batch(padded, i), ad.arange(batch_size)) y = ad.dot(reshaped, self.w) if self.use_bias: y += self.b if self.activation is not None: y = self.activation(y) return y
def call(self, inputs, **kwargs): initial_val = ad.dot( ad.zeros_like(inputs)[:, 0, :], ad.zeros_like(self.wx[:, :self.units])) outputs = ad.while_loop( lambda body_inputs: ad.less(body_inputs[0], ad.shape(inputs)[1]), lambda x: self.step(inputs, x), [ad.variable(0.0), initial_val, initial_val], output_index=-1, ) if self.return_sequences: return outputs.transpose(axes=[1, 0, 2]) return outputs[-1]
def test_backward(self): x_val = np.random.random((3, 4)) x = ad.variable(x_val) y = ad.shape(x) self.numeric_gradient_check(y, {}, [x])
def call_batch(self, padded: ad.Operation, i: int): height = ad.shape(padded)[1] new_height = (height - self.dilated_kernel_size[0]) // self.strides[0] + 1 return ad.map_fn(lambda r: self.call_row(padded, i, r), ad.arange(new_height))
def call_row(self, padded: ad.Operation, i: int, r: int): width = ad.shape(padded)[2] new_width = (width - self.dilated_kernel_size[1]) // self.strides[1] + 1 return ad.map_fn(lambda c: self.call_column(padded, i, r, c), ad.arange(new_width))
def test_forward_variable_shape(self): x = ad.placeholder(shape=(None, 3)) y = ad.random(ad.shape(x)) z = y.forward({x: np.random.random((5, 3))}) self.assertEqual((5, 3), z.shape)