tf.keras 数据读取与展示

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)
2.6.0
sys.version_info(major=3, minor=9, micro=6, releaselevel='final', serial=0)
matplotlib 3.5.2
numpy 1.22.3
pandas 1.4.2
sklearn 1.1.1
tensorflow 2.6.0
keras.api._v2.keras 2.6.0

使用一个比较经典的数据fashion_mnist

fashion_mnist = keras.datasets.fashion_mnist
# 用load_data导入数据
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 将训练数据集分为验证集和训练集
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]

# 打印数据集的格式
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 2us/step
40960/29515 [=========================================] - 0s 2us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 22s 1us/step
26435584/26421880 [==============================] - 22s 1us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0s/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 3s 1us/step
4431872/4422102 [==============================] - 3s 1us/step
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
# 打印一张图片
def show_single_image(img_arr):
    plt.imshow(img_arr, cmap="binary")
    plt.show()
# 打印第一张图片
show_single_image(x_train[0])

在这里插入图片描述

# 打印一批照片
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
    assert len(x_data) == len(y_data)
    assert n_rows * n_cols < len(x_data)
    plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
    for row in range(n_rows):
        for col in range(n_cols):
            index = n_cols * row + col
            plt.subplot(n_rows, n_cols, index+1)
            plt.imshow(x_data[index], cmap="binary",
                      interpolation = 'nearest')
            plt.axis('off') # 把坐标系去掉
            plt.title(class_names[y_data[index]]) # 打印标题
        plt.show()
class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress',
              'Coat', 'Sandal', 'Shirt', 'Sneaker',
              'Bag', 'Ankle boot']
show_imgs(4, 5, x_train, y_train, class_names)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

# tf.keras.model.Sequential()
'''
model = keras.models.Sequential()
# flatten 展平,把28*28的二维矩阵展平成28*28的一维向量
model.add(keras.layers.Flatten(input_shape=[28, 28]))
# Dense 全连接层
model.add(keras.layers.Dense(300, activation="relu"))
# Dense 全连接层, 100和之前的300全连接
model.add(keras.layers.Dense(100, activation="relu"))
# Dense 全连接层
model.add(keras.layers.Dense(10, activation="softmax"))
'''

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(300, activation="relu"),
    keras.layers.Dense(100, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

# relu: y = max(0, x)
# softmax: 将向量编程概率分布 x = [x1, x2, x3]
# y = [e^x1/sum, e^x2/sum, e^x3/sum], sum = e^x1 + e^x2 + e^x3

# reason for sparse: y->index. y ->one_hot->[]
# 如果y是向量,就用categorical_crossentropy,如果不是,就用sparse_categorical_crossentropy
# optimizer 就是模型的求解方法
model.compile(loss="sparse_categorical_crossentropy",
             optimizer = keras.optimizers.SGD(0.001),
             metrics = ["accuracy"])
model.layers
[<keras.layers.core.Flatten at 0x2269afc98b0>,
 <keras.layers.core.Dense at 0x2269f7fc070>,
 <keras.layers.core.Dense at 0x2269f7fc3d0>,
 <keras.layers.core.Dense at 0x2269f7f9f70>]
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 300)               235500    
_________________________________________________________________
dense_4 (Dense)              (None, 100)               30100     
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1010      
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________

:tf2较后来的版本默认batch_size=32,每一批取32个数据,(55000/32=1719)

history = model.fit(x_train, y_train, epochs=10,
               
                    validation_data=(x_valid, y_valid))
Epoch 1/10
1719/1719 [==============================] - 3s 1ms/step - loss: 1.8436 - accuracy: 0.7233 - val_loss: 0.6021 - val_accuracy: 0.7814
Epoch 2/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.5714 - accuracy: 0.7830 - val_loss: 0.5542 - val_accuracy: 0.7974
Epoch 3/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.5027 - accuracy: 0.8141 - val_loss: 0.5051 - val_accuracy: 0.8314
Epoch 4/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4594 - accuracy: 0.8316 - val_loss: 0.4948 - val_accuracy: 0.8258
Epoch 5/10
1719/1719 [==============================] - 4s 2ms/step - loss: 0.4294 - accuracy: 0.8417 - val_loss: 0.4561 - val_accuracy: 0.8432
Epoch 6/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4054 - accuracy: 0.8513 - val_loss: 0.4567 - val_accuracy: 0.8398
Epoch 7/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3896 - accuracy: 0.8559 - val_loss: 0.4642 - val_accuracy: 0.8422
Epoch 8/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3750 - accuracy: 0.8627 - val_loss: 0.4647 - val_accuracy: 0.8432
Epoch 9/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3648 - accuracy: 0.8664 - val_loss: 0.4238 - val_accuracy: 0.8574
Epoch 10/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3537 - accuracy: 0.8695 - val_loss: 0.4348 - val_accuracy: 0.8566
history.history
{'loss': [1.8436256647109985,
  0.5714119076728821,
  0.5026618242263794,
  0.45940732955932617,
  0.4294375479221344,
  0.40538403391838074,
  0.38960757851600647,
  0.3750316798686981,
  0.36476728320121765,
  0.353736937046051],
 'accuracy': [0.7233090996742249,
  0.7829636335372925,
  0.8141272664070129,
  0.831636369228363,
  0.8417090773582458,
  0.8512908816337585,
  0.8559454679489136,
  0.8627272844314575,
  0.866381824016571,
  0.869490921497345],
 'val_loss': [0.602108895778656,
  0.5542056560516357,
  0.5050806403160095,
  0.49478277564048767,
  0.4561069905757904,
  0.4567115306854248,
  0.464207261800766,
  0.46472445130348206,
  0.4237830638885498,
  0.43475541472435],
 'val_accuracy': [0.7814000248908997,
  0.7973999977111816,
  0.8313999772071838,
  0.8258000016212463,
  0.8432000279426575,
  0.8398000001907349,
  0.842199981212616,
  0.8432000279426575,
  0.8574000000953674,
  0.8565999865531921]}
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()

plot_learning_curves(history)

在这里插入图片描述

model.evaluate(x_test, y_test, verbose=0)
[0.47178930044174194, 0.8377000093460083]
boston_housing module: Boston housing price regression dataset. cifar10 module: CIFAR10 small images classification dataset. cifar100 module: CIFAR100 small images classification dataset. fashion_mnist module: Fashion-MNIST dataset. imdb module: IMDB sentiment classification dataset. mnist module: MNIST handwritten digits dataset. reuters module: Reuters topic classification dataset. import tensorflow as tf from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() cifar100 = keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar100.load_data() cifar10 = keras.datasets.cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() imdb = keras.datasets.imdb (x_train, y_train), (x_test, y_test) = imdb.load_data() # word_index is a dictionary mapping words to an integer index word_index = imdb.get_word_index() # We reverse it, mapping integer indices to words reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) # We decode the review; note that our indices were offset by 3 # because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown". decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in x_train[0]]) print(decoded_review) boston_housing = keras.datasets.boston_housing (x_train, y_train), (x_test, y_test) = boston_housing.load_data() reuters= keras.datasets.reuters (x_train, y_train), (x_test, y_test) = reuters.load_data() tf.keras.datasets.reuters.get_word_index( path='reuters_word_index.json' )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值