Skip to content

💯 Default Values

👉 Setting default values

Some configs are unlikely to ever change. In such cases, a default value can be specified.

The default needs to be propagated to the generated code. Hence, we'll add the default to the protobuf schema.

// default.proto
syntax = "proto3";

package defaults;

import "py_gen_ml/extensions.proto";

// Optimizer configuration
message Optimizer {
    // Optimizer type
    string type = 1 [(pgml.default).string = "sgd"];
    // Learning rate
    float learning_rate = 2 [(pgml.default).float = 0.01];
}

The default value will be added to the generated code.

# Autogenerated code. DO NOT EDIT.
import py_gen_ml as pgml


class Optimizer(pgml.YamlBaseModel):
    """Optimizer configuration"""

    type: str = "sgd"
    """Optimizer type"""

    learning_rate: float = 0.01
    """Learning rate"""

In this case, all values have a default, so it is possible to instantiate the class without specifying any values.

from pgml_out.default_base import Optimizer

optimizer = Optimizer()

🔠 Enums

Enum values can be specified using the name of the enum value.

// default_enum.proto
syntax = "proto3";

package defaults_enum;

import "py_gen_ml/extensions.proto";

// Activation function
enum Activation {
    // Rectified Linear Unit
    RELU = 0;
    // Gaussian Error Linear Unit
    GELU = 1;
}

// Linear layer
message Linear {
    // Number of input features
    int32 in_features = 1;
    // Number of output features
    int32 out_features = 2;
    // Activation function
    Activation activation = 3 [(pgml.default).enum = "GELU"];
}
# Autogenerated code. DO NOT EDIT.
import enum

import py_gen_ml as pgml


class Activation(str, enum.Enum):
    """Activation function"""

    RELU = "RELU"
    """Rectified Linear Unit"""

    GELU = "GELU"
    """Gaussian Error Linear Unit"""


class Linear(pgml.YamlBaseModel):
    """Linear layer"""

    in_features: int
    """Number of input features"""

    out_features: int
    """Number of output features"""

    activation: Activation = Activation.GELU
    """Activation function"""

🚧 Limitations

It is currently only possible to specify defaults for built-ins such as string, float, int, etc. For message fields, you cannot specify a default value. We leave this feature for future work.