以下示範如何用MNIST數據集,訓練手寫數字辨識模型。

步驟一、開啟 https://mls.pixetto.ai ,點擊〔Python〕。

步驟二、創建Python文件。

步驟四、貼上以下程式碼,輸入 [SHIFT]+[ENTER] 執行。執行完畢將產生名為 mnist.tflite 的網路模型。

from pixetto import mls
import tensorflow as tf
from tensorflow.keras import backend as K
import numpy as np
mnist = tf.keras.datasets.mnist
K.set_learning_phase(0)
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train[:,:,:, np.newaxis]
x_test = x_test[:,:,:, np.newaxis]
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10)
model.evaluate(x_test, y_test)
mls.save_model_as_tflite(model, 'mnist.tflite')

操作影片