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

Method testSemiRandomFeatures

caffe2/python/layers_test.py:1873–2041  ·  view source on GitHub ↗
(self, batch_size, input_dims, output_dims, s, scale,
                               set_weight_as_global_constant, use_struct_input)

Source from the content-addressed store, hash-verified

1871 use_struct_input=st.booleans(),
1872 )
1873 def testSemiRandomFeatures(self, batch_size, input_dims, output_dims, s, scale,
1874 set_weight_as_global_constant, use_struct_input):
1875
1876 def _semi_random_hypothesis_test(srf_output, X_full, X_random, rand_w,
1877 rand_b, s):
1878 '''
1879 Runs hypothesis test for Semi Random Features layer.
1880
1881 Inputs:
1882 srf_output -- output of net after running semi random features layer
1883 X_full -- full input data
1884 X_random -- random-output input data
1885 rand_w -- random-initialized weight parameter from train_init_net
1886 rand_b -- random-initialized bias parameter from train_init_net
1887 s -- degree parameter
1888
1889 '''
1890 # Get output from net
1891 net_output = workspace.FetchBlob(srf_output)
1892
1893 # Fetch learned parameter blobs
1894 learned_w = workspace.FetchBlob(self.model.layers[0].learned_w)
1895 learned_b = workspace.FetchBlob(self.model.layers[0].learned_b)
1896
1897 # Computing output directly
1898 x_rand = np.matmul(X_random, np.transpose(rand_w)) + rand_b
1899 x_learn = np.matmul(X_full, np.transpose(learned_w)) + learned_b
1900 x_pow = np.power(x_rand, s)
1901 if s > 0:
1902 h_rand_features = np.piecewise(x_rand,
1903 [x_rand <= 0, x_rand > 0],
1904 [0, 1])
1905 else:
1906 h_rand_features = np.piecewise(x_rand,
1907 [x_rand <= 0, x_rand > 0],
1908 [0, lambda x: x / (1 + x)])
1909 output_ref = np.multiply(np.multiply(x_pow, h_rand_features), x_learn)
1910
1911 # Comparing net output and computed output
1912 npt.assert_allclose(net_output, output_ref, rtol=1e-3, atol=1e-3)
1913
1914 X_full = np.random.normal(size=(batch_size, input_dims)).astype(np.float32)
1915 if use_struct_input:
1916 X_random = np.random.normal(size=(batch_size, input_dims)).\
1917 astype(np.float32)
1918 input_data = [X_full, X_random]
1919 input_record = self.new_record(schema.Struct(
1920 ('full', schema.Scalar(
1921 (np.float32, (input_dims,))
1922 )),
1923 ('random', schema.Scalar(
1924 (np.float32, (input_dims,))
1925 ))
1926 ))
1927 else:
1928 X_random = X_full
1929 input_data = [X_full]
1930 input_record = self.new_record(schema.Scalar(

Callers

nothing calls this directly

Calls 11

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

Tested by

no test coverage detected