-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathann.py
28 lines (23 loc) · 998 Bytes
/
ann.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tensorflow as tf
from tensorflow.keras import layers, models
# Load CIFAR-10 dataset
(X_train, y_train), (X_val, y_val) = tf.keras.datasets.cifar10.load_data()
X_train, X_val = X_train / 255.0, X_val / 255.0
train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(10000).batch(64)
val_dataset = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(64)
# Define the ANN model
def create_ann():
model = models.Sequential([
layers.Flatten(input_shape=(32, 32, 3)),
layers.Dense(512, activation='relu'),
layers.Dropout(0.2),
layers.Dense(256, activation='relu'),
layers.Dense(10, activation='softmax')
])
return model
# Compile and train the model
ann_model = create_ann()
ann_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ann_model.fit(train_dataset, validation_data=val_dataset, epochs=10)
# Evaluate
ann_model.evaluate(val_dataset)