x=tf.compat.v1.sparse.placeholder(tf.float32)y=tf.sparse.reduce_sum(x)withtf.compat.v1.Session()assess: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)}))# Willsucceed.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} 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.
[null,null,["Last updated 2023-03-17 UTC."],[],[],null,["# tf.compat.v1.sparse_placeholder\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.9.3/tensorflow/python/ops/array_ops.py#L3401-L3497) |\n\nInserts a placeholder for a sparse tensor that will be always fed.\n\n#### View aliases\n\n\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`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/sparse_placeholder)\n\n\u003cbr /\u003e\n\n tf.compat.v1.sparse_placeholder(\n dtype, shape=None, name=None\n )\n\n| **Important:** This sparse tensor will produce an error if evaluated. Its value must be fed using the `feed_dict` optional argument to `Session.run()`, [`Tensor.eval()`](https://www.tensorflow.org/api_docs/python/tf/Tensor#eval), or [`Operation.run()`](https://www.tensorflow.org/api_docs/python/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.sparse.SparseTensor(indices=indices, values=values,\n 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"]]