假设我想编写一个符合tf.kerasAPI的自定义优化器类(使用 TensorFlow version>=2.0)。我对记录在案的方法与实现中的方法感到困惑。
tf.keras.optimizers.Optimizer 状态的文档,
### Write a customized optimizer.
If you intend to create your own optimization algorithm, simply inherit from
this class and override the following methods:
- resource_apply_dense (update variable given gradient tensor is dense)
- resource_apply_sparse (update variable given gradient tensor is sparse)
- create_slots (if your optimizer algorithm requires additional variables)
Run Code Online (Sandbox Code Playgroud)
不过,目前的tf.keras.optimizers.Optimizer实现没有定义resource_apply_dense方法,但它确实定义了一个私人的前瞻性_resource_apply_dense方法存根。同样,没有resource_apply_sparseorcreate_slots方法,但有_resource_apply_sparse方法存根和_create_slots方法调用。
在官方 …
考虑以下:
>>> import numbers
>>> import numpy
>>> a = numpy.int_(0)
>>> isinstance(a, int)
False
>>> isinstance(a, numbers.Integral)
True
>>> b = numpy.float_(0)
>>> isinstance(b, float)
True
>>> isinstance(b, numbers.Real)
True
Run Code Online (Sandbox Code Playgroud)
NumPy numpy.int_和numpy.float_类型都在Python的数字抽象基类层次结构中,但我很奇怪,np.int_对象不是内置int类的实例,而np.float_对象是内置float类型的实例.
为什么会这样?
假设我有一个函数表示一些可能失败的计算
f :: a -> Maybe b
Run Code Online (Sandbox Code Playgroud)
如果我有一个列表l,我可以找到列表中f成功使用的第一个(从左到右扫描时)项目,以下函数findList f l在哪里findList
findList :: (a -> Maybe b) -> [a] -> Maybe b
findList f [] = Nothing
findList f (x : xs) = case f x of
Nothing -> findList f xs
Just y -> Just y
Run Code Online (Sandbox Code Playgroud)
(或使用,例如,firstJust从extra包)。
如果我想用Setfrom做同样的事情我该怎么办containers?
也就是说,我想要一个带有签名的函数
findSet :: (a -> Maybe b) -> Set a -> Maybe b
Run Code Online (Sandbox Code Playgroud)
这相当于
findSet f …Run Code Online (Sandbox Code Playgroud)