I'm trying to build a ML production pipeline using TFX, and currently working on Trainer module. I need to implement modelling in a separate file.
Here is part of pipeline, which handle training:
trainer = Trainer(
module_file=module_file,
custom_executor_spec=executor_spec.ExecutorClassSpec(GenericExecutor),
transformed_examples=transform.outputs['transformed_examples'],
schema=schema_gen.outputs['schema'],
transform_graph=transform.outputs['transform_graph'],
train_args=trainer_pb2.TrainArgs(num_steps=10000),
eval_args=trainer_pb2.EvalArgs(num_steps=5000))
And here is part of module_file, which define model:
_DENSE_FLOAT_FEATURE_KEYS = ['number_of_likes', 'number_of_comments', 'owner_influence']
def _build_keras_model() -> tf.keras.Model:
inputs = [
keras.layers.Input(shape=(1,), name=_transformed_name(f))
for f in _DENSE_FLOAT_FEATURE_KEYS
]
d = keras.layers.concatenate(inputs)
for _ in range(int(4)):
d = keras.layers.Dense(8, activation='relu')(d)
output = keras.layers.Dense(1)(d)
model = keras.Model(inputs=inputs, outputs=output)
model.compile(loss='mean_absolute_error',
optimizer='adam',
metrics=['mean_absolute_error'])
model.summary(print_fn=absl.logging.info)
return model
And another function, which is called by Trainer:
def run_fn(fn_args: TrainerFnArgs):
tf_transform_output = tft.TFTransformOutput(fn_args.transform_output)
train_dataset = _input_fn(fn_args.train_files, tf_transform_output,
batch_size=_TRAIN_BATCH_SIZE)
eval_dataset = _input_fn(fn_args.eval_files, tf_transform_output,
batch_size=_EVAL_BATCH_SIZE)
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
model = _build_keras_model()
model.fit(
train_dataset,
steps_per_epoch=fn_args.train_steps,
validation_data=eval_dataset,
validation_steps=fn_args.eval_steps)
When I run this, I got such error:
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("DeserializeSparse_3:0", shape=(None, 2), dtype=int64, device=/job:localhost/replica:0/task:0/device:CPU:0), values=Tensor("DeserializeSparse_3:1", shape=(None,), dtype=float32, device=/job:localhost/replica:0/task:0/device:CPU:0), dense_shape=Tensor("stack_3:0", shape=(2,), dtype=int64, device=/job:localhost/replica:0/task:0/device:CPU:0)). Consider casting elements to a supported type.[while running 'Run[Trainer]']
Whole logic and code I took from official TFX examples and guides. After concatenation of inputs, data comes to model` layers in Tensor format, not in SparseTensor. I can not figure out what is going there, and there are no such cases in web :(
Will be very grateful for the help!