栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

keras demo

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

keras demo

import os
import tensorflow as tf
from tensorflow import keras

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(-1, 28, 28, 1)
x_train = x_train.astype("float32")
# print(x_train.shape)
y_train = y_train.astype("float32")
x_test = x_test.reshape(-1,28,28,1)
x_test = x_test.astype("float32")
y_test = y_test.astype("float32")

# print(y_train)
x_train /= 255
x_test /= 255

from tensorflow.keras.utils import to_categorical
y_train_new = to_categorical(num_classes=10,y=y_train)
print(y_train_new)
y_test_new = to_categorical(num_classes=10,y=y_test)

def LeNet_5():
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Conv2D(filters=6,kernel_size=(5,5),padding="valid",activation="tanh",input_shape=[28, 28, 1]))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
    model.add(tf.keras.layers.Conv2D(filters=16,kernel_size=(5,5),padding="valid",activation="tanh"))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(120,activation="tanh"))
    model.add(tf.keras.layers.Dense(84,activation="tanh"))
    model.add(tf.keras.layers.Dense(10,activation="softmax"))
    return model

def create_model():
    model = LeNet_5()
    model.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"])
    model.fit(x_train,y_train_new,batch_size=64,epochs=1,verbose=1,validation_split=0.2,shuffle=True)
    return model

# Create and train a new model instance.
model = create_model()
model.fit(x_train, y_train_new, epochs=5)

# Save the entire model as a SavedModel.
model.save('saved_model/my_model')


# # checkpoint save ckpt
# # Include the epoch in the file name (uses `str.format`)
# checkpoint_path = "training/cp-{epoch:04d}.ckpt"
# checkpoint_dir = os.path.dirname(checkpoint_path)

# batch_size = 32

# # Create a callback that saves the model's weights every 5 epochs
# cp_callback = tf.keras.callbacks.ModelCheckpoint(
#     filepath=checkpoint_path, 
#     verbose=1, 
#     save_weights_only=True,
#     save_freq=5*batch_size)

# # Create a new model instance
# model = create_model()

# # Save the weights using the `checkpoint_path` format
# model.save_weights(checkpoint_path.format(epoch=0))

# # Train the model with the new callback
# model.fit(x_train, 
#           y_train_new,
#           epochs=50, 
#           batch_size=batch_size, 
#           callbacks=[cp_callback],
#           validation_data=(x_test, y_test_new),
#           verbose=0)

# # latest checkpoint
# latest = tf.train.latest_checkpoint(checkpoint_dir)

# # Create a new model instance
# model = create_model()

# # Load the previously saved weights
# model.load_weights(latest)

# # Re-evaluate the model
# loss, acc = model.evaluate(x_test, y_test_new, verbose=2)
# print("Restored model, accuracy: {:5.2f}%".format(100 * acc))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655636.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号