如何在属性中使用反射来检索有关调用它的类的信息(How to use reflection inside an attribute to retrieve information about the class that called it)
我遇到以下问题,
我得到了一个'Car'类,该函数使用EntityFramework访问数据库,并通过几个过滤器获取所有表格行。
像这样的东西:
public class car { private const string VEHICLE_TYPE = "something_here"; [CachedAttribute(30)] public virtual List<Vehicles> GetVehiclesFromDB() { return _repository.Set<Vehicles>().Where(e => e.VehicleType == VEHICLE_TYPE && e.Weight < CAR_MAX_WEIGHT); } }
我还在其上添加了一个属性。 在这个属性里我需要能够检索一些东西,
我需要获取类名(本例中是car),我还需要获取函数返回值的类型(在本例中为对象Vehicles),并且需要在VEHICLE_TYPE中获取值。
之后,我需要能够从属性内执行该功能('GetVehiclesFromDB')
public class CachedAttribute : Attribute { public CachedAttribute(int seconds) { // todo } }
我从来没有做过这样的事情,我不知道我应该采取什么样的方法。 这是一种反思吗? 或者它可能完全不同?
编辑:
我确实需要使用这个缓存,我想使用Redis。
I got the following problem,
I got a class 'Car' with a function that uses EntityFramework to reach the Database and gets all the table rows by a few filters.
Something like this :
public class car { private const string VEHICLE_TYPE = "something_here"; [CachedAttribute(30)] public virtual List<Vehicles> GetVehiclesFromDB() { return _repository.Set<Vehicles>().Where(e => e.VehicleType == VEHICLE_TYPE && e.Weight < CAR_MAX_WEIGHT); } }
I also added an attribute on top of it. Inside this attribute i need to be able to retrieve a few things,
I need to get the class name( in this case car), I also need to get the type of the returned value of the function ( in this case the object Vehicles) and i need to get the value inside VEHICLE_TYPE .
After that i need to be able to execute that function ('GetVehiclesFromDB') from inside the attribute
public class CachedAttribute : Attribute { public CachedAttribute(int seconds) { // todo } }
I never done those sort of things and i am not sure what is the approach i should take. Is it a case of reflection? or maybe it is something completely different?
Edit :
I indeed need to use this for cache, i want to use Redis for that.
原文:https://stackoverflow.com/questions/42503569
最满意答案
我写了关于这个话题的硕士论文:
托马马丁。 “ 卷积神经网络结构的分析和优化 ”。 arXiv预印本arXiv:1707.09725(2017)。
长话短说:有几种分析技术(第2.5章)和学习topoligies的算法(第3章),但实际上它主要是试错法和直觉。
I wrote my masters thesis about the topic:
Thoma, Martin. "Analysis and Optimization of Convolutional Neural Network Architectures." arXiv preprint arXiv:1707.09725 (2017).
Long story short: There are a couple of techniques for analysis (chapter 2.5) and algorithms that learn topoligies (chapter 3), but in practice it is mostly trial and error / gut feeling.
相关问答
更多-
如果你使用冲浪功能,这意味着一个漂浮矢量[128]或[64]取决于你的冲浪配置你将设置神经网络如下 - 使用模型创建数据库: -bikes -cars -autobus -truck - 拍摄每种类型物体的照片,例如10张照片模型关闭汽车,10张照片模型关闭自行车10张照片,不同模型从卡车等等,到每张照片提取其每个物体的冲浪特征向量。 - 每种类型的对象都将代表神经网络中的一类对象; -car ;object class 1 =binary representaation in 4 bits= 0 0 ...
-
这里是一个很好的例子: Brainnet 1 - 神经网络项目 - 带插图和代码 - 学习神经网络编程一步一步开发一个简单的手写检测系统 ,将演示神经网络编程的一些实际用途。 Here is good example: Brainnet 1 - A Neural Netwok Project - With Illustration And Code - Learn Neural Network Programming Step By Step And Develop a Simple Handwriting ...
-
如何训练人造神经网络使用视觉输入来玩Diablo 2?(How to train an artificial neural network to play Diablo 2 using visual input?)[2023-12-15]
我可以看到你担心如何训练ANN,但这个项目隐藏着你可能不了解的复杂性 。 通过图像处理对计算机游戏进行对象/角色识别是一项非常具有挑战性的任务(对于FPS和RPG游戏来说,这并不奇怪 )。 我不怀疑你的技能,我也不是说不能做,但是您可以轻松地花费10倍的时间来识别内容,而不是实施ANN本身(假设您已经拥有数字图像处理技术的经验)。 我认为你的想法很有趣,也很有野心 。 在这一点上,您可能需要重新考虑它。 我觉得这个项目是你为大学打算的东西,所以如果工作的重点真的是ANN,你应该选择另一个游戏,更简单一些。 ... -
反向传播神经网络(Backpropagation neural network)[2022-06-09]
最明显的问题是您的训练数据集非常小 。 由于您使用的是scipy.optimize.minimize而不是通常的迭代梯度下降,我认为您的模型也可能过度拟合您的训练数据 。 在这里,迭代算法可能效果更好。 不要忘记仔细监控验证错误 。 如果您尝试使用梯度下降进行反向传播,请注意,根据反向传播使用的参数,神经网络需要一段时间才能收敛 您可以尝试多次向网络提供相同的训练数据或调整学习速率,但理想情况下您应该使用更多样化的数据。 Correctly normalizing the data solved the p ... -
有关为实际应用寻找良好神经网络架构的资源(Resources on finding good Neural Network architectures for real applications)[2023-08-23]
我写了关于这个话题的硕士论文: 托马马丁。 “ 卷积神经网络结构的分析和优化 ”。 arXiv预印本arXiv:1707.09725(2017)。 长话短说:有几种分析技术(第2.5章)和学习topoligies的算法(第3章),但实际上它主要是试错法和直觉。 I wrote my masters thesis about the topic: Thoma, Martin. "Analysis and Optimization of Convolutional Neural Network Architec ... -
现在重复我的评论建议作为答案,以便更容易看到后来在此页面上结尾的其他人(因为我不是100%确定它是解决方案,因此首先发布为评论): 将奖励的幅度减小到(或至少接近)[0.0,1.0]或[-1.0,1.0]间隔有助于网络更快地收敛。 以这种方式改变奖励值(简单地将它们全部除以数字以使它们位于较小的间隔中)不会改变网络在理论上能够学习的内容。 通过在整个网络中寻找更大的权重,网络还可以简单地学习具有更大奖励的相同概念。 然而,学习如此大的重量通常需要更多的时间。 主要原因是权重通常初始化为接近0的随机值,因此通 ...
-
图像质量神经网络项目(Image quality neural network project)[2022-02-17]
你应该使用卷积神经网络。 我建议只从图像识别项目复制神经网络,并在数据集上重新训练它。 有效的卷积NN实现不应该使用那么多内存,尽管可能需要很长时间才能进行训练。 您还可以使用较低分辨率的图像,这通常是在机器视觉中完成的。 将所有图像缩小到256x256像素。 You should use convolutional neural networks. I'd suggest just copying a neural network from an image recognition project, an ... -
这个问题没有简单的答案。 神经网络的优点和缺点是一个非常复杂的话题。 这里有一些提示: 没有免费的午餐定理:粗略地说,这个定理证明没有“完美”的机器学习方法。 对于每种问题,某种方法都是好的,还有另外一个问题,同样的方法会导致可怕的结果。 然而,其失败的方法可以通过其他方法很容易地解决。 在进行任何机器学习时,应始终考虑这一点。 神经网络很容易实现(你不需要一个好的线性代数求解器,例如SVN)。 神经网络的VC维不清楚。 当你想考虑解决方案有多好时,这一点非常重要。 神经网络不能被重新训练。 如果稍后添加数 ...
-
这方面的一个例子叫做神经进化。 您可以做的是将backprop与evolution结合起来,为您的数据集找到最佳结构。 Neataptic是提供神经进化的NN文库之一。 通过一些简单的编码,您可以将其转换为backprop + evolution。 这样做的缺点是它需要更多的计算能力,因为它需要遗传算法来运行整个群体。 因此,使用神经进化确实会使性能变得微弱。 但是,我认为有更多技术禁用某些节点,如果对输出没有负面影响,它们将被删除。 我不确定。 An example of this is called n ...
-
PyNeurGen是您的问题的可能解决方案。 它是一个支持多种网络架构的python库。 该库还包含一个前馈网络演示。 对于使用NARX net,您可以使用以下定义: NARX Net PyNeurGen PyNeurGen is a possible solution for your problem. Its a python library with support for several net architectures. The library also contains a demo for ...