Minor edits and fixes for kernelized layers.
PiperOrigin-RevId: 239195286
This commit is contained in:
parent
ad2e814522
commit
abf1fdc852
@ -85,7 +85,7 @@ class RandomFourierFeatures(base_layer.Layer):
|
||||
model.add(random_features_layer)
|
||||
model.add(tf.keras.layers.Dense(units=num_classes, activation='softmax')
|
||||
|
||||
model.compile(elif isinstance(identifier, six.string_types):
|
||||
model.compile(
|
||||
loss=tf.keras.losses.categorical_crossentropy, optimizer=..., metrics=...)
|
||||
```
|
||||
|
||||
|
@ -356,21 +356,23 @@ class RandomFourierFeaturesTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertLess(abs_error, 0.5)
|
||||
|
||||
@parameterized.named_parameters(
|
||||
('gaussian', 'gaussian', 10.0, _exact_gaussian(stddev=10.0)),
|
||||
('laplacian', 'laplacian', 50.0, _exact_laplacian(stddev=50.0)))
|
||||
('gaussian', 'gaussian', 5.0, _exact_gaussian(stddev=5.0)),
|
||||
('laplacian', 'laplacian', 10.0, _exact_laplacian(stddev=10.0)))
|
||||
@test_util.run_in_graph_and_eager_modes()
|
||||
def test_good_kernel_approximation_multiple_inputs(self, initializer, scale,
|
||||
exact_kernel_fn):
|
||||
# Parameters.
|
||||
input_dim = 5
|
||||
output_dim = 5000
|
||||
output_dim = 2000
|
||||
x_rows = 20
|
||||
y_rows = 30
|
||||
|
||||
random_seed.set_random_seed(1234)
|
||||
x = random_ops.random_uniform(shape=(x_rows, input_dim), maxval=1.0)
|
||||
y = random_ops.random_uniform(shape=(y_rows, input_dim), maxval=1.0)
|
||||
x = constant_op.constant(
|
||||
np.random.uniform(size=(x_rows, input_dim)), dtype=dtypes.float32)
|
||||
y = constant_op.constant(
|
||||
np.random.uniform(size=(y_rows, input_dim)), dtype=dtypes.float32)
|
||||
|
||||
random_seed.set_random_seed(1234)
|
||||
rff_layer = kernel_layers.RandomFourierFeatures(
|
||||
output_dim=output_dim,
|
||||
kernel_initializer=initializer,
|
||||
@ -384,7 +386,7 @@ class RandomFourierFeaturesTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
approx_kernel_matrix = kernelized_utils.inner_product(output_x, output_y)
|
||||
exact_kernel_matrix = exact_kernel_fn(x, y)
|
||||
self._assert_all_close(approx_kernel_matrix, exact_kernel_matrix, atol=0.1)
|
||||
self._assert_all_close(approx_kernel_matrix, exact_kernel_matrix, atol=0.05)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -58,7 +58,7 @@ def inner_product(u, v):
|
||||
|
||||
|
||||
def exact_gaussian_kernel(x, y, stddev):
|
||||
"""Computes exact Gaussian kernel value(s) for tensors x and y and stddev.
|
||||
r"""Computes exact Gaussian kernel value(s) for tensors x and y and stddev.
|
||||
|
||||
The Gaussian kernel for vectors u, v is defined as follows:
|
||||
K(u, v) = exp(-||u-v||^2 / (2* stddev^2))
|
||||
@ -79,7 +79,7 @@ def exact_gaussian_kernel(x, y, stddev):
|
||||
all (u,v) pairs where u, v are rows from x and y respectively.
|
||||
|
||||
Raises:
|
||||
InvalidShapeError: if the shapes of x, y are not compatible.
|
||||
ValueError: if the shapes of x, y are not compatible.
|
||||
"""
|
||||
x_aligned, y_aligned = _align_matrices(x, y)
|
||||
diff_squared_l2_norm = math_ops.reduce_sum(
|
||||
@ -88,7 +88,7 @@ def exact_gaussian_kernel(x, y, stddev):
|
||||
|
||||
|
||||
def exact_laplacian_kernel(x, y, stddev):
|
||||
"""Computes exact Laplacian kernel value(s) for tensors x and y using stddev.
|
||||
r"""Computes exact Laplacian kernel value(s) for tensors x and y using stddev.
|
||||
|
||||
The Laplacian kernel for vectors u, v is defined as follows:
|
||||
K(u, v) = exp(-||u-v|| / stddev)
|
||||
@ -109,7 +109,7 @@ def exact_laplacian_kernel(x, y, stddev):
|
||||
all (u,v) pairs where u, v are rows from x and y respectively.
|
||||
|
||||
Raises:
|
||||
InvalidShapeError: if the shapes of x, y are not compatible.
|
||||
ValueError: if the shapes of x, y are not compatible.
|
||||
"""
|
||||
x_aligned, y_aligned = _align_matrices(x, y)
|
||||
diff_l1_norm = math_ops.reduce_sum(
|
||||
|
Loading…
Reference in New Issue
Block a user