9951 explained code solutions for 126 technologies


python-tensorflowHow can I use the Python TensorFlow library to build a machine learning model?


Using the Python TensorFlow library, you can build a machine learning model by following these steps:

  1. Import the TensorFlow library and other necessary libraries:
import tensorflow as tf
import numpy as np
  1. Define the model parameters:
learning_rate = 0.01
training_epochs = 1000
  1. Create the input data and labels:
x_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_train = np.array([[0], [1], [1], [0]])
  1. Create the model and define the loss function:
model = tf.keras.Sequential([
    tf.keras.layers.Dense(2, activation='relu', input_shape=(2,)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate),
              loss='binary_crossentropy',
              metrics=['accuracy'])
  1. Train the model:
model.fit(x_train, y_train, epochs=training_epochs)
  1. Test the model:
test_data = np.array([[1, 1]])
model.predict(test_data)

Output example

array([[0.00244538]], dtype=float32)
  1. Save the model:
model.save('my_model.h5')

Helpful links

Edit this code on GitHub