how to use tensorflow in python

To use tensorflow in Python, you first need to install it. You can do this using pip install command:

main.py
pip install tensorflow
23 chars
2 lines

Once installation is complete, you can import tensorflow in your Python script and start using it. Here is a simple example of using tensorflow to create a neural network:

main.py
import tensorflow as tf

# Define the neural network architecture
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(10, activation='softmax')
])

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

# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
print("Test accuracy:", accuracy)
518 chars
20 lines

This code defines a simple neural network with two dense layers and trains it on MNIST dataset. The model is compiled with 'adam' optimizer, 'categorical_crossentropy' loss function and 'accuracy' metric. It is then trained for 5 epochs on training data and evaluated on test data. Finally, the test accuracy is printed.

gistlibby LogSnag