def compare_binary_op(self, kk_func, z_layer, shape): x = klayers.Input(shape=shape[0][1:]) y = klayers.Input(shape=shape[1][1:]) batch = shape[0][0] kkresult = kk_func(x, y) x_value = np.random.uniform(0, 1, shape[0]) y_value = np.random.uniform(0, 1, shape[1]) k_grad_y_pred = KK.get_session().run(KK.gradients(kkresult, [x, y]), feed_dict={ x: x_value, y: y_value }) k_output = KK.get_session().run(kkresult, feed_dict={ x: x_value, y: y_value }) inputs = [Input(s) for s in remove_batch(shape)] model = Model(inputs, z_layer(inputs)) z_output = model.forward([x_value, y_value]) grad_output = np.array(z_output) grad_output.fill(1.0) z_grad_y_pred = model.backward(x_value, grad_output) self.assert_allclose(z_output, k_output) [ self.assert_allclose(z, k) for (z, k) in zip(z_grad_y_pred, k_grad_y_pred) ]
def add(self, model): from zoo.pipeline.api.autograd import Lambda if (isinstance(model, Lambda)): if not self.is_built(): if not model.input_shape: raise Exception("You should specify inputShape for the first layer") input_shapes = model.input_shape else: input_shapes = self.get_output_shape() model = model.create(remove_batch(input_shapes)) self.value.add(model.value) return self
def add(self, model): from zoo.pipeline.api.autograd import Lambda if (isinstance(model, Lambda)): if not self.is_built(): if not model.input_shape: raise Exception("You should specify inputShape for the first layer") input_shapes = model.input_shape else: input_shapes = self.get_output_shape() model = model.create(remove_batch(input_shapes)) self.value.add(model.value) return self
def __call__(self, x=None): """ Some other modules point to current module :param x: upstream module nodes. x is either a Node or list of Node. :return: node containing current module """ x = to_list(x if x else []) layer = self if isinstance(self, Lambda): input_shapes = [var.get_output_shape() for var in x] layer = self.create(remove_batch(input_shapes)) return Variable.from_jvalue( callBigDlFunc(self.bigdl_type, "connectInputs", layer, to_list(x)))
def __call__(self, x=None): """ Some other modules point to current module :param x: upstream module nodes. x is either a Node or list of Node. :return: node containing current module """ x = to_list(x if x else []) layer = self if isinstance(self, Lambda): input_shapes = [var.get_output_shape() for var in x] layer = self.create(remove_batch(input_shapes)) return Variable.from_jvalue(callBigDlFunc(self.bigdl_type, "connectInputs", layer, to_list(x)))
def __call__(self, x=None): """ Some other modules point to current module :param x: upstream module nodes. x is either a Node or list of Node. :return: node containing current module """ x = to_list(x if x else []) layer = self if isinstance(self, Lambda): input_shapes = [ ZooKerasLayer.of(node.element().value).get_output_shape() for node in x ] layer = self.create(remove_batch(input_shapes)) return Node.of( callBigDlFunc(self.bigdl_type, "createNode", layer, to_list(x)))
def compare_binary_op(self, kk_func, z_layer, shape, rtol=1e-5, atol=1e-5): x = klayers.Input(shape=shape[0][1:]) y = klayers.Input(shape=shape[1][1:]) batch = shape[0][0] kkresult = kk_func(x, y) x_value = np.random.uniform(0, 1, shape[0]) y_value = np.random.uniform(0, 1, shape[1]) k_grads = KK.get_session().run(KK.gradients(kkresult, [x, y]), feed_dict={ x: x_value, y: y_value }) k_output = KK.get_session().run(kkresult, feed_dict={ x: x_value, y: y_value }) inputs = [Input(s) for s in remove_batch(shape)] model = Model(inputs, z_layer(inputs)) z_output = model.forward([x_value, y_value]) grad_output = np.array(z_output) grad_output.fill(1.0) z_grads = model.backward([x_value, y_value], grad_output) # Check if the model can be forward/backward multiple times or not z_output2 = model.forward([x_value, y_value]) z_grads2 = model.backward([x_value, y_value], grad_output) self.assert_allclose(z_output, z_output2, rtol, atol) [ self.assert_allclose(z, k, rtol, atol) for (z, k) in zip(z_grads, z_grads2) ] self.assert_allclose(z_output, k_output, rtol, atol) [ self.assert_allclose(z, k, rtol, atol) for (z, k) in zip(z_grads, k_grads) ]
def test_remove_batch(self): from zoo.pipeline.api.utils import remove_batch assert remove_batch([2, 3, 4]) == [3, 4] assert remove_batch([[2, 6, 7], [2, 3, 4]]) == [[6, 7], [3, 4]]