(self, batch_size, input_dims, output_dims, s, scale,
set_weight_as_global_constant)
| 1753 | set_weight_as_global_constant=st.booleans() |
| 1754 | ) |
| 1755 | def testArcCosineFeatureMap(self, batch_size, input_dims, output_dims, s, scale, |
| 1756 | set_weight_as_global_constant): |
| 1757 | |
| 1758 | def _arc_cosine_hypothesis_test(ac_output, X, W, b, s): |
| 1759 | ''' |
| 1760 | Runs hypothesis test for Arc Cosine layer. |
| 1761 | |
| 1762 | Inputs: |
| 1763 | ac_output -- output of net after running arc cosine layer |
| 1764 | X -- input data |
| 1765 | W -- weight parameter from train_init_net |
| 1766 | b -- bias parameter from train_init_net |
| 1767 | s -- degree parameter |
| 1768 | ''' |
| 1769 | # Get output from net |
| 1770 | net_output = workspace.FetchBlob(ac_output) |
| 1771 | |
| 1772 | # Computing output directly |
| 1773 | x_rand = np.matmul(X, np.transpose(W)) + b |
| 1774 | x_pow = np.power(x_rand, s) |
| 1775 | if s > 0: |
| 1776 | h_rand_features = np.piecewise(x_rand, |
| 1777 | [x_rand <= 0, x_rand > 0], |
| 1778 | [0, 1]) |
| 1779 | else: |
| 1780 | h_rand_features = np.piecewise(x_rand, |
| 1781 | [x_rand <= 0, x_rand > 0], |
| 1782 | [0, lambda x: x / (1 + x)]) |
| 1783 | output_ref = np.multiply(x_pow, h_rand_features) |
| 1784 | |
| 1785 | # Comparing net output and computed output |
| 1786 | npt.assert_allclose(net_output, output_ref, rtol=1e-3, atol=1e-3) |
| 1787 | |
| 1788 | X = np.random.normal(size=(batch_size, input_dims)).astype(np.float32) |
| 1789 | input_record = self.new_record(schema.Scalar((np.float32, (input_dims,)))) |
| 1790 | schema.FeedRecord(input_record, [X]) |
| 1791 | input_blob = input_record.field_blobs()[0] |
| 1792 | |
| 1793 | ac_output = self.model.ArcCosineFeatureMap( |
| 1794 | input_record, |
| 1795 | output_dims, |
| 1796 | s=s, |
| 1797 | scale=scale, |
| 1798 | set_weight_as_global_constant=set_weight_as_global_constant |
| 1799 | ) |
| 1800 | self.model.output_schema = schema.Struct() |
| 1801 | self.assertEqual( |
| 1802 | schema.Scalar((np.float32, (output_dims, ))), |
| 1803 | ac_output |
| 1804 | ) |
| 1805 | |
| 1806 | train_init_net, train_net = self.get_training_nets() |
| 1807 | |
| 1808 | # Run create_init_net to initialize the global constants, and W and b |
| 1809 | workspace.RunNetOnce(train_init_net) |
| 1810 | workspace.RunNetOnce(self.model.create_init_net(name='init_net')) |
| 1811 | |
| 1812 | if set_weight_as_global_constant: |
nothing calls this directly
no test coverage detected