Does anyone know how to preserve the sparse zeros in a tensor when using tf.keras.experimental.SequenceFeatures? It seems to require a sparse tensor, but that eliminates the zeros in a sequence. If I have zeros in a sequence it throws an error
Does anyone know of another way to input sequence data into tensorflow using features?
here is a toy example:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import LSTM, Dropout, Dense
inputs = {'x1': tf.keras.layers.Input(name='x1', sparse=True, shape=(10), dtype='float32'),
'x2': tf.keras.layers.Input(name='x2', sparse=True, shape=(10), dtype='float32')}
columns = [tf.feature_column.sequence_numeric_column('x1', dtype=tf.float32),
tf.feature_column.sequence_numeric_column('x2', dtype=tf.float32)]
input_layer, input_length = tf.keras.experimental.SequenceFeatures(columns)(inputs)
lstm_out = LSTM(128, return_sequences=False)(input_layer)
lstm_out = Dense(5)(lstm_out)
model = tf.keras.models.Model(inputs, lstm_out)
model.compile(loss='mse', metrics='mae', optimizer='Adam')
def generator():
while True:
x_1 =np.asarray([[0,1,2,3,4,0,6,7,8,9],[1,2,3,4,5,0,7,8,9,10]], np.float32)
x_2 =np.asarray([[1,0.1,0,0.3,0.4,0.5,0.6,0.7,0.8,0.9],
[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0]], np.float32)
x1 = tf.sparse.from_dense(tf.convert_to_tensor(x_1, np.float32))
x2 = tf.sparse.from_dense(tf.convert_to_tensor(x_2, np.float32))
x = {'x1': x1, 'x2': x2}
data_np = np.asarray([[11,12,13,14,15],[12,13,14,15,16]], np.float32)
y = tf.convert_to_tensor(data_np, np.float32)
yield x, y
x, y = generator().__next__()
data = tf.data.Dataset.from_generator(generator, output_signature=({'x1':
tf.SparseTensorSpec(tf.TensorShape([2, 10]), tf.float32),
'x2': tf.SparseTensorSpec(tf.TensorShape([2, 10]), tf.float32)}, tf.TensorSpec(shape=(2, 5),
dtype=tf.float32)))
model.fit(data, steps_per_epoch=1, epochs=1, verbose=2)