-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_functional_api.py
More file actions
37 lines (22 loc) · 862 Bytes
/
08_functional_api.py
File metadata and controls
37 lines (22 loc) · 862 Bytes
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
29
30
31
32
33
34
35
36
37
from tensorflow import keras
import pandas as pd
import matplotlib.pyplot as plt
# 函数式API
(train_image, train_label), (test_image, test_label) = keras.datasets.fashion_mnist.load_data()
train_image = train_image / 255.0
test_image = test_image / 255.0
input1 = keras.Input(shape=(28, 28))
# 直接调用该层
x = keras.layers.Flatten()(input1)
# 直接调用该层
x = keras.layers.Dense(32, activation='relu')(x)
x = keras.layers.Dropout(0.5)(x)
x = keras.layers.Dense(64, activation='relu')(x)
output = keras.layers.Dense(10, activation='softmax')(x)
model = keras.Model(input1, output)
model.summary()
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_image, train_label, epochs=10)
test_loss, test_acc = model.evaluate(test_image, test_label)
print(test_loss)
print(test_acc)