Why do we need feature engeneering in TensorFlow?

Hi! Can you approve, please my thoughts. We could count artificual feature only by creating of function and passing to model input this func’s output. So, we need feature engeneering with Lambda layer only because we want to save full chain of transformations from raw data?

HI @someone555777

I’m not sure if i fully understand your question. But here some thoughts.

In TensorFlow (and other machine learning frameworks), feature engineering can indeed be performed by creating functions and passing their output as inputs to the model. These functions can be used to perform various transformations on the raw data, creating new features that are more suitable for the model to learn from.

The Lambda layer in TensorFlow is one way to incorporate custom functions or transformations directly into the model. It allows you to define a function and use it as a layer within the model architecture. This is particularly useful when you want to apply a simple operation to each data point individually, such as scaling, cropping, or reshaping the data.

However, feature engineering is not limited to just using Lambda layers for saving the full chain of transformations. It encompasses a broader range of techniques, including:

  • Manual Feature Engineering: Besides using Lambda layers, you can manually create new features based on domain knowledge or insights. For example, if you have a dataset of dates, you could extract features like day of the week, month, or year, which may be relevant for certain tasks.
  • Polynomial Features: You can generate polynomial features to capture non-linear relationships between variables. This can be done using libraries like scikit-learn’s PolynomialFeatures.
  • Encoding Categorical Variables: Besides Lambda layers, you may use one-hot encoding, label encoding, or other techniques to convert categorical variables into numerical representations.
  • Feature Scaling: While Lambda layers can handle basic scaling operations, more advanced scaling techniques like Min-Max scaling or Standardization can also be applied.
  • Handling Missing Data: You can use Lambda layers or other methods to impute missing values or discard samples with missing data.
  • Feature Selection: This involves choosing the most relevant features based on their importance scores or statistical significance.
  • Dimensionality Reduction: Applying techniques like PCA or t-SNE to reduce the number of features while preserving important information.

In summary, while Lambda layers in TensorFlow are one way to include custom functions and transformations within the model, feature engineering encompasses a broader set of techniques. It involves both manual and automated approaches to create meaningful and useful features from the raw data, which in turn helps the model to learn better and improve its performance on the given task.

Please, don’t take my thoughts as the “Only Truth” let’s discuss

best regards

ok, let me clearlify. We used in C3_W2_Lab_1_Manual_Dimensionality something like this

# Training dataset
trainds = load_dataset('/tmp/data/taxi-train*', TRAIN_BATCH_SIZE, 'train')

# Evaluation dataset
evalds = load_dataset('/tmp/data/taxi-valid*', 1000, 'eval').take(NUM_EVAL_EXAMPLES//1000)

# Needs to be specified since the dataset is infinite 
# This happens because the repeat method was used when creating the dataset

# Train the model and save the history
history = model.fit(trainds,

why only not to use
trainds = [... for ... trainds] or with function call?

and pass it to model.fit?

Why should we do all this strange manipulations only to change a bit our data?

def transform(inputs, numeric_cols):

    # Make a copy of the inputs to apply the transformations to
    transformed = inputs.copy()

    # Define feature columns
    feature_columns = {
        colname: tf.feature_column.numeric_column(colname)
        for colname in numeric_cols

    # Scaling longitude from range [-70, -78] to [0, 1]
    for lon_col in ['pickup_longitude', 'dropoff_longitude']:
        transformed[lon_col] = layers.Lambda(

    # Scaling latitude from range [37, 45] to [0, 1]
    for lat_col in ['pickup_latitude', 'dropoff_latitude']:
        transformed[lat_col] = layers.Lambda(

    # add Euclidean distance
    transformed['euclidean'] = layers.Lambda(
    # Add euclidean distance to feature columns
    feature_columns['euclidean'] = fc.numeric_column('euclidean')

    return transformed, feature_columns

Hi @someone555777

Thanks for sharing your thoughts. You got a good points.

In the code snippet you provided, the reason for performing these “strange manipulations” and transformations is the main goal of feature engineering. The goal is to preprocess and engineer new features from the raw data to improve the model’s performance and make it more effective in capturing the underlying patterns in the data.

Let’s break down the specific steps in the transform function:

  1. transformed = inputs.copy(): A copy of the original input data is made to avoid modifying the original data directly.
  2. feature_columns: This dictionary is created to define the feature columns used for training the model. Feature columns are a way to represent the features in a format that TensorFlow understands during the training process.
  3. layers.Lambda: The Lambda layers are used to apply custom transformations to specific features in the data. For example, the longitude and latitude values are scaled to a [0, 1] range using the scale_longitude and scale_latitude functions, respectively. This scaling can be helpful for certain algorithms that are sensitive to the magnitude of features.
  4. euclidean: A custom function euclidean is used to calculate the Euclidean distance between pickup and dropoff locations. This new feature, euclidean, is then added to the data.
  5. feature_columns['euclidean'] = fc.numeric_column('euclidean'): The newly engineered feature, euclidean, is added to the feature columns dictionary to make sure the model knows how to handle this new feature during training.

The reason why these transformations are done in this way rather than directly modifying the dataset (trainds) is to ensure the transformations are applied consistently during training and evaluation. By creating a copy of the inputs and performing the transformations within the transform function, you can maintain the original dataset (trainds) in its raw form and have a separate, transformed version (transformed) to use for training the model.

Using functions like layers.Lambda within the model architecture is a convenient way to include custom transformations directly within the neural network, making it easier to define and train complex models with TensorFlow.

In summary, the purpose of these “strange manipulations” and transformations is to perform feature engineering, which involves creating new features and preprocessing the data to improve the model’s ability to learn and make accurate predictions. By following this approach, you can maintain the original data integrity and apply consistent transformations during the training process.

Best regards

I understand what do this “strange manipulations”, my question was about why we should do them and not only direct transformations of data before input of model. As I understand you wanted to explain me this here

So, as I understand have you approved my thoughts? Is this only one reason to use this “strange manipulations”?

Not exactly. The primary purpose of using the Lambda layer (or any custom layer) in feature engineering is not specifically to “save the full chain of transformations” from raw data. Instead, the Lambda layer is used to apply custom transformations directly within the neural network architecture to preprocess the data before it enters the subsequent layers of the model.

Feature engineering, in general, involves creating new features or transforming existing features to better represent the underlying patterns in the data. This process can be done outside the model using various techniques and methods. However, when using TensorFlow or other deep learning frameworks, the Lambda layer allows you to perform custom transformations directly within the model itself, making it more convenient and efficient.

Here’s why we use the Lambda layer for feature engineering:

  • Seamless Integration: The Lambda layer allows you to incorporate custom transformations as part of the neural network. It seamlessly integrates these transformations with the rest of the model’s layers, making it easier to build and train the model.

  • End-to-End Training: By using the Lambda layer for feature engineering, the entire model, including the data preprocessing steps, can be trained end-to-end using automatic differentiation and backpropagation.

  • Custom Transformations: Feature engineering may require applying specific transformations to the data that are not readily available as built-in layers in TensorFlow. The Lambda layer allows you to define and use custom functions as part of the model architecture.

  • Flexibility: Since the Lambda layer accepts any Python function, it provides great flexibility in designing custom transformations, making it suitable for a wide range of feature engineering tasks.

In summary, while feature engineering can be done outside the model, using the Lambda layer (or custom layers) within the model provides a more integrated and efficient approach. It allows you to apply custom transformations directly within the neural network, making it easier to preprocess the data and capture important patterns effectively during the model training process.

Still not understand why why shouldn’t to do all of this in Tensorflow input pipline with this Lambda layer.

We can do all of this manipulations by simple python on data before sending to input pipeline and Lambda layers, isn’t it?

The approach It will depend on the nature of the transformations. Lambda layers are used to incorporate specific transformations directly into the neural network, enabling dynamic application during training and inference. This is valuable when the transformation is closely linked to the model’s architecture and requires per-sample processing during training.

On the other hand, general data preprocessing, like scaling, normalization, or feature extraction, can be done outside the model using Python. This approach offers separation between data preparation and model architecture, which can enhance code modularity and readability.

Both methods can achieve similar results, but your choice should align with the complexity and relationship of the transformations to the model. Simple preprocessing steps are suited for Python-based data manipulation, while Lambda layers are fitting for transformations that are integral to the neural network’s computation.

Can you clearlify a bit, what you mean under “enabling dynamic application”? Can you give me examples? I would like to know, when the working with data is easier with Lambda layers or unreal without it just with python?

Hi @someone555777

“Enabling dynamic application” in the context of Lambda layers refers to the ability to apply specific transformations directly within the neural network architecture during both training and inference stages. This dynamic application is valuable when the transformation is closely tied to the architecture of the neural network and requires processing on a per-sample basis during training.

For example:

Consider training a recurrent neural network (RNN) for natural language processing. To preprocess text data, you might want to convert words into word embeddings before feeding them to the RNN. With Lambda layers, you can include the embedding process as a part of the neural network. This means that during both training and inference, the network can dynamically convert words to embeddings on the fly, making the entire process seamless and integrated.

That means that Lambda layers are particularly beneficial when the transformation is tightly integrated with the model’s architecture and needs to be applied dynamically on a sample-by-sample basis. They work well when the transformation is complex and requires specialized operations that can be encapsulated within the neural network itself.

On the other hand, general data preprocessing tasks like scaling, normalization, or basic feature extraction are more suited to be handled outside the model using Python. These preprocessing steps are often independent of the neural network’s architecture and can be applied uniformly to the entire dataset. Using Python for these tasks offers more flexibility, as it allows you to easily manipulate data without being constrained by the model’s architecture.

In summary, Lambda layers are useful when transformations are closely tied to the neural network’s architecture and require dynamic application on a per-sample basis during training and inference. Python-based preprocessing is preferable for tasks that are more generic and can be applied uniformly to the dataset, providing greater code modularity and readability. The choice between the two methods depends on the complexity and integration of the transformation with the model.

I hope I clarified your question.

best regards

But I can do embeddings before the inputing to NN too. Still can’t understand the situations when I should use Lambda and not just python before. And I don’t very understand why do you see approach with Lambda as encapsulated? We will see this func that used in Lambda layer the same as python, isn’t it?

Lambda layers are most useful when the transformation you’re applying is closely tied to the architecture of the neural network and needs to be dynamically applied during both training and inference. The decision to use Lambda layers should be based on whether the transformation is an integral part of the neural network’s processing flow and requires specialized operations. Examples of such transformations include custom activation functions, complex data manipulations that are specific to the model, or operations that need to be applied sample by sample.

On the other hand, using Python-based preprocessing is appropriate when the transformation is more generic and doesn’t depend on the architecture of the neural network. These preprocessing steps can be applied uniformly to the entire dataset before feeding it into the neural network. This approach provides more flexibility for data manipulation that is not tightly linked to the neural network’s computation.

You’re correct that the function used in a Lambda layer is essentially similar to Python functions. The term “encapsulation” here refers to the idea that complex transformations or operations can be encapsulated directly within the neural network’s architecture using Lambda layers. This means that the transformation logic is embedded within the network structure itself, making it a part of the neural network’s computation. This can lead to more modular and compact code, especially when the transformation is closely tied to the network’s processing.

To provide a clearer perspective:

  • If the transformation is standard (like scaling, normalization) and can be applied before feeding the data into the network, you can use Python-based preprocessing.
  • If the transformation is complex, tightly linked to the network’s architecture, and needs to be dynamically applied during training and inference, Lambda layers are a suitable choice.

Think of Lambda layers as a way to integrate specialized data manipulations directly into the network’s flow, making the code more focused and aligned with the neural network’s architecture. However, it’s essential to evaluate the complexity of the transformation and its relationship to the network before deciding whether to use Lambda layers or Python-based preprocessing.

so, can you give me an example?

It is kind a hard to give you a example without current working on a project. Just keep in mind that the approach must be suitable for your project, since It is not a general concept.

Again, the lambda layer applicability depends of your goals (desire outputs). It is a tool, don’t take it as dogma (rule).

It is something that you have to apply during a experiment and depends of some factors, like your Neural Network Architecture, for example.

My tip for you is: work on prototypes, do experiments and choose the best approach for your project.

best regards