AI - TensorFlow - 示例01:基本分类

基本分类

基本分类(Basic classification):https://www.tensorflow.org/tutorials/keras/basic_classification

Fashion MNIST数据集

tf.keras

是一种用于在TensorFlow中构建和训练模型的高阶API:https://www.tensorflow.org/api_docs/python/tf/keras/

示例

脚本内容

xxx

运行结果

xxx

问题处理

问题1:执行fashion_mnist.load_data()失败

错误提示
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
......
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz: None -- [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

处理方法1

选择一个链接,

手工下载下面四个文件,并存放在“~/.keras/datasets”下的fashion-mnist目录。

  • train-labels-idx1-ubyte.gz
  • train-images-idx3-ubyte.gz
  • t10k-labels-idx1-ubyte.gz
  • t10k-images-idx3-ubyte.gz
guowli@5CG450158J MINGW64 ~/.keras/datasets
$ pwd
/c/Users/guowli/.keras/datasets

guowli@5CG450158J MINGW64 ~/.keras/datasets
$ ls -l
total 0
drwxr-xr-x 1 guowli 1049089 0 Mar 27 14:44 fashion-mnist/

guowli@5CG450158J MINGW64 ~/.keras/datasets
$ ls -l fashion-mnist/
total 30164
-rw-r--r-- 1 guowli 1049089  4422102 Mar 27 15:47 t10k-images-idx3-ubyte.gz
-rw-r--r-- 1 guowli 1049089     5148 Mar 27 15:47 t10k-labels-idx1-ubyte.gz
-rw-r--r-- 1 guowli 1049089 26421880 Mar 27 15:47 train-images-idx3-ubyte.gz
-rw-r--r-- 1 guowli 1049089    29515 Mar 27 15:47 train-labels-idx1-ubyte.gz

处理方法2

手工下载文件,存放在指定目录。
改写“tensorflow\python\keras\datasets\fashion_mnist.py”定义的load_data()函数。

from tensorflow.python.keras.utils import get_file
import numpy as np
import pathlib
import gzip

def load_data():  # 改写“tensorflow\python\keras\datasets\fashion_mnist.py”定义的load_data()函数
    base = "file:///" + str(pathlib.Path.cwd()) + "\\"  # 当前目录

    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(get_file(fname, origin=base + 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)

(train_images, train_labels), (test_images, test_labels) = load_data()

问题2:使用gzip.open()打开.gz文件失败

错误提示

“OSError: Not a gzipped file (b‘\n\n‘)”

处理方法

对于损坏的、不完整的.gz文件,zip.open()将无法打开。检查.gz文件是否完整无损。

参考信息

https://github.com/tensorflow/tensorflow/issues/170

原文地址:https://www.cnblogs.com/anliven/p/10612178.html

时间: 2024-07-30 14:16:07

AI - TensorFlow - 示例01:基本分类的相关文章

AI - TensorFlow - 示例:影评文本分类

影评文本分类 文本分类(Text classification):https://www.tensorflow.org/tutorials/keras/basic_text_classification主要步骤: 1.加载IMDB数据集 2.探索数据:了解数据格式.将整数转换为字词 3.准备数据 4.构建模型:隐藏单元.损失函数和优化器 5.创建验证集 6.训练模型 7.评估模型 8.可视化:创建准确率和损失随时间变化的图 IMDB数据集 https://www.tensorflow.org/a

AI - TensorFlow - 示例03:基本回归

基本回归 回归(Regression):https://www.tensorflow.org/tutorials/keras/basic_regression 主要步骤:数据部分 获取数据(Get the data) 清洗数据(Clean the data) 划分训练集和测试集(Split the data into train and test) 检查数据(Inspect the data) 分离标签(Split features from labels) 规范化数据(Normalize th

AI - TensorFlow - 示例05:保存和恢复模型

保存和恢复模型(Save and restore models) 官网示例:https://www.tensorflow.org/tutorials/keras/save_and_restore_models 在训练期间保存检查点 在训练期间或训练结束时自动保存检查点.权重存储在检查点格式的文件集合中,这些文件仅包含经过训练的权重(采用二进制格式).可以使用经过训练的模型,而无需重新训练该模型,或从上次暂停的地方继续训练,以防训练过程中断 检查点回调用法:创建检查点回调,训练模型并将ModelC

阿里PAI深度学习组件:Tensorflow实现图片智能分类实验

PAI简介 阿里云机器学习PAI(Platform of Artificial Intelligence)是一款一站式的机器学习平台,包含数据预处理.特征工程.常规机器学习算法.深度学习框架.模型的评估以及预测这一整套机器学习相关服务.由于目前PAI还属于公测阶段,所以是不收费的.但是PAI底层依赖于maxcompute(计算)和oss(存储),所以会收取一定的托管费和深度学习存储费用.不过实测发现每天差不多一两分钱,充10块能玩好久. 实验准备 实验的整个过程都在官方文档有很详细的说明:htt

机器学习的大局观:使用神经网络和TensorFlow来对文本分类

https://medium.freecodecamp.com/big-picture-machine-learning-classifying-text-with-neural-networks-and-tensorflow-d94036ac2274 机器学习的开发人员常常说,如果你想学习机器学习,必须先学习算法是怎么样工作的原理,但是我的经验告诉我,不是这样的. 我说,你应该首先能够看到大局:机器学习的应用程序是怎么样工作的.一旦你理解它,你就可以轻松地深入学习和体会到机器学习算法的工作原理

tensorflow实现svm多分类 iris 3分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)

# Multi-class (Nonlinear) SVM Example # # This function wll illustrate how to # implement the gaussian kernel with # multiple classes on the iris dataset. # # Gaussian Kernel: # K(x1, x2) = exp(-gamma * abs(x1 - x2)^2) # # X : (Sepal Length, Petal Wi

第二十二节,TensorFlow中的图片分类模型库slim的使用

Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的"代码瘦身".它类似我们在TensorFlow模块中所介绍的tf.contrib.lyers模块,将很多常见的TensorFlow函数进行了二次封装,使得代码变得更加简洁,特别适用于构建复杂结构的深度神经网络,它可以用了定义.训练.和评估复杂的模型. 这里我们为什么要过来介绍这一节的内容呢?主要是

AI - TensorFlow Tensor

张量(Tensor) 在Tensorflow中,变量统一称作张量(Tensor). 张量(Tensor)是任意维度的数组. 0阶张量:纯量或标量 (scalar), 也就是一个数值,例如,\'Howdy\' 或 5 1阶张量:向量 (vector)或矢量,也就是一维数组(一组有序排列的数),例如,[2, 3, 5, 7, 11] 或 [5] 2阶张量:矩阵 (matrix),也就是二维数组(有序排列的向量),例如,[[3.1, 8.2, 5.9][4.3, -2.7, 6.5]] 3阶张量:三维

TensorFlow教程01 诞生和发展

2015年11月的一天,Google发布了Tensorflow的白皮书并很快将Tensorflow开源.以Google的技术影响力,这个新闻在技术圈很快扩散,大家听着这个陌生的名词兴奋而又没有太多头绪.Tensor到底是什么,Tensorflow什么定位,Google为什么要将它开源... 在技术圈之外,这条消息其实并没有引起非常大的轰动,因为这个世界的消息真的太多了.那一年我还在攻读博士学位,在傍晚的周例会上,草草讲完我的控制课题进展后,我激情洋溢的给实验室的师兄弟介绍了Tensorflow,