當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.compat.v1.sparse_placeholder用法及代碼示例

為將始終饋送的稀疏張量插入占位符。

用法

tf.compat.v1.sparse_placeholder(
    dtype, shape=None, name=None
)

參數

  • dtype 要饋送的張量中values 元素的類型。
  • shape 要饋送的張量的形狀(可選)。如果未指定形狀,則可以輸入任何形狀的稀疏張量。
  • name 操作前綴的名稱(可選)。

返回

  • 一個SparseTensor,可用作提供值的句柄,但不直接評估。

拋出

  • RuntimeError 如果啟用了即刻執行

重要的:如果評估,此稀疏張量將產生錯誤。它的值必須使用 feed_dict 可選參數提供給 Session.run()Tensor.eval()Operation.run()

例如:

x = tf.compat.v1.sparse.placeholder(tf.float32)
y = tf.sparse.reduce_sum(x)

with tf.compat.v1.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.compat.v1.SparseTensorValue(indices, values, shape)}))  # Will
    succeed.
  print(sess.run(y, feed_dict={
    x:(indices, values, shape)}))  # Will succeed.

  sp = tf.sparse.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} 占位符與即刻執行不兼容。

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.sparse_placeholder。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。