keras.backend: K.placeholder函数参数

本文讲解keras.backend.placeholder函数使用以及函数参数:


1、如何导入keras.backend:

from keras import backend as K


if __name__ == '__main__':
	inputs = K.placeholder(shape=(2, 3, 5), ndim=4)
	print('inputs ndim: %s' % K.ndim(inputs))

2、运行输入结果:

inputs ndim: 3

3、函数源码解析:

def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None):
    """Instantiates a placeholder tensor and returns it.

    # Arguments
        shape: Shape of the placeholder
            (integer tuple, may include `None` entries).
        ndim: Number of axes of the tensor.
            At least one of {`shape`, `ndim`} must be specified.
            If both are specified, `shape` is used.
        dtype: Placeholder type.
        sparse: Boolean, whether the placeholder should have a sparse type.
        name: Optional name string for the placeholder.

    # Returns
        Tensor instance (with Keras metadata included).

    # Examples
    ```python
        >>> from keras import backend as K
        >>> input_ph = K.placeholder(shape=(2, 4, 5))
        >>> input_ph._keras_shape
        (2, 4, 5)
        >>> input_ph
        <tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>
    ```
    """
    if dtype is None:
        dtype = floatx()
    if not shape:
        if ndim:
            shape = tuple([None for _ in range(ndim)])
    if sparse:
        x = tf.sparse_placeholder(dtype, shape=shape, name=name)
    else:
        x = tf.placeholder(dtype, shape=shape, name=name)
    x._keras_shape = shape
    x._uses_learning_phase = False
    return x


4、函数参数解释

# shape: Shape of the placeholder(integer tuple, may include `None` entries).
# shape: 占位符的形状, 输入为整形int的元组,可以含有None.

# ndim: Number of axes of the tensor.
# ndim: 张量维度数.
# 注意: 至少指定`shape`或者`ndim`参数, 如果同时指定, 将以`shape`传入为主.

# dtype: Placeholder type.
# dtype: 数据类型(data type).

# Sparse: Boolean, whether the placeholder should have a sparse type.
# Sparse: 布尔类型, 占位符是否应为稀疏类型.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值