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))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Keras是一个开源的神经网络库,可以用于构建和训练各种类型的深度学习模型。多层LSTM是指在Keras中使用多个LSTM层来构建神经网络模型。 通过引用提供的链接,您可以找到一个基于Keras的多层LSTM实现的数据集的示例代码。这个代码示例展示了如何使用Keras库来构建一个多层LSTM模型,并将其应用于某个数据集。 在这个示例代码中,作者首先导入了所需的库和模块,然后定义了一个LSTM模型。模型中包含了多个LSTM层,这些层可以用来处理不同的时间步长的输入数据。接下来,作者加载了一个数据集,并对其进行预处理。然后,他们将数据分为训练集和测试集,并将其输入到LSTM模型中进行训练和测试。 如果您对具体的代码实现感兴趣,可以点击链接查看详细代码并学习如何使用Keras构建和训练多层LSTM模型。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [基于keras多层LSTM实现数据集](https://download.csdn.net/download/chaochaopang0/10405315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Keras的一些简单的Demo](https://download.csdn.net/download/weixin_44651576/10954049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值