# TensorFlow building and training a simple model: Exercises and solutions

## Python TensorFlow building and training a simple model [9 exercises with solution]

[** An editor is available at the bottom of the page to write and execute the scripts.** Go to the editor]

**Creating a Computational Graph:**

**1.** Write a Python program that creates a TensorFlow placeholder for input data of shape (None, 20) where "None" represents a variable batch size.

Click me to see the sample solution

**2.** Write a Python program that defines a TensorFlow constant tensor containing weights for a neural network layer with shape (10, 4).

Click me to see the sample solution

**3.** Write a Python program that creates a TensorFlow operation to perform matrix multiplication between two tensors.

Click me to see the sample solution

**4.** Write a Python program that builds a feedforward neural network using TensorFlow with one hidden layer and a sigmoid activation function.

Click me to see the sample solution

**5.** Write a Python program that creates a TensorFlow placeholder for a 3D tensor representing images with dimensions (batch_size, height, width).

Click me to see the sample solution

**Defining a Loss Function:**

**6.** Write a Python program that defines a mean squared error (MSE) loss function using TensorFlow for a regression task.

Click me to see the sample solution

**7.** Write a Python program that implements a categorical cross-entropy loss function using TensorFlow for a multi-class classification problem.

Click me to see the sample solution

**8.** Write a Python program that creates a custom loss function using TensorFlow that penalizes errors differently for positive and negative examples.

Click me to see the sample solution

**9.** Write a Python program that creates a custom loss function in TensorFlow that penalizes errors differently for positive and negative examples.

Click me to see the sample solution

**Gradient Descent Optimization:**

**10.** Write a Python program that implements a gradient descent optimizer using TensorFlow for a simple linear regression model.

Click me to see the sample solution

**11.** Write a Python program that implements a gradient descent optimizer using TensorFlow for a simple linear regression model.

Click me to see the sample solution

**12.** Write a Python program to experiment with different learning rates in a gradient descent optimizer and observe the impact on convergence in TensorFlow.

Click me to see the sample solution

**Python Code Editor:**

[ Want to contribute to Python exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

**It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.**

https://www.w3resource.com/machine-learning/tensorflow/tensorflow_building_and_training_sample_model.php

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics