神经网络保存-导入

保存

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import gzip
# fashion_mnist=tf.keras.datasets.fashion_mnist
# (train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()
 
#数据在个人资源里面,放到该文件目录中即可
def load_data():
#     dirname = os.path.join('datasets', 'fashion-mnist')
#     base = 'https://storage.googleapis.com/tensorflow/tf-ke ras-datasets/'
    files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
    ]
 
    paths = []
    for fname in files:
        paths.append(fname)
 
    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
 
    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
 
    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
 
    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
 
    return (x_train, y_train), (x_test, y_test)
(x_train, y_train), (x_test, y_test)=load_data()

x_train=np.expand_dims(x_train,-1)

y_train_one_hot=tf.one_hot(y_train,10).numpy()
x_train=np.float32(x_train)

model=tf.keras.Sequential([
    tf.keras.layers.Conv2D(1,3,1),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(64,activation="relu"),
    tf.keras.layers.Dense(32,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
 
model.build(input_shape=[None,28,28,1])
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[tf.keras.losses.CategoricalCrossentropy()])

import os
checkpoint_path="training_1/cp.ckpt"
cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,save_weights_only=True,verbose=1)


history=model.fit(x_train,y_train_one_hot,epochs=10,callbacks=[cp_callback])
LOSS=history.history["loss"]
plt.plot(LOSS)
plt.show()

导入

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import gzip
# fashion_mnist=tf.keras.datasets.fashion_mnist
# (train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()
 
#数据在个人资源里面,放到该文件目录中即可
def load_data():
#     dirname = os.path.join('datasets', 'fashion-mnist')
#     base = 'https://storage.googleapis.com/tensorflow/tf-ke ras-datasets/'
    files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
    ]
 
    paths = []
    for fname in files:
        paths.append(fname)
 
    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
 
    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
 
    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
 
    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
 
    return (x_train, y_train), (x_test, y_test)
(x_train, y_train), (x_test, y_test)=load_data()

x_test=np.expand_dims(x_test,-1)
model=tf.keras.Sequential([
    tf.keras.layers.Conv2D(1,3,1),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(64,activation="relu"),
    tf.keras.layers.Dense(32,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model.build(input_shape=[None,28,28,1])
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[tf.keras.losses.CategoricalCrossentropy()])

checkpoint_path="training_1/cp.ckpt"
model.load_weights(checkpoint_path)

x_test=np.array(x_test,dtype=np.float32)
print(np.argmax(model.predict(x_test),axis=1))
print(y_test)
np.sum((y_test==np.argmax(model.predict(x_test),axis=1))*1)/y_test.shape[0]

相关推荐

  1. 神经网络保存-导入

    2024-06-15 07:20:04       27 阅读
  2. 神经网络----网络模型的加载及保存

    2024-06-15 07:20:04       46 阅读
  3. 神经网络---网络模型的保存、加载

    2024-06-15 07:20:04       29 阅读
  4. 神经网络】深度神经网络

    2024-06-15 07:20:04       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 07:20:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 07:20:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 07:20:04       82 阅读
  4. Python语言-面向对象

    2024-06-15 07:20:04       91 阅读

热门阅读

  1. textarea 中的内容在word中显示换行不起作用

    2024-06-15 07:20:04       27 阅读
  2. 【已解决】npm ERR! cb() never called!

    2024-06-15 07:20:04       29 阅读
  3. 扩展学习|高校风险管理研究综述

    2024-06-15 07:20:04       27 阅读
  4. 探索C嘎嘎的奇妙世界:第二关---C++的输入与输出

    2024-06-15 07:20:04       34 阅读