https://kedro.org/ logo
#questions
Title
# questions
g

Gilad Rubin

11/25/2023, 5:54 PM
Hey everyone, I'm new to Kedro, but I'm loving it already. I'm currently working through the Kedro starter tutorial and have a question regarding parameter passing in
nodes.py
. In the tutorial, parameters are passed to functions using a
parameters
dictionary, as shown in the
split_data
function:
Copy code
def split_data(data: pd.DataFrame, parameters: Dict) -> Tuple: 
    # function body
    features = parameters["features"]
    ...
My question is: Is it possible to specify function parameters in
nodes.py
directly, instead of using a
params
dictionary? For example, I'd like to directly define parameters like
features
and
test_size
in the function signature, rather than extracting them from a dictionary. This approach would make the function more portable and easier to interact with. Specifically, I'm looking to refactor the function to unpack the dictionary within the node definition itself. Is this a supported pattern in Kedro, or are there any best practices or considerations I should be aware of when implementing this? Any guidance or examples would be greatly appreciated! Thank you!
K 1
w

William Caicedo

11/25/2023, 8:36 PM
If I understood correctly, this is what you want:
Copy code
# in parameters.yml
step_size: 1
learning_rate: 0.01

# in node definition
def increase_volume(volume, step):
    return volume + step


# in pipeline definition
node(
    func=increase_volume,
    inputs=["input_volume", "params:step_size"],
    outputs="output_volume",
)
https://docs.kedro.org/en/0.18.8/configuration/parameters.html#how-to-use-parameters
g

Gilad Rubin

11/26/2023, 8:12 AM
Yes, this is what I was looking for. What is the syntax for accessing nested parameters? for example, if I have: mode_options: step_size : 3
w

William Caicedo

11/26/2023, 9:20 AM
Copy code
# in parameters.yml
model_params:
    learning_rate: 0.01
    test_data_ratio: 0.2
    number_of_train_iterations: 10000

# in node definition
def train_model(data, model):
    lr = model["learning_rate"]
    test_data_ratio = model["test_data_ratio"]
    iterations = model["number_of_train_iterations"]
    ...


# in pipeline definition
node(
    func=train_model,
    inputs=["input_data", "params:model_params"],
    outputs="output_data",
)
g

Gilad Rubin

11/26/2023, 9:23 AM
Thanks @William Caicedo I'm wondering if there's a way to directly access "learning_rate" when it's nested within model_params. e.g.
Copy code
# in parameters.yml
model_params:
    learning_rate: 0.01
    test_data_ratio: 0.2
    number_of_train_iterations: 10000

# in node definition
def train_model(data, model, lr):
    test_data_ratio = model["test_data_ratio"]
    iterations = model["number_of_train_iterations"]
    ...


# in pipeline definition
node(
    func=train_model,
    inputs=["input_data", "params:model_params:learning_rate"], #this doesn't work
    outputs="output_data",
)
j

Juan Luis

11/26/2023, 12:22 PM
have you tried
params:model_params.learning_rate
@Gilad Rubin?
(with
.
instead of
:
in the accessor)
🥳 1
K 1
g

Gilad Rubin

11/26/2023, 1:16 PM
I haven't. Now it works, thanks!
🙌🏼 1
f

fmfreeze

12/12/2023, 9:59 PM
A nested parameter accessed via
params:model_params.learning_rate
is not showing up properly in kedro viz. Is that on purpose?