{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.SageMaker.Types.HyperbandStrategyConfig
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.SageMaker.Types.HyperbandStrategyConfig where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | The configuration for @Hyperband@, a multi-fidelity based hyperparameter
-- tuning strategy. @Hyperband@ uses the final and intermediate results of
-- a training job to dynamically allocate resources to utilized
-- hyperparameter configurations while automatically stopping
-- under-performing configurations. This parameter should be provided only
-- if @Hyperband@ is selected as the @StrategyConfig@ under the
-- @HyperParameterTuningJobConfig@ API.
--
-- /See:/ 'newHyperbandStrategyConfig' smart constructor.
data HyperbandStrategyConfig = HyperbandStrategyConfig'
  { -- | The maximum number of resources (such as epochs) that can be used by a
    -- training job launched by a hyperparameter tuning job. Once a job reaches
    -- the @MaxResource@ value, it is stopped. If a value for @MaxResource@ is
    -- not provided, and @Hyperband@ is selected as the hyperparameter tuning
    -- strategy, @HyperbandTrainingJ@ attempts to infer @MaxResource@ from the
    -- following keys (if present) in
    -- <https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html#sagemaker-Type-HyperParameterTrainingJobDefinition-StaticHyperParameters StaticsHyperParameters>:
    --
    -- -   @epochs@
    --
    -- -   @numepochs@
    --
    -- -   @n-epochs@
    --
    -- -   @n_epochs@
    --
    -- -   @num_epochs@
    --
    -- If @HyperbandStrategyConfig@ is unable to infer a value for
    -- @MaxResource@, it generates a validation error. The maximum value is
    -- 20,000 epochs. All metrics that correspond to an objective metric are
    -- used to derive
    -- <https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-early-stopping.html early stopping decisions>.
    -- For
    -- <https://docs.aws.amazon.com/sagemaker/latest/dg/distributed-training.html distributive>
    -- training jobs, ensure that duplicate metrics are not printed in the logs
    -- across the individual nodes in a training job. If multiple nodes are
    -- publishing duplicate or incorrect metrics, training jobs may make an
    -- incorrect stopping decision and stop the job prematurely.
    HyperbandStrategyConfig -> Maybe Natural
maxResource :: Prelude.Maybe Prelude.Natural,
    -- | The minimum number of resources (such as epochs) that can be used by a
    -- training job launched by a hyperparameter tuning job. If the value for
    -- @MinResource@ has not been reached, the training job will not be stopped
    -- by @Hyperband@.
    HyperbandStrategyConfig -> Maybe Natural
minResource :: Prelude.Maybe Prelude.Natural
  }
  deriving (HyperbandStrategyConfig -> HyperbandStrategyConfig -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HyperbandStrategyConfig -> HyperbandStrategyConfig -> Bool
$c/= :: HyperbandStrategyConfig -> HyperbandStrategyConfig -> Bool
== :: HyperbandStrategyConfig -> HyperbandStrategyConfig -> Bool
$c== :: HyperbandStrategyConfig -> HyperbandStrategyConfig -> Bool
Prelude.Eq, ReadPrec [HyperbandStrategyConfig]
ReadPrec HyperbandStrategyConfig
Int -> ReadS HyperbandStrategyConfig
ReadS [HyperbandStrategyConfig]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [HyperbandStrategyConfig]
$creadListPrec :: ReadPrec [HyperbandStrategyConfig]
readPrec :: ReadPrec HyperbandStrategyConfig
$creadPrec :: ReadPrec HyperbandStrategyConfig
readList :: ReadS [HyperbandStrategyConfig]
$creadList :: ReadS [HyperbandStrategyConfig]
readsPrec :: Int -> ReadS HyperbandStrategyConfig
$creadsPrec :: Int -> ReadS HyperbandStrategyConfig
Prelude.Read, Int -> HyperbandStrategyConfig -> ShowS
[HyperbandStrategyConfig] -> ShowS
HyperbandStrategyConfig -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HyperbandStrategyConfig] -> ShowS
$cshowList :: [HyperbandStrategyConfig] -> ShowS
show :: HyperbandStrategyConfig -> String
$cshow :: HyperbandStrategyConfig -> String
showsPrec :: Int -> HyperbandStrategyConfig -> ShowS
$cshowsPrec :: Int -> HyperbandStrategyConfig -> ShowS
Prelude.Show, forall x. Rep HyperbandStrategyConfig x -> HyperbandStrategyConfig
forall x. HyperbandStrategyConfig -> Rep HyperbandStrategyConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep HyperbandStrategyConfig x -> HyperbandStrategyConfig
$cfrom :: forall x. HyperbandStrategyConfig -> Rep HyperbandStrategyConfig x
Prelude.Generic)

-- |
-- Create a value of 'HyperbandStrategyConfig' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'maxResource', 'hyperbandStrategyConfig_maxResource' - The maximum number of resources (such as epochs) that can be used by a
-- training job launched by a hyperparameter tuning job. Once a job reaches
-- the @MaxResource@ value, it is stopped. If a value for @MaxResource@ is
-- not provided, and @Hyperband@ is selected as the hyperparameter tuning
-- strategy, @HyperbandTrainingJ@ attempts to infer @MaxResource@ from the
-- following keys (if present) in
-- <https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html#sagemaker-Type-HyperParameterTrainingJobDefinition-StaticHyperParameters StaticsHyperParameters>:
--
-- -   @epochs@
--
-- -   @numepochs@
--
-- -   @n-epochs@
--
-- -   @n_epochs@
--
-- -   @num_epochs@
--
-- If @HyperbandStrategyConfig@ is unable to infer a value for
-- @MaxResource@, it generates a validation error. The maximum value is
-- 20,000 epochs. All metrics that correspond to an objective metric are
-- used to derive
-- <https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-early-stopping.html early stopping decisions>.
-- For
-- <https://docs.aws.amazon.com/sagemaker/latest/dg/distributed-training.html distributive>
-- training jobs, ensure that duplicate metrics are not printed in the logs
-- across the individual nodes in a training job. If multiple nodes are
-- publishing duplicate or incorrect metrics, training jobs may make an
-- incorrect stopping decision and stop the job prematurely.
--
-- 'minResource', 'hyperbandStrategyConfig_minResource' - The minimum number of resources (such as epochs) that can be used by a
-- training job launched by a hyperparameter tuning job. If the value for
-- @MinResource@ has not been reached, the training job will not be stopped
-- by @Hyperband@.
newHyperbandStrategyConfig ::
  HyperbandStrategyConfig
newHyperbandStrategyConfig :: HyperbandStrategyConfig
newHyperbandStrategyConfig =
  HyperbandStrategyConfig'
    { $sel:maxResource:HyperbandStrategyConfig' :: Maybe Natural
maxResource =
        forall a. Maybe a
Prelude.Nothing,
      $sel:minResource:HyperbandStrategyConfig' :: Maybe Natural
minResource = forall a. Maybe a
Prelude.Nothing
    }

-- | The maximum number of resources (such as epochs) that can be used by a
-- training job launched by a hyperparameter tuning job. Once a job reaches
-- the @MaxResource@ value, it is stopped. If a value for @MaxResource@ is
-- not provided, and @Hyperband@ is selected as the hyperparameter tuning
-- strategy, @HyperbandTrainingJ@ attempts to infer @MaxResource@ from the
-- following keys (if present) in
-- <https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html#sagemaker-Type-HyperParameterTrainingJobDefinition-StaticHyperParameters StaticsHyperParameters>:
--
-- -   @epochs@
--
-- -   @numepochs@
--
-- -   @n-epochs@
--
-- -   @n_epochs@
--
-- -   @num_epochs@
--
-- If @HyperbandStrategyConfig@ is unable to infer a value for
-- @MaxResource@, it generates a validation error. The maximum value is
-- 20,000 epochs. All metrics that correspond to an objective metric are
-- used to derive
-- <https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-early-stopping.html early stopping decisions>.
-- For
-- <https://docs.aws.amazon.com/sagemaker/latest/dg/distributed-training.html distributive>
-- training jobs, ensure that duplicate metrics are not printed in the logs
-- across the individual nodes in a training job. If multiple nodes are
-- publishing duplicate or incorrect metrics, training jobs may make an
-- incorrect stopping decision and stop the job prematurely.
hyperbandStrategyConfig_maxResource :: Lens.Lens' HyperbandStrategyConfig (Prelude.Maybe Prelude.Natural)
hyperbandStrategyConfig_maxResource :: Lens' HyperbandStrategyConfig (Maybe Natural)
hyperbandStrategyConfig_maxResource = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HyperbandStrategyConfig' {Maybe Natural
maxResource :: Maybe Natural
$sel:maxResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
maxResource} -> Maybe Natural
maxResource) (\s :: HyperbandStrategyConfig
s@HyperbandStrategyConfig' {} Maybe Natural
a -> HyperbandStrategyConfig
s {$sel:maxResource:HyperbandStrategyConfig' :: Maybe Natural
maxResource = Maybe Natural
a} :: HyperbandStrategyConfig)

-- | The minimum number of resources (such as epochs) that can be used by a
-- training job launched by a hyperparameter tuning job. If the value for
-- @MinResource@ has not been reached, the training job will not be stopped
-- by @Hyperband@.
hyperbandStrategyConfig_minResource :: Lens.Lens' HyperbandStrategyConfig (Prelude.Maybe Prelude.Natural)
hyperbandStrategyConfig_minResource :: Lens' HyperbandStrategyConfig (Maybe Natural)
hyperbandStrategyConfig_minResource = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HyperbandStrategyConfig' {Maybe Natural
minResource :: Maybe Natural
$sel:minResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
minResource} -> Maybe Natural
minResource) (\s :: HyperbandStrategyConfig
s@HyperbandStrategyConfig' {} Maybe Natural
a -> HyperbandStrategyConfig
s {$sel:minResource:HyperbandStrategyConfig' :: Maybe Natural
minResource = Maybe Natural
a} :: HyperbandStrategyConfig)

instance Data.FromJSON HyperbandStrategyConfig where
  parseJSON :: Value -> Parser HyperbandStrategyConfig
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"HyperbandStrategyConfig"
      ( \Object
x ->
          Maybe Natural -> Maybe Natural -> HyperbandStrategyConfig
HyperbandStrategyConfig'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"MaxResource")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"MinResource")
      )

instance Prelude.Hashable HyperbandStrategyConfig where
  hashWithSalt :: Int -> HyperbandStrategyConfig -> Int
hashWithSalt Int
_salt HyperbandStrategyConfig' {Maybe Natural
minResource :: Maybe Natural
maxResource :: Maybe Natural
$sel:minResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
$sel:maxResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
maxResource
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
minResource

instance Prelude.NFData HyperbandStrategyConfig where
  rnf :: HyperbandStrategyConfig -> ()
rnf HyperbandStrategyConfig' {Maybe Natural
minResource :: Maybe Natural
maxResource :: Maybe Natural
$sel:minResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
$sel:maxResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
maxResource
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
minResource

instance Data.ToJSON HyperbandStrategyConfig where
  toJSON :: HyperbandStrategyConfig -> Value
toJSON HyperbandStrategyConfig' {Maybe Natural
minResource :: Maybe Natural
maxResource :: Maybe Natural
$sel:minResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
$sel:maxResource:HyperbandStrategyConfig' :: HyperbandStrategyConfig -> Maybe Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"MaxResource" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
maxResource,
            (Key
"MinResource" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
minResource
          ]
      )