9951 explained code solutions for 126 technologies


python-tensorflowHow do I create a neural network using Python and TensorFlow?


Creating a neural network using Python and TensorFlow is a simple process. Here is an example of a basic neural network:

import tensorflow as tf

# Define the model
model = tf.keras.Sequential([
  tf.keras.layers.Dense(units=1, input_shape=[1])
])

# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')

# Provide the data
xs = [1, 2, 3, 4, 5]
ys = [1, 1.5, 2, 2.5, 3]

# Train the model
model.fit(xs, ys, epochs=500)

# Make a prediction
print(model.predict([7.0]))

# Output
[[4.0037344]]

The code above creates a simple neural network using TensorFlow's Sequential model. The Sequential model is a linear stack of layers. The first layer is a Dense layer with 1 unit and an input shape of 1. The model is then compiled with the sgd (stochastic gradient descent) optimizer and mean_squared_error loss function. The dataset is provided in the xs and ys variables. Finally, the model is trained with the fit function for 500 epochs. After training, the model can be used to make predictions, as demonstrated by the predict function.

The parts of the code are as follows:

  1. import tensorflow as tf - imports the TensorFlow library.
  2. model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])]) - creates a Sequential model with a Dense layer.
  3. model.compile(optimizer='sgd', loss='mean_squared_error') - compiles the model with the sgd optimizer and mean_squared_error loss function.
  4. model.fit(xs, ys, epochs=500) - trains the model with the provided dataset for 500 epochs.
  5. print(model.predict([7.0])) - makes a prediction with the trained model.

For more information on creating neural networks with TensorFlow, please refer to the TensorFlow documentation.

Edit this code on GitHub