TensorFlow函数:tf.sparse_placeholder

2018-03-01 11:19 更新

tf.sparse_placeholder 函数

sparse_placeholder(
    dtype,
    shape=None,
    name=None
)

定义在:tensorflow/python/ops/array_ops.py.

请参阅指南:输入和读取器>占位符操作

为稀疏张量插入占位符,该稀疏张量将始终被提供.

注意:如果计算,此稀疏张量将产生一个错误;必须使用 feed_dict 可选参数将其值提供到 Session.run(),Tensor.eval() 或 Operation.run().

如下示例:

x = tf.sparse_placeholder(tf.float32)
y = tf.sparse_reduce_sum(x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: will fail because x was not fed.

  indices = np.array([[3, 2, 0], [4, 5, 1]], dtype=np.int64)
  values = np.array([1.0, 2.0], dtype=np.float32)
  shape = np.array([7, 9, 2], dtype=np.int64)
  print(sess.run(y, feed_dict={
    x: tf.SparseTensorValue(indices, values, shape)}))  # Will succeed.
  print(sess.run(y, feed_dict={
    x: (indices, values, shape)}))  # Will succeed.

  sp = tf.SparseTensor(indices=indices, values=values, dense_shape=shape)
  sp_value = sp.eval(session=sess)
  print(sess.run(y, feed_dict={x: sp_value}))  # Will succeed

@compatibility {eager}占位符与 eager 执行不兼容.

函数参数:

  • dtype:张量中要提供的 values 元素的类型.
  • shape:要提供的张量的形状(可选).如果未指定形状,则可以提供任何形状的稀疏张量.
  • name:前缀操作的名称(可选).

函数返回值:

tf.sparse_placeholder 函数返回一个可以用作提供值的句柄的 SparseTensor,但不能直接计算.

可能引发的异常:

  • RuntimeError:如果 eager 执行已启用,则引发该异常.
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号