9951 explained code solutions for 126 technologies


python-tensorflowHow can I use Python and TensorFlow to create a neural network?


Using Python and TensorFlow to create a neural network is a relatively simple process.

First, you need to import the necessary packages. This includes TensorFlow and any other packages you may need:

import tensorflow as tf
import numpy as np

Next, you need to define the layers of the neural network. This includes the number of neurons and the activation functions:

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

Then, you need to compile the model. This includes specifying the loss function, optimizer, and any metrics you may want to track:

model.compile(
    loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['accuracy']
)

Next, you need to train the model. This includes specifying the training data, batch size, and number of epochs:

model.fit(x_train, y_train, batch_size=32, epochs=10)

Epoch 1/10
1875/1875 [==============================] - 2s 890us/step - loss: 0.4555 - accuracy: 0.8357
...
Epoch 10/10
1875/1875 [==============================] - 2s 881us/step - loss: 0.1652 - accuracy: 0.9456

Finally, you need to evaluate the model. This includes specifying the test data and any metrics you want to track:

model.evaluate(x_test, y_test, batch_size=32)

313/313 [==============================] - 0s 1ms/step - loss: 0.3520 - accuracy: 0.8975

Code explanation

  1. import tensorflow as tf - imports the TensorFlow package.
  2. import numpy as np - imports the NumPy package.
  3. tf.keras.Sequential() - creates a sequential neural network model.
  4. tf.keras.layers.Dense() - creates a fully-connected layer of neurons.
  5. model.compile() - compiles the model with the specified loss function, optimizer, and metrics.
  6. model.fit() - trains the model with the specified training data, batch size, and number of epochs.
  7. model.evaluate() - evaluates the model with the specified test data and metrics.

Helpful links

Edit this code on GitHub