Home > Net >  How to print the results of a Tensorflow classifier training method
How to print the results of a Tensorflow classifier training method

Time:08-30

I have this code for trying to train a dataset using the classification technique ->

CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica'] # Lets define some constants to help us later on

train_path = tf.keras.utils.get_file(
    "iris_training.csv", 
    "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
test_path = tf.keras.utils.get_file(
    "iris_test.csv", 
    "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")

# Here we use keras (a module inside of TensorFlow) to grab our datasets and 
# read them into a pandas dataframe 

train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0) 

train.rename(columns={'120': 'SepalLength', '4': 'SepalWidth',
                      'setosa': 'PetalLength', 'versicolor': 'PetalWidth',
                      'virginica': 'Species'}, inplace=True)
test.rename(columns={'30': 'SepalLength', '4': 'SepalWidth',
                     'setosa': 'PetalLength', 'versicolor': 'PetalWidth',
                     'virginica': 'Species'}, inplace=True)

train_y = train.pop('Species')
test_y = test.pop('Species')
train.head() # the species column should now be gone

def input_fn(features, labels, training=True, batch_size=256):
  # Convert the inputs to a Dataset. 
  dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

  # Shuffle and repeat if you are in training mode. 
  if training:
    dataset = dataset.shuffle(1000).repeat()
  return dataset.batch(batch_size)

# Feature columns describe how to use the input.
my_feature_columns = [] 
for key in train.keys():
  my_feature_columns.append(tf.feature_column.numeric_column(key=key))

print(my_feature_columns) 

# Build a DNN with 2 hidden layers with 30 and 10 hidden nodes each. 
classifier = tf.estimator.DNNClassifier(
  feature_columns=my_feature_columns,  
  hidden_units=[30, 10], # Two hidden layers of 30 and 10 nodes respectively.
  n_classes=3) # The model must choose between 3 classes.

classifier.train(
    input_fn=lambda: input_fn(train, train_y, training=True),
    steps=5000) # defines the set amount of instructions, or things looked at

# we include a lambda to avoid creating an inner function previously

# TODO: print loss and steps

I am trying to follow the Freecodecamp machine learning course, and after running the train method, there is information printed to the console information like so ->

enter image description here

However, this doesn't happen when I run this in a google colab notebook. How do I print the loss and steps when moving through the loss function? Thanks for the help

CodePudding user response:

Add the following line to your code, after importing tensorflow and before training the model.

tf.get_logger().setLevel('INFO')
  • Related