def test_convert_simple_df_to_tensor(): df = pd.DataFrame({"X1": [1, 3], "X2": [2, 4]}) actual_tensor = convert_pandas_to_tf_tensor(df) expected_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.dtypes.int64) tf.debugging.assert_equal(actual_tensor, expected_tensor) actual_tensor = convert_pandas_to_tf_tensor(df[["X1"]]) expected_tensor = tf.constant([[1], [3]], dtype=tf.dtypes.int64) tf.debugging.assert_equal(actual_tensor, expected_tensor)
def test_convert_simple_df_to_tensor_with_dtype(): df = pd.DataFrame({"X1": [1, 3], "X2": [2, 4]}) actual_tensor = convert_pandas_to_tf_tensor(df, dtype=tf.dtypes.float16) expected_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.dtypes.float16) tf.debugging.assert_equal(actual_tensor, expected_tensor)
def test_convert_image_df_to_tensor(): images = np.zeros([4, 3, 32, 32]) df = pd.DataFrame({"image": TensorArray(images)}) actual_tensor = convert_pandas_to_tf_tensor(df) expected_tensor = tf.zeros([4, 3, 32, 32], dtype=images.dtype) tf.debugging.assert_equal(actual_tensor, expected_tensor)
def predict( self, data: DataBatchType, feature_columns: Optional[Union[List[str], List[int]]] = None, dtype: Optional[tf.dtypes.DType] = None, ) -> DataBatchType: """Run inference on data batch. The data is converted into a TensorFlow Tensor before being inputted to the model. Args: data: A batch of input data. Either a pandas DataFrame or numpy array. feature_columns: The names or indices of the columns in the data to use as features to predict on. If None, then use all columns in ``data``. dtype: The TensorFlow dtype to use when creating the TensorFlow tensor. If set to None, then automatically infer the dtype. Examples: .. code-block:: python import numpy as np import tensorflow as tf from ray.train.predictors.tensorflow import TensorflowPredictor def build_model(self): return tf.keras.Sequential( [ tf.keras.layers.InputLayer(input_shape=(2,)), tf.keras.layers.Dense(1), ] ) predictor = TensorflowPredictor(model_definition=build_model) data = np.array([[1, 2], [3, 4]]) predictions = predictor.predict(data) .. code-block:: python import pandas as pd import tensorflow as tf from ray.train.predictors.tensorflow import TensorflowPredictor def build_model(self): return tf.keras.Sequential( [ tf.keras.layers.InputLayer(input_shape=(1,)), tf.keras.layers.Dense(1), ] ) predictor = TensorflowPredictor(model_definition=build_model) # Pandas dataframe. data = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"]) predictions = predictor.predict(data) # Only use first column as the feature predictions = predictor.predict(data, feature_columns=["A"]) Returns: DataBatchType: Prediction result. """ if self.preprocessor: data = self.preprocessor.transform_batch(data) if isinstance(data, pd.DataFrame): if feature_columns: data = data[feature_columns] tensor = convert_pandas_to_tf_tensor(data, dtype=dtype) else: tensor = tf.convert_to_tensor(data, dtype=dtype) # TensorFlow model objects cannot be pickled, therefore we use # a callable that returns the model and initialize it here, # instead of having an initialized model object as an attribute. model = self.model_definition() if self.model_weights is not None: input_shape = list(tensor.shape) # The batch axis can contain varying number of elements, so we set # the shape along the axis to `None`. input_shape[0] = None model.build(input_shape=input_shape) model.set_weights(self.model_weights) prediction = list(model(tensor).numpy()) return pd.DataFrame({"predictions": prediction}, columns=["predictions"])