Francis Duval
03/21/2024, 1:08 PMimport numpy as np
def softmax(values):
exp_values = np.exp(values)
softmax_values = exp_values / np.sum(exp_values )
return softmax_values
def compute_function(func, values):
return func(values)
compute_function(softmax, [1, 2, 3, 4, 5])
But I want it in a node:
node(
func='compute_function',
inputs=[softmax, 'result_from_other_node'], # This will not work!
outputs='result',
name='name'
)
Thanks!Nok Lam Chan
03/21/2024, 1:13 PMfunc from? Why can't you simply use softmax(value)?Nok Lam Chan
03/21/2024, 1:15 PMFrancis Duval
03/21/2024, 1:15 PMsoftmax and compute_function are defined in nodes.py, and imported in pipeline.py.Nok Lam Chan
03/21/2024, 1:16 PMnode(
func='compute_function',
inputs=[softmax, 'result_from_other_node'],
outputs='result',
name='name'
)
My question is, how is this different from this? Why do you need another indirection function?
node(
func=softmax,
inputs=[ 'result_from_other_node'],
outputs='result',
name='name'
)Francis Duval
03/21/2024, 1:18 PMNok Lam Chan
03/21/2024, 1:19 PMinputs and ouptuts expected datasets or parameters. If you have a higher level function, you should look for functools or something like a decorator. Whatever it is, that should be done prior setting up the node and you pass that function directiont to funcFrancis Duval
03/21/2024, 1:20 PMFrancis Duval
03/21/2024, 1:48 PMpartial and update_wrapper from functools, thanks!Francis Duval
03/21/2024, 1:51 PMcompute_accuracies_nn_weight takes as arguments a weight function weight_func as well as a grid of parameters of this weight function **args_weight_func_grid. So I did this:
def modified_softmax(values, alpha):
amplified_values = values * alpha
exp_amplified_values = np.exp(amplified_values)
softmax_values = exp_amplified_values / np.sum(exp_amplified_values)
return softmax_values
node(
func=update_wrapper(
wrapper=partial(compute_accuracies_nn_weight, weight_func=modified_softmax, alpha=[1, 2, 5, 10, 15]),
wrapped=compute_accuracies_nn_weight
),
inputs={
'df_inference': 'df_inference',
'df_search': 'df_search',
'nn_idx_matrix': 'nn_idx_matrix',
'nn_sim_matrix': 'nn_sim_matrix',
'k_grid': 'params:k_grid',
},
outputs='accuracies_knn_softmax_weights',
name='compute_accuracies_knn_softmax_weights'
),Nok Lam Chan
03/21/2024, 2:01 PMfunc=update_wrapper(
wrapper=partial(compute_accuracies_nn_weight, weight_func=modified_softmax, alpha=[1, 2, 5, 10, 15]),
wrapped=compute_accuracies_nn_weight
)
This should move to node.py most likely. There's nothing wrong to do that in pipeline.py , purely for organising code onlyFrancis Duval
03/21/2024, 2:01 PM