mirror of
https://github.com/The-Art-of-Hacking/h4cker.git
synced 2024-12-18 10:56:09 +00:00
Update tf_keras.md
This commit is contained in:
parent
08e17c9c19
commit
a51d189b00
@ -17,30 +17,31 @@ To provide students with hands-on experience in developing, training, and evalua
|
|||||||
|
|
||||||
**Setting Up the Environment**:
|
**Setting Up the Environment**:
|
||||||
- Installing TensorFlow and Keras:
|
- Installing TensorFlow and Keras:
|
||||||
```bash
|
|
||||||
|
```bash
|
||||||
pip install tensorflow keras
|
pip install tensorflow keras
|
||||||
```
|
```
|
||||||
|
|
||||||
**Image Data Preprocessing**:
|
**Image Data Preprocessing**:
|
||||||
|
|
||||||
- **Step 1**: Importing Necessary Libraries:
|
- **Step 1**: Importing Necessary Libraries:
|
||||||
```python
|
```python
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
from tensorflow.keras import datasets, layers, models
|
from tensorflow.keras import datasets, layers, models
|
||||||
```
|
```
|
||||||
|
|
||||||
- **Step 2**: Loading and Preprocessing Image Data:
|
- **Step 2**: Loading and Preprocessing Image Data:
|
||||||
```python
|
```python
|
||||||
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
|
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
|
||||||
|
|
||||||
# Normalize pixel values to be between 0 and 1
|
# Normalize pixel values to be between 0 and 1
|
||||||
train_images, test_images = train_images / 255.0, test_images / 255.0
|
train_images, test_images = train_images / 255.0, test_images / 255.0
|
||||||
```
|
```
|
||||||
|
|
||||||
**Building a Convolutional Neural Network (CNN)**:
|
**Building a Convolutional Neural Network (CNN)**:
|
||||||
|
|
||||||
- **Step 3**: Defining the CNN Architecture:
|
- **Step 3**: Defining the CNN Architecture:
|
||||||
```python
|
```python
|
||||||
model = models.Sequential([
|
model = models.Sequential([
|
||||||
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
|
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
|
||||||
layers.MaxPooling2D((2, 2)),
|
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.MaxPooling2D((2, 2)),
|
||||||
layers.Conv2D(64, (3, 3), activation='relu')
|
layers.Conv2D(64, (3, 3), activation='relu')
|
||||||
])
|
])
|
||||||
```
|
```
|
||||||
|
|
||||||
- **Step 4**: Adding Dense Layers:
|
- **Step 4**: Adding Dense Layers:
|
||||||
```python
|
```python
|
||||||
model.add(layers.Flatten())
|
model.add(layers.Flatten())
|
||||||
model.add(layers.Dense(64, activation='relu'))
|
model.add(layers.Dense(64, activation='relu'))
|
||||||
model.add(layers.Dense(10))
|
model.add(layers.Dense(10))
|
||||||
```
|
```
|
||||||
|
|
||||||
**Compiling and Training the Model**:
|
**Compiling and Training the Model**:
|
||||||
|
|
||||||
- **Step 5**: Compiling the Model:
|
- **Step 5**: Compiling the Model:
|
||||||
```python
|
```python
|
||||||
model.compile(optimizer='adam',
|
model.compile(optimizer='adam',
|
||||||
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
||||||
metrics=['accuracy'])
|
metrics=['accuracy'])
|
||||||
```
|
```
|
||||||
|
|
||||||
- **Step 6**: Training the Model:
|
- **Step 6**: Training the Model:
|
||||||
```python
|
```python
|
||||||
history = model.fit(train_images, train_labels, epochs=10,
|
history = model.fit(train_images, train_labels, epochs=10,
|
||||||
validation_data=(test_images, test_labels))
|
validation_data=(test_images, test_labels))
|
||||||
```
|
```
|
||||||
|
|
||||||
**Evaluating the Model**:
|
**Evaluating the Model**:
|
||||||
|
|
||||||
- **Step 7**: Evaluating the Model and Visualizing Results:
|
- **Step 7**: Evaluating the Model and Visualizing Results:
|
||||||
```python
|
```python
|
||||||
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
|
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
|
||||||
|
|
||||||
import matplotlib.pyplot as plt
|
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.ylim([0.5, 1])
|
||||||
plt.legend(loc='lower right')
|
plt.legend(loc='lower right')
|
||||||
plt.show()
|
plt.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## **Resources**
|
## **Resources**
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user