如何在不使用eval的情况下从符号获取函数? [英] How to get a function from a symbol without using eval?

查看:68
本文介绍了如何在不使用eval的情况下从符号获取函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个符号,代表要调用的函数的名称:

I've got a symbol that represents the name of a function to be called:

julia> func_sym = :tanh

我可以使用该符号来获取tanh函数并使用以下命令进行调用:

I can use that symbol to get the tanh function and call it using:

julia> eval(func_sym)(2)
0.9640275800758169

但是我宁愿避免在那里使用"eval",因为它会被多次调用并且价格昂贵(根据上下文,func_sym可以有几个不同的值).

But I'd rather avoid the 'eval' there as it will be called many times and it's expensive (and func_sym can have several different values depending on context).

在Ruby中的IIRC,您可以这样说:

IIRC in Ruby you can say something like:

obj.send(func_sym, args)

朱莉娅有类似的东西吗?

Is there something similar in Julia?

关于为什么我有用符号表示的功能的更多详细信息:

some more details on why I have functions represented by symbols:

我有一个类型(来自神经网络),其中包括激活函数,最初我将其作为函数包括在内:

I have a type (from a neural network) that includes the activation function, originally I included it as a funcion:

type NeuralLayer
  weights::Matrix{Float32}
  biases::Vector{Float32}
  a_func::Function
end

但是,我需要使用JLD将这些内容序列化到文件中,但是无法序列化Function,所以我使用了一个符号:

However, I needed to serialize these things to files using JLD, but it's not possible to serialize a Function, so I went with a symbol:

type NeuralLayer
  weights::Matrix{Float32}
  biases::Vector{Float32}
  a_func::Symbol
end

当前,我使用上面的评估方法来调用激活函数.有很多NeuralLayers集合,每个集合都有自己的激活功能.

And currently I use the eval approach above to call the activation function. There are collections of NeuralLayers and each can have it's own activation function.

推荐答案

@以赛亚书的答案很明确;甚至对原始问题进行编辑后也是如此.要详细说明并使其更适合您的情况,请执行以下操作:我将您的NeuralLayer类型更改为参数类型:

@Isaiah's answer is spot-on; perhaps even more-so after the edit to the original question. To elaborate and make this more specific to your case: I'd change your NeuralLayer type to be parametric:

type NeuralLayer{func_type}
  weights::Matrix{Float32}
  biases::Vector{Float32}
end

由于func_type没有出现在字段的类型中,因此构造函数将要求您显式指定它:layer = NeuralLayer{:excitatory}(w, b).这里的一个限制是您不能修改类型参数.

Since func_type doesn't appear in the types of the fields, the constructor will require you to explicitly specify it: layer = NeuralLayer{:excitatory}(w, b). One restriction here is that you cannot modify a type parameter.

现在,func_type可能是一个符号(就像您现在正在做的那样),或者它可能是功能更相关的参数(或多个参数),可以调节您的激活功能.然后,您可以定义激活函数,如下所示:

Now, func_type could be a symbol (like you're doing now) or it could be a more functionally relevant parameter (or parameters) that tunes your activation function. Then you define your activation functions like this:

# If you define your NeuralLayer with just one parameter:
activation(layer::NeuralLayer{:inhibitory}) = …
activation(layer::NeuralLayer{:excitatory}) = …
# Or if you want to use several physiological parameters instead:
activation{g_K,g_Na,g_l}(layer::NeuralLayer{g_K,g_Na,g_l} = f(g_K, g_Na, g_l)

关键是功能和行为是数据的外部.使用类型定义和抽象类型层次结构来定义行为,就像在外部函数中编码的一样……但是只能将数据本身存储在类型中.这与Python或其他高度面向对象的范例截然不同,并且需要一些时间来习惯.

The key point is that functions and behavior are external to the data. Use type definitions and abstract type hierarchies to define behavior, as is coded in the external functions… but only store data itself in the types. This is dramatically different from Python or other strongly object-oriented paradigms, and it takes some getting used to.

这篇关于如何在不使用eval的情况下从符号获取函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆