tf.sparse.placeholder
Stay organized with collections
Save and categorize content based on your preferences.
Inserts a placeholder for a sparse tensor that will be always fed.
tf.sparse.placeholder(
dtype, shape=None, name=None
)
For example:
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.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} Placeholders are not compatible with eager execution.
Args |
dtype
|
The type of values 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 sparse tensor of any shape.
|
name
|
A name for prefixing the operations (optional).
|
Returns |
A SparseTensor that may be used as a handle for feeding a value, but not
evaluated directly.
|
Raises |
RuntimeError
|
if eager execution is enabled
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.sparse.placeholder\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/array_ops.py#L2649-L2713) |\n\nInserts a placeholder for a sparse tensor that will be always fed.\n\n#### View aliases\n\n\n**Main aliases**\n\n\\`tf.sparse_placeholder\\`\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.sparse.placeholder`](/api_docs/python/tf/compat/v1/sparse_placeholder), [`tf.compat.v1.sparse_placeholder`](/api_docs/python/tf/compat/v1/sparse_placeholder)\n\n\u003cbr /\u003e\n\n tf.sparse.placeholder(\n dtype, shape=None, name=None\n )\n\n| **Key Point:** This sparse tensor will produce an error if evaluated. Its value must be fed using the `feed_dict` optional argument to [`Session.run()`](../../tf/InteractiveSession#run), [`Tensor.eval()`](../../tf/Tensor#eval), or [`Operation.run()`](../../tf/Operation#run).\n\n#### For example:\n\n x = tf.compat.v1.sparse.placeholder(tf.float32)\n y = tf.sparse.reduce_sum(x)\n\n with tf.compat.v1.Session() as sess:\n print(sess.run(y)) # ERROR: will fail because x was not fed.\n\n indices = np.array([[3, 2, 0], [4, 5, 1]], dtype=np.int64)\n values = np.array([1.0, 2.0], dtype=np.float32)\n shape = np.array([7, 9, 2], dtype=np.int64)\n print(sess.run(y, feed_dict={\n x: tf.compat.v1.SparseTensorValue(indices, values, shape)})) # Will\n succeed.\n print(sess.run(y, feed_dict={\n x: (indices, values, shape)})) # Will succeed.\n\n sp = tf.SparseTensor(indices=indices, values=values, dense_shape=shape)\n sp_value = sp.eval(session=sess)\n print(sess.run(y, feed_dict={x: sp_value})) # Will succeed.\n\n@compatibility{eager} Placeholders are not compatible with eager execution.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|-------------------------------------------------------------------------------------------------------------------------|\n| `dtype` | The type of `values` elements in the tensor to be fed. |\n| `shape` | The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a sparse tensor of any shape. |\n| `name` | A name for prefixing the operations (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `SparseTensor` that may be used as a handle for feeding a value, but not evaluated directly. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|-------------------------------|\n| `RuntimeError` | if eager execution is enabled |\n\n\u003cbr /\u003e"]]