Add docs/tech_docs/python/TensorFlow.md
This commit is contained in:
102
docs/tech_docs/python/TensorFlow.md
Normal file
102
docs/tech_docs/python/TensorFlow.md
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
For deep learning and neural network creation, `TensorFlow` stands as a cornerstone in the Python ecosystem. Developed by the Google Brain team, TensorFlow is an open-source library that allows developers to build and train complex machine learning models with ease. Its flexible architecture permits deployment across a variety of platforms, from servers to edge devices. Here's a concise reference guide for common use cases with `TensorFlow`, especially tailored for building and training machine learning models:
|
||||||
|
|
||||||
|
# `TensorFlow` Reference Guide
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```
|
||||||
|
pip install tensorflow
|
||||||
|
```
|
||||||
|
Note: For GPU support, use `tensorflow-gpu` instead, and ensure you have the necessary CUDA and cuDNN libraries installed.
|
||||||
|
|
||||||
|
## Basic Concepts
|
||||||
|
|
||||||
|
### Importing TensorFlow
|
||||||
|
```python
|
||||||
|
import tensorflow as tf
|
||||||
|
```
|
||||||
|
|
||||||
|
### Creating Tensors
|
||||||
|
```python
|
||||||
|
# Constant tensor
|
||||||
|
tensor = tf.constant([[1, 2], [3, 4]])
|
||||||
|
|
||||||
|
# Variable tensor
|
||||||
|
variable = tf.Variable([[1, 2], [3, 4]])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Operations with Tensors
|
||||||
|
```python
|
||||||
|
# Addition
|
||||||
|
result = tf.add(tensor, tensor)
|
||||||
|
|
||||||
|
# Element-wise multiplication
|
||||||
|
result = tf.multiply(tensor, tensor)
|
||||||
|
|
||||||
|
# Matrix multiplication
|
||||||
|
result = tf.matmul(tensor, tensor)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building Neural Networks
|
||||||
|
|
||||||
|
### Defining a Sequential Model
|
||||||
|
```python
|
||||||
|
model = tf.keras.Sequential([
|
||||||
|
tf.keras.layers.Flatten(input_shape=(28, 28)), # Input layer
|
||||||
|
tf.keras.layers.Dense(128, activation='relu'), # Hidden layer
|
||||||
|
tf.keras.layers.Dropout(0.2),
|
||||||
|
tf.keras.layers.Dense(10) # Output layer
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
### Compiling the Model
|
||||||
|
```python
|
||||||
|
model.compile(optimizer='adam',
|
||||||
|
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
|
||||||
|
metrics=['accuracy'])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Training and Evaluating Models
|
||||||
|
|
||||||
|
### Training the Model
|
||||||
|
```python
|
||||||
|
model.fit(train_images, train_labels, epochs=5)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Evaluating the Model
|
||||||
|
```python
|
||||||
|
model.evaluate(test_images, test_labels, verbose=2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Making Predictions
|
||||||
|
```python
|
||||||
|
probability_model = tf.keras.Sequential([
|
||||||
|
model,
|
||||||
|
tf.keras.layers.Softmax()
|
||||||
|
])
|
||||||
|
|
||||||
|
predictions = probability_model.predict(test_images)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Saving and Loading Models
|
||||||
|
```python
|
||||||
|
# Save the entire model
|
||||||
|
model.save('my_model.h5')
|
||||||
|
|
||||||
|
# Load the model
|
||||||
|
new_model = tf.keras.models.load_model('my_model.h5')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Working with Data
|
||||||
|
```python
|
||||||
|
# Load a dataset
|
||||||
|
mnist = tf.keras.datasets.mnist
|
||||||
|
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
|
||||||
|
|
||||||
|
# Normalize the data
|
||||||
|
train_images, test_images = train_images / 255.0, test_images / 255.0
|
||||||
|
```
|
||||||
|
|
||||||
|
`TensorFlow` provides a comprehensive ecosystem of tools, libraries, and community resources that lets researchers push the state-of-the-art in ML, and developers easily build and deploy ML-powered applications. This guide covers the basics of TensorFlow for machine learning, including creating tensors, building and training neural network models, and saving/loading models. TensorFlow's capabilities are extensive and support a wide range of tasks beyond what's covered here, making it an essential tool for modern machine learning development.
|
||||||
|
```
|
||||||
|
|
||||||
|
TensorFlow's robust and scalable nature makes it suitable for both research and production, empowering users to transition seamlessly from concept to code, to training, to deployment.
|
||||||
Reference in New Issue
Block a user