Hylke
Hylke
MModular
Created by Hylke on 9/22/2024 in #questions
How to define nested dict function
(This is a shortened cross post of my stackoverflow question). How do I define a function in Mojo where the argument or return value is a nested structure, like a dictionary. But where the exact structure is not known at compile time because, for example, it is read from disk. For example, how do I type annotate params in the function neural_network to represent a nested dict to keep the model parameters (i.e., a pytree).
network_params = {
'linear1': {'weights': Tensor(...), 'bias': 1.0},
...
}

fn neural_network(params: Dict, x: Tensor) -> Float64:
x = params['linear1']['weights'] @ x + params['linear1']['bias']
...
return x

x_input = Tensor(...)
y = neural_network(network_params, x_input)
network_params = {
'linear1': {'weights': Tensor(...), 'bias': 1.0},
...
}

fn neural_network(params: Dict, x: Tensor) -> Float64:
x = params['linear1']['weights'] @ x + params['linear1']['bias']
...
return x

x_input = Tensor(...)
y = neural_network(network_params, x_input)
Thanks so much!
6 replies
MModular
Created by Hylke on 5/9/2024 in #questions
Mojo test
Did anyone get unit tests working? I run mojo test on the following folder structure:
├── my_package
│   ├── __init__.:fire:
│   └── tensor.:fire:
└── test
├── __init__.:fire:
└── test_tensor.:fire:
├── my_package
│   ├── __init__.:fire:
│   └── tensor.:fire:
└── test
├── __init__.:fire:
└── test_tensor.:fire:
But it doesn't pick up my tests Total Discovered Tests: 0. The test_tensor.🔥 contains a function:
def test_transformation():
...
def test_transformation():
...
As described in the docs: https://docs.modular.com/mojo/tools/testing Any suggestions?
7 replies