抖音上有一个曹健《人工智能实践:Tensorflow笔记》,讲的不错,跟着学一下。

运行代码p24_cifar10_datasets.py时

 1 import tensorflow as tf
 2 from matplotlib import pyplot as plt
 3 import numpy as np
 4 
 5 np.set_printoptions(threshold=np.inf)
 6 
 7 cifar10 = tf.keras.datasets.cifar10
 8 (x_train, y_train), (x_test, y_test) = cifar10.load_data()
 9 
10 # 可视化训练集输入特征的第一个元素
11 plt.imshow(x_train[0])  # 绘制图片
12 plt.show()
13 
14 # 打印出训练集输入特征的第一个元素
15 print("x_train[0]:\n", x_train[0])
16 # 打印出训练集标签的第一个元素
17 print("y_train[0]:\n", y_train[0])
18 
19 # 打印出整个训练集输入特征形状
20 print("x_train.shape:\n", x_train.shape)
21 # 打印出整个训练集标签的形状
22 print("y_train.shape:\n", y_train.shape)
23 # 打印出整个测试集输入特征的形状
24 print("x_test.shape:\n", x_test.shape)
25 # 打印出整个测试集标签的形状
26 print("y_test.shape:\n", y_test.shape)

遇到cifar10无法下载的问题。

经过调试,发现手工复制链接到浏览器可以下载,但是底层代码文件data_utils.py里会报异常。

因此,将data_utils.py代码修改一下

 改为

 1     try:
 2       try:
 3         a=1#urlretrieve(origin, fpath, dl_progress)
 4       except HTTPError as e:
 5         raise Exception(error_msg.format(origin, e.code, e.msg))
 6       except URLError as e:
 7         raise Exception(error_msg.format(origin, e.errno, e.reason))
 8     except (Exception, KeyboardInterrupt) as e:
 9       if os.path.exists(fpath):
10         os.remove(fpath)
11       raise
12     ProgressTracker.progbar = None

然后运行,会提示找不到文件。

这时把文件按照放到指定位置改个名字。

运行一下p24_cifar10_datasets.py,会正常显示图片和数据。此时停止运行。

然后把修改的代码再恢复回来。

这样就行了。

=======================

 这个无法下载的问题和之前的不同。该视频教程里,之前有些下载是要走google的,需要梯子。