关于张量流神经网络中输入大小的问题

A question about input size in a tensorflow neural network

我是 PyTorch 用户,但最近阅读了一些使用 tensorflow 实现的代码。 我的问题是,如果我们只有一个像这样的简单神经网络,那么 input 大小指定在哪里?或者这个模型是否允许使用 可变 尺寸输入?

class SimpleNet(tf.keras.Model):
    def __init__(self):
        super(SimpleNet, self).__init__()
        activation = tf.keras.activations.tanh
        self.features = [

         #Where is the input size specified?
            tf.keras.layers.Dense(89, activation),
            tf.keras.layers.Dense(6 * 32, activation),
            tf.keras.layers.Dense(32, activation),
            tf.keras.layers.Dense(1),
        ]

    def process(self, x):
        x = apply_layers(x, self.features)
        return x

    ...

input_shape 是在您将真实数据传递给模型时推断出来的。意思是,如果您没有明确定义 input_shape ,它就是可变的。 例如,您可以在模型的第一层明确定义 input_shape

tf.keras.layers.Dense(89, activation, input_shape=(5,))

并且每一层都从上一层的输出中推导出需要的input shape。但是请注意,一旦您为模型提供了真实数据,它就会存储此 input_shape 以供将来使用。另一个例子表明,只要与第一个 Dense 层兼容,数据的形状并不重要:

import tensorflow as tf

activation = tf.keras.activations.tanh
model1 = tf.keras.Sequential(
    [
    tf.keras.layers.Dense(89, activation),
    tf.keras.layers.Dense(6 * 32, activation),
    tf.keras.layers.Dense(32, activation),
    tf.keras.layers.Dense(1),
    ])

tf.print('Works -->', model1(tf.random.normal((10, 1))).shape)
model2 = tf.keras.Sequential(
    [
    tf.keras.layers.Dense(89, activation),
    tf.keras.layers.Dense(6 * 32, activation),
    tf.keras.layers.Dense(32, activation),
    tf.keras.layers.Dense(1),
    ])
tf.print('Works -->', model2(tf.random.normal((10, 5))).shape)
Works --> TensorShape([10, 1])
Works --> TensorShape([10, 1])

其中 10 表示样本数,1 和 5 表示特征数。