0
0
TensorFlowml~20 mins

Numpy interoperability in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Numpy interoperability
Problem:You want to use TensorFlow to build a simple model but also use NumPy arrays for data input and output. Currently, you are unsure how to convert between TensorFlow tensors and NumPy arrays correctly.
Current Metrics:No model training yet; the problem is about data conversion and interoperability.
Issue:You are not able to seamlessly convert TensorFlow tensors to NumPy arrays and vice versa, which causes confusion and errors when feeding data or reading results.
Your Task
Learn how to convert NumPy arrays to TensorFlow tensors and convert TensorFlow tensors back to NumPy arrays correctly. Then build a simple linear model using TensorFlow, train it on NumPy data, and output predictions as NumPy arrays.
Use TensorFlow 2.x eager execution (default).
Use NumPy arrays as input data and output predictions.
Do not use deprecated TensorFlow APIs.
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
TensorFlow
import numpy as np
import tensorflow as tf

# Create NumPy input data
X_np = np.array([[1.0], [2.0], [3.0], [4.0]], dtype=np.float32)
y_np = np.array([[2.0], [4.0], [6.0], [8.0]], dtype=np.float32)

# Convert NumPy arrays to TensorFlow tensors
X_tf = tf.convert_to_tensor(X_np)
y_tf = tf.convert_to_tensor(y_np)

# Build a simple linear model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=(1,))
])

# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')

# Train the model on NumPy data
model.fit(X_np, y_np, epochs=100, verbose=0)

# Make predictions (TensorFlow tensors)
predictions_tf = model(X_tf)

# Convert predictions to NumPy arrays
predictions_np = predictions_tf.numpy()

# Print results
print('Predictions as TensorFlow tensors:', predictions_tf)
print('Predictions as NumPy arrays:', predictions_np)
Converted NumPy arrays to TensorFlow tensors using tf.convert_to_tensor.
Built a simple tf.keras.Sequential model with one Dense layer.
Trained the model directly on NumPy arrays.
Converted model predictions from TensorFlow tensors back to NumPy arrays using .numpy() method.
Results Interpretation

Before: Confusion about how to convert between NumPy arrays and TensorFlow tensors caused errors.

After: Using tf.convert_to_tensor and .numpy() methods allows smooth conversion. Model trains on NumPy data and outputs predictions as NumPy arrays.

TensorFlow and NumPy work well together. You can easily convert data back and forth, making it simple to use TensorFlow models with NumPy data.
Bonus Experiment
Try using TensorFlow Dataset API to feed NumPy data to the model instead of passing NumPy arrays directly.
💡 Hint
Use tf.data.Dataset.from_tensor_slices() to create a dataset from NumPy arrays and then batch it before training.