def testSoftmaxAxes(self): arr = np.linspace(0., 1, 12).reshape(3, 4) x_neg_axis = nn_ops.softmax_v2(arr, axis=-2) y_pos_axis = nn_ops.softmax_v2(arr, axis=0) z_gt_axis = nn_ops.softmax_v2(arr, axis=0) x_neg_axis_tf = self.evaluate(x_neg_axis) y_pos_axis_tf = self.evaluate(y_pos_axis) z_gt_axis_tf = self.evaluate(z_gt_axis) eps = 1e-3 self.assertAllClose(x_neg_axis_tf, y_pos_axis_tf, eps) self.assertAllClose(y_pos_axis_tf, z_gt_axis_tf, eps)
def testSoftmax(self): x_shape = [5, 10] x_np = np.random.randn(*x_shape).astype(np.float32) y_np = self._softmax(x_np) x_tf = constant_op.constant(x_np) y_tf = nn_ops.softmax_v2(x_tf) y_tf_last_dim = nn_ops.softmax_v2(x_tf, 1) y_tf_np = self.evaluate(y_tf) y_tf_last_dim_np = self.evaluate(y_tf_last_dim) eps = 1e-3 self.assertAllClose(y_tf_np, y_np, eps) self.assertAllClose(y_tf_last_dim_np, y_np, eps)
def testGradient(self, x_shape): x_np = np.random.randn(*x_shape).astype(np.float64) with self.cached_session(): x_tf = constant_op.constant(x_np) y_tf = nn_ops.softmax_v2(x_tf) err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf, x_shape) eps = 2e-8 self.assertLess(err, eps)
def testShortTensors(self): eps = 1e-5 x_list = [[], [1]] x_row_matrices = [[row] for row in x_list] y_row_matrices = [ self._softmax(np.array(row_matrix)).tolist() for row_matrix in x_row_matrices ] y_list = [row_matrix[0] for row_matrix in y_row_matrices] y_expected_from_numpy = ragged_factory_ops.constant( y_list, dtype=dtypes.float32) x_tf = ragged_factory_ops.constant(x_list, dtype=dtypes.float32) y_tf = nn_ops.softmax_v2(x_tf) self.assertAllClose(y_tf, y_expected_from_numpy, eps)
def testOrdinaryValues(self): eps = 1e-5 x_list = [np.log([0.5, 0.25, 0.25]), np.log([0.5, 0.5])] x_row_matrices = [[row] for row in x_list] y_row_matrices = [ self._softmax(np.array(row_matrix)).tolist() for row_matrix in x_row_matrices ] y_list = [row_matrix[0] for row_matrix in y_row_matrices] y_expected_from_numpy = ragged_factory_ops.constant( y_list, dtype=dtypes.float32) y_expected = ragged_factory_ops.constant([[0.5, 0.25, 0.25], [0.5, 0.5]], dtype=dtypes.float32) self.assertAllClose(y_expected_from_numpy, y_expected, eps) x_tf = ragged_factory_ops.constant(x_list, dtype=dtypes.float32) y_tf = nn_ops.softmax_v2(x_tf) self.assertAllClose(y_tf, y_expected_from_numpy, eps)