ageron / ageron/handson-ml2

Chapter 13: How to convert a sparse tensor with rank(st_input) greater than 2 to RaggedTensor?

Open
#101 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
30k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

I am reading a text file through Data API and converting the text into TFRecords. When I am reading the records line by line, I am getting a sparse matrix of rank 2 which is easily being transformed into a RaggedTensor by `tf.RaggedTensor.from_sparse`. However, when I am reading the records in batch of size 10, resulting sparse tensor is of rank 3. In the documentation, it is written that `tf.RaggedTensor.from_sparse` works only with sparse tensors of rank 2. Is there a way to create RaggedTensors from a sparse tensor of rank 3? I tried `tf.sparse.reshape` and tried removing the axis=1. This was successful as I was able to print the output. But this seems a little hackish way to do and I am looking for something more tensorflow-ish. An example to reproduce my case is as follows:-

1. When reading tfrecords line by line-

```python
indices = array([
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 0, 5],
[ 0, 6],
[ 0, 7],
[ 0, 8],
[ 0, 9],
[ 0, 10]], dtype=int64)

values = array([b'What', b'interesting', b'fact', b'about', b'India', b'can',
b'you', b'add', b'to', b'my', b'knowledge?'], dtype=object)
dense_shape = array([ 1, 11], dtype=int64)

tf.RaggedTensor.from_sparse(tf.SparseTensor(indices=indices,values=values,dense_shape=dense_shape))
```

gives

```

```

2. When reading a batch of size 1 (actual batch size was 10 but that would become way too cumbersome):

```python
indices = array([
[ 0, 0, 0],
[ 0, 0, 1],
[ 0, 0, 2],
[ 0, 0, 3],
[ 0, 0, 4],
[ 0, 0, 5],
[ 0, 0, 6],
[ 0, 0, 7],
[ 0, 0, 8],
[ 0, 0, 9],
[ 0, 0, 10]], dtype=int64)

values = array([b'What', b'interesting', b'fact', b'about', b'India', b'can',
b'you', b'add', b'to', b'my', b'knowledge?'], dtype=object)
dense_shape = array([ 1, 1, 11], dtype=int64)

tf.RaggedTensor.from_sparse(tf.SparseTensor(indices=indices,values=values,dense_shape=dense_shape))
```

gives an error message:

```
ValueError: rank(st_input) must be 2
```

However, using the statement

```python
tf.sparse.reshape(tf.SparseTensor(indices=indices,values=values,dense_shape=dense_shape),shape=[1,11])
```

resolves this issue.

Apologies for the sloppy editing.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.