tf.placeholder_with_default 函数取代tf.placeholder的方法

本文介绍了如何使用tf.placeholder_with_default函数替代tf.placeholder。tf.placeholder_with_default在未被喂入值时会传递默认值,而tf.placeholder则需要指定数据类型和形状。文中给出了从tf.placeholder转换到tf.placeholder_with_default的示例,并强调了使用zeros函数来指定形状和类型的重要性。

tf.placeholder_with_default 函数取代tf.placeholder的方法

相信大家在储存是tf.placeholder函数使用的比较多,这里介绍另一种占位符的使用方法

函数基本定义

内容引用

参考引用
https://www.tensorflow.org/versions/r1.14

tf.placeholder_with_default

A placeholder op that passes through input when its output is not fed.

tf.placeholder_with_default = tf.compat.v1.placeholder_with_default

基本定义

tf.placeholder_with_default(
    input,
    shape,
    name=None
)

函数定义为下记
input: A Tensor. The default value to produce when output is not fed.
shape: A tf.TensorShape or list of ints. The (possibly partial) shape of the tensor.
name: A name for the operation (optional).

tf.placeholder

Inserts a placeholder for a tensor that will be always fed.

相当于
tf.placeholder = tf.compat.v1.placeholder

基本定义

tf.placeholder(
    dtype,
    shape=None,
    name=None
)

函数定义为下记
dtype: The type of elements in the tensor to be fed.
shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.
name: A name for the operation (optional).

tf.placeholder使用大家都已经很熟悉了

下面介绍如何用tf.placeholder_with_default 函数取代tf.placeholder

tf.placeholder中我们需要指定加载数据的类型和要输入张量的形状 (tensors形状)

tf.placeholder_with_default 中 需要给输入数据制定形状和类型为此我们用到zeros这个python函数来赋予这些数值

zeros(shape, dtype=float,)
返回:返回来一个给定形状和类型的用0填充的数组;

直接上代码

input_x =np.zeros(["这里定义输入数据形状"], dtype="这里定义输入数据类型")
x = tf.placeholder_with_default(input_x, shape=["这里定义输入数据形状"]) 

通过转换可以实现取代tf.placeholder

这里小编只是提供一种参考,希望对大家有所帮助
小编也在努力学习中,如有错误希望多指教
资料查找不易如需引用请标注来源

参考引用stackoverflow.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值