zoukankan      html  css  js  c++  java
  • 深度学习-Tensorflow2.2-tf.data输入模块{2}-tf.data输入实例-10

    # -*- coding: utf-8 -*-
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 修改警告级别,不显示警告
    import tensorflow as tf
    
    
    # 下载数据集并划分为训练集和测试集
    (train_images,train_lables),(test_images,test_labels) = tf.keras.datasets.mnist.load_data()
    # 归一化
    train_images = train_images/255
    test_images = test_images/255
    print(train_images.shape)
    # 建立模型创建dataset
    ds_train_img = tf.data.Dataset.from_tensor_slices(train_images)
    print(ds_train_img)
    
    

    在这里插入图片描述

    # -*- coding: utf-8 -*-
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 修改警告级别,不显示警告
    import tensorflow as tf
    
    
    # 下载数据集并划分为训练集和测试集
    (train_images,train_lables),(test_images,test_labels) = tf.keras.datasets.mnist.load_data()
    # 归一化
    train_images = train_images/255
    test_images = test_images/255
    print(train_images.shape)
    # 创建dataset
    ds_train_img = tf.data.Dataset.from_tensor_slices(train_images)
    print(ds_train_img)
    ds_train_lab = tf.data.Dataset.from_tensor_slices(train_lables)
    print(ds_train_lab)
    # 合并(元组)
    ds_train = tf.data.Dataset.zip((ds_train_img,ds_train_lab))
    print(ds_train)
    # 取出其中10000个组件进行乱序,无限重复每次输出64张图片
    ds_train = ds_train.shuffle(10000).repeat().batch(64)
    
    # 建立模型
    model = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28,28)),
        tf.keras.layers.Dense(128,activation="relu"),
        tf.keras.layers.Dense(10,activation="softmax")
    ])
    # 编译模型
    model.compile(optimizer="adam",
                  loss="sparse_categorical_crossentropy",
                  metrics=["accuracy"])
    steps_per_epochs = train_images.shape[0]//64 # 我们上面是无限循环迭代,定义每一个epochs训练多少步
    # 训练模型 一共训练5次每次训练 train_images.shape[0]//64个组件
    model.fit(ds_train,epochs=5,steps_per_epoch=steps_per_epochs)
    

    在这里插入图片描述

    # -*- coding: utf-8 -*-
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 修改警告级别,不显示警告
    import tensorflow as tf
    
    
    # 下载数据集并划分为训练集和测试集
    (train_images,train_lables),(test_images,test_labels) = tf.keras.datasets.mnist.load_data()
    # 归一化
    train_images = train_images/255
    test_images = test_images/255
    # print(train_images.shape)
    # 创建dataset
    ds_train_img = tf.data.Dataset.from_tensor_slices(train_images)
    # print(ds_train_img)
    ds_train_lab = tf.data.Dataset.from_tensor_slices(train_lables)
    # print(ds_train_lab)
    
    # 合并(元组形式放入)
    ds_train = tf.data.Dataset.zip((ds_train_img,ds_train_lab))
    
    # print(ds_train)
    # 取出其中10000个组件进行乱序,无限重复每次输出64张图片
    ds_train = ds_train.shuffle(10000).repeat().batch(64)
    ds_test = tf.data.Dataset.from_tensor_slices((test_images,test_labels))# 创建test数据集放入一个元组
    ds_test = ds_test.batch(64)# 每次输出64个组件
    # 建立模型
    model = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28,28)),
        tf.keras.layers.Dense(128,activation="relu"),
        tf.keras.layers.Dense(10,activation="softmax")
    ])
    # 编译模型
    model.compile(optimizer="adam",
                  loss="sparse_categorical_crossentropy",
                  metrics=["accuracy"])
    steps_per_epochs = train_images.shape[0]//64 # 我们上面是无限循环迭代,定义每一个epochs训练多少步
    # 训练模型 一共训练5次每次训练 train_images.shape[0]//64个组件/测试数据ds_test 每次测试10000整除64个组件
    model.fit(ds_train,
              epochs=5,
              steps_per_epoch=steps_per_epochs,
              validation_data=ds_test,
              validation_steps=10000//64)
    

    在这里插入图片描述

  • 相关阅读:
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 菜单与按钮
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    高考后的BB
  • 原文地址:https://www.cnblogs.com/gemoumou/p/14186272.html
Copyright © 2011-2022 走看看