The code from the manual, "Working with sparse tensors" (link):
x = tf.keras.Input(shape=(4,), sparse=True)
y = tf.keras.layers.Dense(4)(x)
model = tf.keras.Model(x, y)
sparse_data = tf.sparse.SparseTensor(
indices = [(0,0),(0,1),(0,2),
(4,3),(5,0),(5,1)],
values = [1,1,1,1,1,1],
dense_shape = (6,4)
)
model(sparse_data)
model.predict(sparse_data)
Produces the error,
ValueError: Unrecognized data type: x=SparseTensor(indices=tf.Tensor( ....
This doesnt happen with keras 2.15, only keras 3
Comment From: fchollet
The ability to call fit
/predict
with sparse tensors is coming. For now, you can use a tf.data.Dataset that yields sparse tensors when calling fit
/predict
.
Like this:
import tensorflow as tf
import keras
print(keras.version()) # 3.0.5
x = keras.Input(shape=(4,), sparse=True)
y = keras.layers.Dense(4)(x)
model = keras.Model(x, y)
sparse_data = tf.sparse.SparseTensor(
indices = [(0,0),(0,1),(0,2),
(4,3),(5,0),(5,1)],
values = [1,1,1,1,1,1],
dense_shape = (6,4)
)
model(sparse_data)
sparse_dataset = tf.data.Dataset.from_tensor_slices(sparse_data).batch(1)
model.predict(sparse_dataset)
Comment From: mdhvgoyal
I would like to work on this.