kopia lustrzana https://github.com/animator/learn-python
2.4 KiB
2.4 KiB
TensorFlow
Developed by the Google Brain team, TensorFlow is an open-source library that provides a comprehensive ecosystem for building and deploying machine learning models. It supports deep learning and neural networks and offers tools for both beginners and experts.
Key Features
- Flexible and comprehensive ecosystem
- Scalable for both production and research
- Supports CPUs, GPUs, and TPUs
Basic Example: Linear Regression
Let's start with a simple linear regression example in TensorFlow.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Generate synthetic data
X = np.array([1, 2, 3, 4, 5], dtype=np.float32)
Y = np.array([2, 4, 6, 8, 10], dtype=np.float32)
# 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')
# Train the model
history = model.fit(X, Y, epochs=500)
# Predict
predictions = model.predict(X)
# Plot the results
plt.plot(X, Y, 'ro', label='Original data')
plt.plot(X, predictions, 'b-', label='Fitted line')
plt.legend()
plt.show()
In this example:
- We define a simple dataset with a linear relationship.
- We build a sequential model with one dense layer (linear regression).
- We compile the model with stochastic gradient descent (SGD) optimizer and mean squared error loss.
- We train the model for 500 epochs and then plot the original data and the fitted line.
When to Use TensorFlow
TensorFlow is a great choice if you:
- Need to deploy machine learning models in production: TensorFlow’s robust deployment options, including TensorFlow Serving, TensorFlow Lite, and TensorFlow.js, make it ideal for production environments.
- Work on large-scale deep learning projects: TensorFlow’s comprehensive ecosystem supports distributed training and has tools like TensorBoard for visualization.
- Require high performance and scalability: TensorFlow is optimized for performance and can leverage GPUs and TPUs for accelerated computing.
- Want extensive support and documentation: TensorFlow has a large community and extensive documentation, which can be very helpful for both beginners and advanced users.
Example Use Cases
- Building and deploying complex neural networks for image recognition, natural language processing, or recommendation systems.
- Developing models that need to be run on mobile or embedded devices.