草庐IT

self-attention

全部标签

python 并在方法中使用 'self'

根据我的阅读/理解,“self”参数类似于“this”。这是真的吗?如果它是可选的,如果没有将self传递到方法中,您会怎么做? 最佳答案 是的,它的使用方式类似。请注意,它是一个位置参数,您可以随意调用它;然而,有一个强烈的约定称它为self(不是this或其他任何东西)。对于可用的实例方法,必须有一些位置参数;它不是可选的。 关于python并在方法中使用'self',我们在StackOverflow上找到一个类似的问题: https://stackove

python - 在函数属性中访问 self

我正在尝试添加一个装饰器,该装饰器将可调用属性添加到函数,这些函数返回的对象与函数的返回值略有不同,但会在某个时候执行该函数。我遇到的问题是,当函数对象被传递到装饰器时,它是未绑定(bind)的并且不包含隐式的self参数。当我调用创建的属性函数(即string())时,我无法访问self,也无法将其传递给原始函数。defdeco(func):"""Addanattributetothefunctiontakesthesameargumentsasthefunctionbutmodifiestheoutput."""defstring(*args,**kwargs):returnstr

python - self 加入 Pandas

我想对Pandas数据框执行自连接,以便将某些行附加到原始行。每行都有一个标记“i”,指示应在右侧附加哪一行。d=pd.DataFrame(['A','B','C'],columns=['some_col'])d['i']=[2,1,1]In[17]:dOut[17]:some_coli0A21B12C1期望的输出:some_colisome_col_y0A2C1B1B2C1B也就是说,第2行附加到第0行,第1行附加到第1行,第1行附加到第2行(如i所示)。我的想法是pd.merge(d,d,left_index=True,right_on='i',how='left')但它会产生完全

python - 没有 self 的内部类功能

大家平安!我正在使用Python3.6.3,我发现奇怪的是这种构造是可能的:classTestClass(object):def__init__(self):self.arg="arg"deftest():print("Heytest")并使用:>>>TestClass.test()"Heytest"我知道在Python中有以self为参数的标准方法(不知道如何正确调用它们)、静态方法、类方法、抽象方法。但是test()是个什么样的方法呢?是静态方法吗?编辑:这种确定类内部函数的方法是否有任何有用的用例? 最佳答案 在python3

python - 我应该使用 "self"来定义不需要从外部访问的类实例化变量/对象吗?

我不是一个完全的初学者,但对Python还是个新手。今天在做一个项目时,我有一个想法,想知道“self”的用法;关于我过去一段时间一直在阅读的内容,我仍然无法弄清楚它是否总是必要的。我的问题仅涉及类实例和实例参数/变量。这个问题不是关于影响所有实例的类变量。示例:classC:def__init__(self,parent=None):super(C,self).__init__(parent)self.some_temp_var=AnotherClass()self.important_property=self.some_temp_var.bring_the_jewels()ins

python - 使用 __getattr__(self, name) 访问实例的其他属性

在Python的documentation,onthe__getattr__function中它说:Notethatiftheattributeisfoundthroughthenormalmechanism,__getattr__()isnotcalled.(Thisisanintentionalasymmetrybetween__getattr__()and__setattr__().)Thisisdonebothforefficiencyreasonsandbecauseotherwise__getattr__()wouldhavenowaytoaccessotherattrib

python - 在 Nose 测试课上使用 __init__(self) 而不是 setup(self) 有缺点吗?

为运行nosetests-sclassTestTemp():def__init__(self):print'__init__'self.even=0defsetup(self):print'__setup__'self.odd=1deftest_even(self):print'test_even'even_number=10asserteven_number%2==self.evendeftest_odd(self):print'test_odd'odd_number=11assertodd_number%2==self.odd打印出以下内容。__init____init____se

python - 为什么 `class X: mypow = pow` 有效? `self` 呢?

这有效并愉快地打印81:classX:mypow=powprint(X().mypow(3,4))但是为什么?方法不是给出了额外的“self”参数并且应该完全混淆吗?为了对比,我也用自己的Pow函数试了一下:defPow(x,y,z=None):returnx**yclassY:myPow=Powprint(Pow(3,4))print(Y().myPow(3,4))直接函数调用打印81并且方法调用按预期崩溃,因为它确实获得了额外的实例参数:Python3:TypeError:unsupportedoperandtype(s)for**orpow():'Y'and'int'Python

python - 我怎样才能停用 'Warning: Source ID 510 was not found when attempting to remove it - GLib.source_remove(self._idle_event_id)' ?

当我执行#!/usr/bin/envpythonimportmatplotlib.pyplotaspltplt.plot([1,2,3,4])plt.show()(和更复杂的例子)我明白了/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3.py:215:Warning:SourceID7wasnotfoundwhenattemptingtoremoveitGLib.source_remove(self._idle_event_id)是什么原因导致的?我该如何消除这些警告?我知道我可以用impor

python - 正弦嵌入 - Attention is all you need

在AttentionIsAllYouNeed,作者实现了位置嵌入(它添加了关于单词在序列中的位置的信息)。为此,他们使用正弦嵌入:PE(pos,2i)=sin(pos/10000**(2*i/hidden_units))PE(pos,2i+1)=cos(pos/10000**(2*i/hidden_units))其中pos是位置,i是维度。它必须产生形状为[max_length,embedding_size]的嵌入矩阵,即给定序列中的一个位置,它返回PE[position,:]的张量。我找到了Kyubyong's实现,但我不完全理解。我尝试通过以下方式在numpy中实现它:hidden