Cortex can train any model that implements the TensorFlow Estimator API. Models can be trained using any subset of the raw and transformed columns.
import tensorflow as tf​def create_estimator(run_config, model_config):"""Create an estimator to train the model.​Args:run_config: An instance of tf.estimator.RunConfig to be used when creatingthe estimator.​model_config: The Cortex configuration for the model. Column references in allinputs (i.e. model_config["target_column"], model_config["input"], andmodel_config["training_input"]) are replaced by their names (e.g. "@column1"will be replaced with "column1"). All other resource references (e.g. constantsand aggregates) are replaced by their runtime values.​Returns:An instance of tf.estimator.Estimator to train the model."""pass
See the tf.estimator.RunConfig and tf.estimator.Estimator documentation for more information.
See Cortex's built-in estimators for example implementations.
import tensorflow as tf​def create_estimator(run_config, model_config):feature_columns = []for col_name in model_config["input"]["numeric_columns"]:feature_columns.append(tf.feature_column.numeric_column(col_name))​return tf.estimator.DNNClassifier(feature_columns=feature_columns,n_classes=model_config["input"]["num_classes"],hidden_units=model_config["hparams"]["hidden_units"],config=run_config,)
You can import PyPI packages or your own Python packages to help create more complex models. See Python Packages for more details.
The following packages have been pre-installed and can be used in your implementations:
tensorflow==1.14.0boto3==1.9.78msgpack==0.6.1numpy>=1.13.3,<2requirements-parser==0.2.0packaging==19.0.0pillow==6.1.0regex==2017.4.5requests==2.21.0
You can install additional PyPI packages and import your own Python packages. See Python Packages for more details.
You can preprocess input features and labels to your model by defining a transform_tensorflow function. You can define tensor transformations you want to apply to the features and labels tensors before they are passed to the model.
def transform_tensorflow(features, labels, model_config):"""Define tensor transformations for the feature and label tensors.​Args:features: A feature dictionary of column names to feature tensors.​labels: The label tensor.​model_config: The Cortex configuration for the model. Column references in allinputs (i.e. model_config["target_column"], model_config["input"], andmodel_config["training_input"]) are replaced by their names (e.g. "@column1"will be replaced with "column1"). All other resource references (e.g. constantsand aggregates) are replaced by their runtime values.​Returns:features and labels tensors."""return features, labels
import tensorflow as tf​def transform_tensorflow(features, labels, model_config):hparams = model_config["hparams"]features["image_pixels"] = tf.reshape(features["image_pixels"], hparams["input_shape"])return features, labels