-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path06_01_03_keras_digits_api.py
45 lines (37 loc) · 1.51 KB
/
06_01_03_keras_digits_api.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
import numpy as np
from keras.models import Sequential, load_model
from keras.layers import Dense, Flatten
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
model = Sequential()
# 64 entradas
model.add(Flatten(input_shape=(64,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# (**For your API application**)
# Load the saved model (assuming the file is in the same directory)
loaded_model = load_model('my_model.h5')
# Use the loaded model for predictions in your API
# Example prediction (replace with your actual prediction logic):
new_data = np.array([[0., 0., 0.125, 0.9375, 0.9375, 1.,
0.6875, 0., 0., 0.,
0.5, 1., 0.6875, 0.1875, 0., 0., 0.,
0., 0.8125, 0.5625,
0., 0., 0., 0., 0., 0.3125,
1., 0.1875, 0.5625, 0.6875,
0.1875, 0., 0., 0.625, 0.9375, 0.9375, 1.,
1., 0.6875, 0.,
0., 0.375, 1., 0.625, 0.4375, 1.,
0.3125, 0., 0., 0.,
0.1875, 0.25, 0.9375, 0.5, 0., 0., 0.,
0., 0.25, 0.9375,
0.4375, 0., 0., 0.]
])
new_data = new_data.reshape((1, 64)) # Reshape to (1, 64)
prediction = loaded_model.predict(new_data)
print(prediction)