外部库为接口class integrateOut2(theano.Op):
def __init__(self, f, int_var, *args,**kwargs):
super(integrateOut2,self).__init__()
self.f = f
self.int_var = int_var
def make_node(self, *inputs):
tmax = inputs[0]
self.fvars=list(inputs[1:])
return theano.Apply(self, [tmax]+self.fvars, [T.dscalar().type()])
def perform(self, node, inputs, output_storage):
# Everything else is an argument to the quad function
tmax = inputs[0]
args = tuple(inputs[1:])
# create a function to evaluate the integral
f = theano.function([self.int_var]+self.fvars, self.f)
# actually compute the integral
output_storage[0][0] = quad(f, 0., tmax, args=args)[0]
def grad(self, inputs, grads):
tmax = inputs[0]
param_grads = T.grad(self.f, self.fvars)
## Recall fundamental theorem of calculus
## d/dt \int^{t}_{0}f(x)dx = f(t)
## So sub in t_max to the graph
FTC_grad = theano.clone(self.f, {self.int_var: tmax})
grad_list = [FTC_grad*grads[0]] + \
[integrateOut2(grad_fn, self.int_var)(*inputs)*grads[0] \
for grad_fn in param_grads]
return grad_list
提供了许多可选属性:
Foo
我的代码接受interface Foo {
propertyA?: string;
propertyB?: string;
}
并填写了一些属性。后来的代码要求部分填写Foo
。
Foo
我可以使用哪种类型的注释来表示“这是interface FooWithA {
propertyA: string;
propertyB?: string;
}
属性X,Y,Z非可选”,以便Foo
停止抱怨?
答案 0 :(得分:2)
您可以使用intersection types:
type FooWithA = Foo & {
propertyA: string;
}
或者您可以扩展界面:
interface FooWithA extends Foo {
propertyA: string
}
如果你担心的是编译器抱怨空检查,你可以在使用这些属性时使用!
non-null assertion operator。