def test_l1_l2_scale_l2Zero(self):
   shape = [5, 5, 5]
   num_elem = 5 * 5 * 5
   tensor = constant_op.constant(1.0, shape=shape)
   loss = regularizers.l1_l2_regularizer(1.0, 0.0)(tensor)
   with self.cached_session():
     self.assertEquals(loss.op.name, 'l1_l2_regularizer')
     self.assertAlmostEqual(loss.eval(), num_elem, 5)
 def testL1L2RegularizerWithScope(self):
   with self.cached_session():
     shape = [5, 5, 5]
     num_elem = 5 * 5 * 5
     tensor = constant_op.constant(1.0, shape=shape)
     with ops.name_scope('foo'):
       loss = regularizers.l1_l2_regularizer(1.0, 1.0, scope='l1_l2')(tensor)
     self.assertEquals(loss.op.name, 'foo/l1_l2')
     self.assertAlmostEqual(loss.eval(), num_elem + num_elem / 2, 5)
  def test_l1_l2(self):
    with self.assertRaises(ValueError):
      regularizers.l1_l2_regularizer(-1., 0.5)
    with self.assertRaises(ValueError):
      regularizers.l1_l2_regularizer(0.5, -1.)
    with self.assertRaises(ValueError):
      regularizers.l1_l2_regularizer(0, 0.5)
    with self.assertRaises(ValueError):
      regularizers.l1_l2_regularizer(0.5, 0)

    with self.cached_session():
      shape = [5, 5, 5]
      num_elem = 5 * 5 * 5
      tensor = constant_op.constant(1.0, shape=shape)
      loss = regularizers.l1_l2_regularizer(1.0, 1.0)(tensor)
      self.assertEquals(loss.op.name, 'l1_l2_regularizer')
      self.assertAlmostEqual(loss.eval(), num_elem + num_elem / 2, 5)
 def test_l1_l2_scales_Zero(self):
   shape = [5, 5, 5]
   tensor = constant_op.constant(1.0, shape=shape)
   loss = regularizers.l1_l2_regularizer(0.0, 0.0)(tensor)
   self.assertEquals(loss, None)