0
0
TensorFlowml~10 mins

Loss functions (MSE, cross-entropy) in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the mean squared error loss from TensorFlow.

TensorFlow
from tensorflow.keras.losses import [1]
Drag options to blanks, or click blank then click option'
AMeanSquaredError
Bmean_squared_error
CMSE
DCrossEntropy
Attempts:
3 left
💡 Hint
Common Mistakes
Using function names instead of class names for import.
Confusing cross-entropy loss with mean squared error.
2fill in blank
medium

Complete the code to compile a model using mean squared error loss.

TensorFlow
model.compile(optimizer='adam', loss=[1], metrics=['accuracy'])
Drag options to blanks, or click blank then click option'
A'mean_squared_error'
B'hinge'
C'cross_entropy'
D'categorical_crossentropy'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cross-entropy loss names when mean squared error is needed.
Forgetting to put the loss name in quotes.
3fill in blank
hard

Fix the error in the code to use sparse categorical cross-entropy loss correctly.

TensorFlow
model.compile(optimizer='adam', loss=[1], metrics=['accuracy'])
Drag options to blanks, or click blank then click option'
A'mean_squared_error'
B'sparse_categorical_crossentropy'
C'binary_crossentropy'
D'categorical_crossentropy'
Attempts:
3 left
💡 Hint
Common Mistakes
Using categorical cross-entropy with sparse labels.
Using mean squared error for classification tasks.
4fill in blank
hard

Fill both blanks to create a model with binary cross-entropy loss and sigmoid activation.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, activation=[1])
])
model.compile(optimizer='adam', loss=[2], metrics=['accuracy'])
Drag options to blanks, or click blank then click option'
A'sigmoid'
B'relu'
C'binary_crossentropy'
D'mean_squared_error'
Attempts:
3 left
💡 Hint
Common Mistakes
Using relu activation for output layer in binary classification.
Using mean squared error loss for classification.
5fill in blank
hard

Fill all three blanks to define a custom loss function using mean squared error.

TensorFlow
import tensorflow as tf

def custom_loss(y_true, y_pred):
    return tf.keras.losses.[1](y_true, y_pred)

model.compile(optimizer='adam', loss=[2], metrics=[[3]])
Drag options to blanks, or click blank then click option'
Amean_squared_error
B'mean_squared_error'
C'accuracy'
D'categorical_accuracy'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the function call inside custom loss.
Using wrong metric names.