Update tf_keras.md

This commit is contained in:
Omar Santos 2023-09-05 21:42:32 -04:00 committed by GitHub
parent 08e17c9c19
commit a51d189b00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,30 +17,31 @@ To provide students with hands-on experience in developing, training, and evalua
**Setting Up the Environment**:
- Installing TensorFlow and Keras:
```bash
```bash
pip install tensorflow keras
```
```
**Image Data Preprocessing**:
- **Step 1**: Importing Necessary Libraries:
```python
- **Step 1**: Importing Necessary Libraries:
```python
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
```
```
- **Step 2**: Loading and Preprocessing Image Data:
```python
- **Step 2**: Loading and Preprocessing Image Data:
```python
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
```
```
**Building a Convolutional Neural Network (CNN)**:
- **Step 3**: Defining the CNN Architecture:
```python
- **Step 3**: Defining the CNN Architecture:
```python
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
@ -48,34 +49,34 @@ To provide students with hands-on experience in developing, training, and evalua
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu')
])
```
```
- **Step 4**: Adding Dense Layers:
```python
- **Step 4**: Adding Dense Layers:
```python
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
```
```
**Compiling and Training the Model**:
- **Step 5**: Compiling the Model:
```python
- **Step 5**: Compiling the Model:
```python
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
```
```
- **Step 6**: Training the Model:
```python
- **Step 6**: Training the Model:
```python
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
```
```
**Evaluating the Model**:
- **Step 7**: Evaluating the Model and Visualizing Results:
```python
- **Step 7**: Evaluating the Model and Visualizing Results:
```python
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
import matplotlib.pyplot as plt
@ -87,9 +88,7 @@ To provide students with hands-on experience in developing, training, and evalua
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.show()
```
```
## **Resources**