小编Dan*_*ola的帖子

定义自定义tf.keras层时是否还需要实现`compute_output_shape()`?

我已经使用 TensorFlow 2.1.0Layer在 中实现了一个自定义tf.keras

过去,在使用独立的 Keras 时,重要的是compute_output_shape(input_shape)在任何自定义层中定义方法,以便可以创建计算图。

现在,转移到 TF2 后,我发现即使我从自定义实现中删除了该方法,该层仍然可以按预期工作。显然,它可以在 Eager 和图形模式下工作。这是我的意思的一个例子:

from tensorflow.keras.layers import Layer, Input
from tensorflow.keras.models import Sequential
import numpy as np


class MyLayer(Layer):
    def call(self, inputs):
        return inputs[:, :-1]  # Do something that changes the shape


m = Sequential([MyLayer(), MyLayer()])
m.predict(np.ones((10, 3)))  # This would not have worked in the past
Run Code Online (Sandbox Code Playgroud)

可以说compute_output_shape()不再需要了吗?我错过了什么重要的东西吗?

在文档中没有明确提及 remove compute_output_shape(),尽管没有一个示例明确实现它。

谢谢

keras tensorflow keras-layer tf.keras tensorflow2.0

4
推荐指数
1
解决办法
1133
查看次数

在纯 numpy 中矢量化康威的生命游戏?

我想知道是否有一种方法可以实现康威的生命游戏,而无需求助于 for 循环、if 语句和其他编程典型的控制结构。对循环进行矢量化应该很容易,但是如何将对邻域的检查转换为矩阵运算?

基本逻辑是这样

def neighbors(cell, distance=1):
    """Return the neighbors of cell."""
    x, y = cell
    r = xrange(0 - distance, 1 + distance)
    return ((x + i, y + j) # new cell offset from center
            for i in r for j in r # iterate over range in 2d
            if not i == j == 0) # exclude the center cell
Run Code Online (Sandbox Code Playgroud)

我希望模组不会认为这是偏离主题的,我真的很好奇,而且我刚刚开始使用 CA。

干杯

python numpy linear-algebra vectorization cellular-automata

2
推荐指数
1
解决办法
3671
查看次数