Tensorflow是用于深度学习模型最著名的库。但是,Tensorflow不是那么好,而且学习曲线很长。为了解决这个问题,Tensorflow的高级Keras API提供了构建块,可以更轻松地创建和训练深度学习模型。此外,Keras模型是通过将可配置的构建块连接在一起而制成的,几乎没有限制,这使得它更加模块化。
分类
要使用高级的Keras API进行简单分类,需要遵循的一些步骤是:
- 导入所需的模块。
- 将数据准备为api提供合适的格式。
- 使用tf.keras(Tensorflow-Keras)apis构建神经网络模型并对其进行编译。
- 使用准备好的数据训练模型,同时尝试解决欠拟合和过度拟合的情况。
- 评估模型。
- 保存和恢复模型便于以后使用。
导入所需的模块
首先需要导入numpy和pandas,因为它们是数据处理和准备所必需的。必须导入Tensorflow api和高级keras api来进行低级操作和模型构建。必须导入Matplotlib,以便对性能和准确性等进行图形分析。
# TensorFlow and tf.kerasimport tensorflow as tffrom tensorflow import kerasimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inline''' %matplotlib inline With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.'''
数据准备
数据准备阶段获取原始数据,使其看起来结构化,消除其中的噪声,并更改适合您所设计的模型的数据格式和形状。数据可以是不同格式的,比如图像数据与文本数据不同,两者都需要不同的处理和预处理。例如,如果我们使用mnist_fashion数据来制作分类器来对服装进行分类,则可以按如下方式进行数据准备:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']import pandas as pdtrain_df = pd.read_csv('data/fashion-mnist_train.csv',sep=',')test_df = pd.read_csv('data/fashion-mnist_test.csv', sep = ',')train_df.head()&#