• TensorFlow 2.9的零零碎碎(四)-模型的输入


    目录

    使用tf.keras.layers.Input

    使用input_shape参数

    所谓模型的输入


    TensorFlow 2.9的零零碎碎(二)-TensorFlow 2.9的零零碎碎(六)都是围绕使用TensorFlow 2.9在MNIST数据集上训练和评价模型来展开。

    Python环境3.8。

    代码调试都用的PyCharm。

    模型的输入一般在第一层指定,有两种方法:使用tf.keras.layers.Input函数或者使用input_shape参数

    使用tf.keras.layers.Input

    tf.keras.layers.Input是一个函数,定义在keras.engine.input_layer.Input

    其中有一个常用参数叫shape,也就是输入的尺寸,比如MNIST数据集的数据是28*28

    在用Sequential实例化处模型对象之后,第一层应该添加Input层,也就是输入层

    1. import tensorflow as tf
    2. model = tf.keras.models.Sequential()
    3. model.add(tf.keras.layers.Input(shape=(28, 28)))

    源码里注释比代码还多,基本都解释清楚了 

    1. @keras_export('keras.Input', 'keras.layers.Input')
    2. @traceback_utils.filter_traceback
    3. def Input( # pylint: disable=invalid-name
    4. shape=None,
    5. batch_size=None,
    6. name=None,
    7. dtype=None,
    8. sparse=None,
    9. tensor=None,
    10. ragged=None,
    11. type_spec=None,
    12. **kwargs):
    13. """`Input()` is used to instantiate a Keras tensor.
    14. A Keras tensor is a symbolic tensor-like object,
    15. which we augment with certain attributes that allow us to build a Keras model
    16. just by knowing the inputs and outputs of the model.
    17. For instance, if `a`, `b` and `c` are Keras tensors,
    18. it becomes possible to do:
    19. `model = Model(input=[a, b], output=c)`
    20. Args:
    21. shape: A shape tuple (integers), not including the batch size.
    22. For instance, `shape=(32,)` indicates that the expected input
    23. will be batches of 32-dimensional vectors. Elements of this tuple
    24. can be None; 'None' elements represent dimensions where the shape is
    25. not known.
    26. batch_size: optional static batch size (integer).
    27. name: An optional name string for the layer.
    28. Should be unique in a model (do not reuse the same name twice).
    29. It will be autogenerated if it isn't provided.
    30. dtype: The data type expected by the input, as a string
    31. (`float32`, `float64`, `int32`...)
    32. sparse: A boolean specifying whether the placeholder to be created is
    33. sparse. Only one of 'ragged' and 'sparse' can be True. Note that,
    34. if `sparse` is False, sparse tensors can still be passed into the
    35. input - they will be densified with a default value of 0.
    36. tensor: Optional existing tensor to wrap into the `Input` layer.
    37. If set, the layer will use the `tf.TypeSpec` of this tensor rather
    38. than creating a new placeholder tensor.
    39. ragged: A boolean specifying whether the placeholder to be created is
    40. ragged. Only one of 'ragged' and 'sparse' can be True. In this case,
    41. values of 'None' in the 'shape' argument represent ragged dimensions.
    42. For more information about RaggedTensors, see
    43. [this guide](https://www.tensorflow.org/guide/ragged_tensors).
    44. type_spec: A `tf.TypeSpec` object to create the input placeholder from.
    45. When provided, all other args except name must be None.
    46. **kwargs: deprecated arguments support. Supports `batch_shape` and
    47. `batch_input_shape`.
    48. Returns:
    49. A `tensor`.
    50. Example:
    51. ```python
    52. # this is a logistic regression in Keras
    53. x = Input(shape=(32,))
    54. y = Dense(16, activation='softmax')(x)
    55. model = Model(x, y)
    56. ```
    57. Note that even if eager execution is enabled,
    58. `Input` produces a symbolic tensor-like object (i.e. a placeholder).
    59. This symbolic tensor-like object can be used with lower-level
    60. TensorFlow ops that take tensors as inputs, as such:
    61. ```python
    62. x = Input(shape=(32,))
    63. y = tf.square(x) # This op will be treated like a layer
    64. model = Model(x, y)
    65. ```
    66. (This behavior does not work for higher-order TensorFlow APIs such as
    67. control flow and being directly watched by a `tf.GradientTape`).
    68. However, the resulting model will not track any variables that were
    69. used as inputs to TensorFlow ops. All variable usages must happen within
    70. Keras layers to make sure they will be tracked by the model's weights.
    71. The Keras Input can also create a placeholder from an arbitrary `tf.TypeSpec`,
    72. e.g:
    73. ```python
    74. x = Input(type_spec=tf.RaggedTensorSpec(shape=[None, None],
    75. dtype=tf.float32, ragged_rank=1))
    76. y = x.values
    77. model = Model(x, y)
    78. ```
    79. When passing an arbitrary `tf.TypeSpec`, it must represent the signature of an
    80. entire batch instead of just one example.
    81. Raises:
    82. ValueError: If both `sparse` and `ragged` are provided.
    83. ValueError: If both `shape` and (`batch_input_shape` or `batch_shape`) are
    84. provided.
    85. ValueError: If `shape`, `tensor` and `type_spec` are None.
    86. ValueError: If arguments besides `type_spec` are non-None while `type_spec`
    87. is passed.
    88. ValueError: if any unrecognized parameters are provided.
    89. """
    90. if sparse and ragged:
    91. raise ValueError(
    92. 'Cannot set both `sparse` and `ragged` to `True` in a Keras `Input`.')
    93. input_layer_config = {'name': name, 'dtype': dtype, 'sparse': sparse,
    94. 'ragged': ragged, 'input_tensor': tensor,
    95. 'type_spec': type_spec}
    96. batch_input_shape = kwargs.pop('batch_input_shape',
    97. kwargs.pop('batch_shape', None))
    98. if shape is not None and batch_input_shape is not None:
    99. raise ValueError('Only provide the `shape` OR `batch_input_shape` argument '
    100. 'to Input, not both at the same time.')
    101. if (batch_input_shape is None and shape is None and tensor is None
    102. and type_spec is None):
    103. raise ValueError('Please provide to Input a `shape` '
    104. 'or a `tensor` or a `type_spec` argument. Note that '
    105. '`shape` does not include the batch '
    106. 'dimension.')
    107. if kwargs:
    108. raise ValueError(f'Unrecognized keyword arguments: {list(kwargs.keys())}')
    109. if batch_input_shape:
    110. shape = batch_input_shape[1:]
    111. input_layer_config.update({'batch_input_shape': batch_input_shape})
    112. else:
    113. input_layer_config.update(
    114. {'batch_size': batch_size, 'input_shape': shape})
    115. input_layer = InputLayer(**input_layer_config)
    116. # Return tensor including `_keras_history`.
    117. # Note that in this case train_output and test_output are the same pointer.
    118. outputs = input_layer._inbound_nodes[0].outputs
    119. if isinstance(outputs, list) and len(outputs) == 1:
    120. return outputs[0]
    121. else:
    122. return outputs

    使用input_shape参数

    一个模型由若干层构成,在keras.api._v2.keras.layers.__init__.py中可以看到Keras封装好的层的名字,不同层的定义所在位置不同,比如keras.engine.base_layer、keras.engine.input_layer、keras.layers.activation.relu、keras.layers.convolutional.conv2d、keras.layers.core.dense

    如果不用tf.keras.layers.Input函数,则可以直接在其他层中构建输入层,比如Dense层、Flatten层

    1. import tensorflow as tf
    2. model = tf.keras.models.Sequential()
    3. model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
    4. model.add(tf.keras.layers.Dense(128, activation='relu'))
    5. model.add(tf.keras.layers.Dropout(0.2))
    6. model.add(tf.keras.layers.Dense(10, activation='softmax'))

    在keras.layers.core.dense模块的Dense类中,有以下注释

    1. When a popular kwarg `input_shape` is passed, then keras will create
    2. an input layer to insert before the current layer. This can be treated
    3. equivalent to explicitly defining an `InputLayer`.

    可以看出,给这些层传参input_shape的时候,Keras会在当前层之前构建一个输入层,效果和使用tf.keras.layers.Input函数相同。

    所谓模型的输入

    一个模型由若干层构成,每一层作为一个模块,接受一个输入,并产生一个输出,这种逻辑在代码中很常见。

    就像我们搭积木一样,无非是小积木组成大积木,大积木再组成更大的积木,最后模型也就出来了。

    用户需要负责的就是第一层的输入,也就是模型的输入

    只要这个总输入定义好,层间的输入和输出TensorFlow、Keras已经封装好了,会自动计算,不用用户去管

  • 相关阅读:
    python+大数据校园卡数据分析 计算机竞赛
    AcWing工程课系列——《Linux基础课》
    【算法】折半查找
    红帽8使用nfs共享本地镜像
    有人说,Linux 发行版激增不利于 Linux 生态系统?
    Go framework-go-zero
    Aspose.Email 22.7 for Java Crack
    尚硅谷Vue系列教程学习笔记(6)
    ubuntu-server部署hive-part2-安装hadoop
    以K近邻算法为例,使用交叉验证优化模型最佳参数
  • 原文地址:https://blog.csdn.net/ytomc/article/details/126278207