MCPcopy Create free account
hub / github.com/pytorch/pytorch / testRandomFourierFeatures

Method testRandomFourierFeatures

caffe2/python/layers_test.py:1678–1745  ·  view source on GitHub ↗
(self, batch_size, input_dims, output_dims, bandwidth)

Source from the content-addressed store, hash-verified

1676 bandwidth=st.floats(min_value=0.1, max_value=5),
1677 )
1678 def testRandomFourierFeatures(self, batch_size, input_dims, output_dims, bandwidth):
1679
1680 def _rff_hypothesis_test(rff_output, X, W, b, scale):
1681 '''
1682 Runs hypothesis test for Semi Random Features layer.
1683
1684 Inputs:
1685 rff_output -- output of net after running random fourier features layer
1686 X -- input data
1687 W -- weight parameter from train_init_net
1688 b -- bias parameter from train_init_net
1689 scale -- value by which to scale the output vector
1690 '''
1691 output = workspace.FetchBlob(rff_output)
1692 output_ref = scale * np.cos(np.dot(X, np.transpose(W)) + b)
1693 npt.assert_allclose(output, output_ref, rtol=1e-3, atol=1e-3)
1694
1695 X = np.random.random((batch_size, input_dims)).astype(np.float32)
1696 scale = np.sqrt(2.0 / output_dims)
1697 input_record = self.new_record(schema.Scalar((np.float32, (input_dims,))))
1698 schema.FeedRecord(input_record, [X])
1699 input_blob = input_record.field_blobs()[0]
1700 rff_output = self.model.RandomFourierFeatures(input_record,
1701 output_dims,
1702 bandwidth)
1703 self.model.output_schema = schema.Struct()
1704
1705 self.assertEqual(
1706 schema.Scalar((np.float32, (output_dims, ))),
1707 rff_output
1708 )
1709
1710 train_init_net, train_net = self.get_training_nets()
1711
1712 # Init net assertions
1713 init_ops_list = [
1714 OpSpec("GaussianFill", None, None),
1715 OpSpec("UniformFill", None, None),
1716 ]
1717 init_ops = self._test_net(train_init_net, init_ops_list)
1718 W = workspace.FetchBlob(self.model.layers[0].w)
1719 b = workspace.FetchBlob(self.model.layers[0].b)
1720
1721 # Operation specifications
1722 fc_spec = OpSpec("FC", [input_blob, init_ops[0].output[0],
1723 init_ops[1].output[0]], None)
1724 cosine_spec = OpSpec("Cos", None, None)
1725 scale_spec = OpSpec("Scale", None, rff_output.field_blobs(),
1726 {'scale': scale})
1727 ops_list = [
1728 fc_spec,
1729 cosine_spec,
1730 scale_spec
1731 ]
1732
1733 # Train net assertions
1734 self._test_net(train_net, ops_list)
1735 _rff_hypothesis_test(rff_output(), X, W, b, scale)

Callers

nothing calls this directly

Calls 10

_test_netMethod · 0.95
OpSpecClass · 0.90
astypeMethod · 0.80
new_recordMethod · 0.80
get_training_netsMethod · 0.80
get_eval_netMethod · 0.80
get_predict_netMethod · 0.80
sqrtMethod · 0.45
field_blobsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected