Search code examples
pythongoogle-colaboratorytensorflow2.0

I'm trying to define a function in Google Colab but im getting this error: "name 'train_data' is not defined"


here is the code that im running, the version of the tensorflow is 2.6.0

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from tensorflow.keras.utils import plot_model
X = tf.range(-100, 100, 4)
y = X + 10
# Split the data into train and test sets
X_train = X[:40] #first 40 are training samples
y_train = y[:40]

X_test = X[40:] # last 10 are testing samples
y_test = y[40:]
y_pred = model.predict(X_test)
def plot_predictions(train_data=X_train,
                     train_labels=y_train,
                     test_data=X_test,
                     test_labels=y_test,
                     peredictions=y_pred):
  '''
  Plots training data, test data and compares predictions to ground truth labels.
  '''
plt.figure(figsize=(10, 7))
# Plot training data in blue
plt.scatter(train_data, train_labels, c="b", labels="Training data")
# Plot testing data in green
plt.scatter(test_data, test_labels, c="g", labels="Testing data")
# Plot model's predictions in red
plt.scatter(test_data, predictions, c="r", labels="predictions")
# Show the legend
plt.legend();

and here is a screenshot from my code and the error in google colab:

enter image description here


Solution

  • thanks to the @MichaelSzczesny I did fix the code. it was needed a little space before the below lines. and the labels="Training data" was incorrect, the correct form is label="Training data". here is the correct and fixed code:

      plt.figure(figsize=(10, 7))
     # Plot training data in blue
      plt.scatter(train_data, train_labels, c="b", label="Training data")
     # Plot testing data in green
      plt.scatter(test_data, test_labels, c="g", label="Testing data")
     # Plot model's predictions in red
      plt.scatter(test_data, predictions, c="r", label="predictions")
     # Show the legend
      plt.legend();