我如何在Objective-C的运行时创建函数 [英] How do I create a function at runtime in Objective-C

查看:99
本文介绍了我如何在Objective-C的运行时创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以现在来晚了,我的Google技能似乎让我失望了.我之前(一次又一次)在SO上找到了一些很好的回应,我想你们可以帮忙.

So it's late here, and my google skills seem to be failing me. I've found some great responses on SO before (time and time again), I thought you guys could help.

我有一个神经网络,试图在本机Objective-C中运行.它可以工作,但是速度太慢.这些网络不是经常性的.每个网络我运行大约20,000次(128x80次,或大约20,000次).问题在于这些网络实际上只是归结为数学函数(每个网络都是4维函数,以x,y,dist(x,y)和bias为输入,并输出3个值).

I have a neural network I'm trying to run in native objective-c. It works, but it's too slow. These networks are not recurrent. Each network I run about 20,000 times ( 128x80 times, or around that). The problem is these networks really just boil down to math functions (each network is a 4 dimensional function, taking x,y,dist(x,y),and bias as inputs, and outputting 3 values).

我想做的是将每个网络(仅一次)转换为一个函数调用,或在运行时在Objective-c中转换为一块代码.

What I want to do is convert each network (only once) into a function call, or a block of code at runtime in objective-c.

我该怎么做?我可以编写一大串需要执行的数学运算,但是如何执行该字符串,或者将该字符串转换为代码块以进行执行呢?

How do I do this? I could make a big string of the math operations that need to be performed, but how do I go about executing that string, or converting the string into a block of code for execution?

同样,我的深夜搜索未能使我成功,如果已得到答复,对不起.任何帮助是极大的赞赏.

Again, my late night search failed me, so sorry if this has already been answered. Any help is greatly appreciated.

-保罗

啊哈!巨大的成功!将近24小时后,我有了工作代码,可以将最多4个输入的神经网络转换为一个4维函数.我在回答中使用了Dave DeLong建议的阻止方法.

Aha! Great success! Nearly 24 hours later, I have working code to turn a neural network with up to 4 inputs into a single 4 dimensional function. I used the block method suggested by Dave DeLong in the answers.

对于任何想跟随我将来所做的事情的人,这里是我所做的(快速)细分(如果这对stackoverflow而言是不正确的礼节,敬请原谅): 首先,我为不同的块函数做了一些typedef:

For anybody who ever wants to follow what I've done in the future, here is a (quick) breakdown of what I did (excuse me if this is incorrect etiquette on stackoverflow): First, I made a few typedef's for the different block functions:

typedef CGFloat (^oneDFunction)(CGFloat x);
typedef CGFloat (^twoDFunction)(CGFloat x, CGFloat y);
typedef CGFloat (^threeDFunction)(CGFloat x, CGFloat y, CGFloat z);
typedef CGFloat (^fourDFunction)(CGFloat x, CGFloat y, CGFloat z, CGFloat w);

oneDFunction采取f(x)的形式,twoD为f(x,y)等.然后,我制作了将两个fourDFunction块组合在一起的函数(和2个oneD,2个twoD等,尽管这些不是必需的) .

A oneDFunction takes the form of f(x), twoD is f(x,y), etc. Then I made functions to combine two fourDFunction blocks (and 2 oneD, 2 twoD, etc, although these were not necessary).

fourDFunction (^combineFourD) (fourDFunction f1, fourDFunction f2) =
  ^(fourDFunction f1,     fourDFunction f2){
    fourDFunction blockToCopy = ^(CGFloat x, CGFloat y, CGFloat z, CGFloat w){
        return f1(x,y,z,w) + f2(x,y,z,w);
    };
    fourDFunction act = [blockToCopy copy];
    [f1 release];
    [f2 release];
    //Need to release act at some point
    return act;            
};

当然,我需要将激活函数应用于每个节点的fourD函数,并且对于每个节点,我需要乘以连接它的权重:

And, of course, I needed to apply the activation function to the fourD function for every node, and for each node, I would need to multiply by the weight connecting it:

//for applying the activation function
fourDFunction (^applyOneToFourD)( oneDFunction f1, fourDFunction f2) = 
^(oneDFunction f1, fourDFunction f2){
    fourDFunction blockToCopy = ^(CGFloat x, CGFloat y, CGFloat z, CGFloat w){
        return f1(f2(x,y,z,w));
    };    

    fourDFunction act = [blockToCopy copy];
    [f1 release];
    [f2 release];

    //Need to release act at some point
    return act; 

};

//For applying the weight to the function
fourDFunction (^weightCombineFour) (CGFloat x, fourDFunction f1) =
 ^(CGFloat weight, fourDFunction f1)
{
    fourDFunction blockToCopy = ^(CGFloat x, CGFloat y, CGFloat z, CGFloat w){

        return weight*f1(x,y,z,w);
    };

    fourDFunction act = [blockToCopy copy];
    [f1 release];
    //[act release];
    //Need to release act at some point
   return act;

};

然后,对于网络中的每个节点,我仅将激活函数应用于源神经元的fourD函数之和乘以它们的连接权重. 组成所有这些块之后,我从每个输出中获取了最终功能.因此,我的输出是输入的单独4D功能.

Then, for each node in the network, I simply applied the activation function to the sum of the fourD functions from the source neurons multiplied by their connection weight. After composing all those blocks, I took the final functions from each output. Therefore, my outputs are separate 4D functions of the inputs.

感谢您的帮助,这非常酷.

Thanks for the help, this was very cool.

推荐答案

您可以使用

然后在其他地方...

And then elsewhere...

int (^retrievedBlock)(int) = [self retrieveBlockWithName:@"myBlock"];
int theAnswer = retrievedBlock(2);  //theAnswer is 4536

如果您有一个表示要评估的数学表达式的字符串,则可以签出 GCMathParser (快速但不能或我自己的 DDMathParser (速度较慢但可扩展).

If you have a string representing some math to evaluate, you could check out GCMathParser (fast but not extensible) or my own DDMathParser (slower but extensible).

这篇关于我如何在Objective-C的运行时创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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