1、加载已有数据集以及数据集处理:
(train_x,train_y), (test_x, train_y) = tf.keras.datasets.mnist.load_data()2、使用os库实现与path相关的操作
os.path.join()#连接两个路径 os.path.dirname()#获取路径名 os.listdir() #获取文件夹下的文件名的列表 tf.keras.utils.get_file()#从URL链接下载文件,extract是是否解压3、其他操作
改变形状:adarray.reshape()加载的数据集为numpy .adarray类型; 使用以下代码查看图片:
import matploylib.pyplot as plt for i in range(9): plt.subplot(3, 3, i+1) #画子图 plt.imshow(train_x[i,:,:]) #显示 plt.axis=False #去掉坐标轴 def showPic(x_train, y_train): classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] num_class = len(classes) sample_per_class = 7 for y, cls in enumerate(classes): index = np.flatnonzero(y_train == y) #此函数是获取y_train中与y相同的值的位置 index = np.random.choice(index, sample_per_class, replace=False) #从列表中任意选择sample_per_class个值,并且不能重复 for i, idx in enumerate(index): plt_idx = i*num_class + y + 1 #每一个类是竖着的,因此是1、11、21、31、41... plt.subplot(sample_per_class, num_class, plt_idx) plt.imshow(x_train[idx]) plt.axis('off')#去掉坐标轴 if i==0: plt.title(cls) plt.show() if __name__ == '__main__': (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data() showPic(x_train, y_train)1、使用keras的compile、fit等训练
使用keras创建网络: 获取数据集,需要转换为tf.data.Dataset类型,或者Numpy Array类型的数据; 使用tf.keras.layers.Sequential([])来创建网络; 使用network.build()输入input_shape来使得网络知道输入的数据长度,以便创建参量; 使用network.compile()来设置网络的优化方法、损失函数的计算方法、评价指标等; 使用network.fit()来对网络进行训练,输入数据集以及训练的epoch、验证集及其epoch。敲黑板:可以使用callback来设置以下内容:
每批量训练结束后写入TensorBoard日志, 用来监视你的度量(指标) 定期将模型保存到磁盘 提前终止训练 训练期间查看模型内部状态和统计信息 等等
网络来源: tf.nn.层函数 或者 tf.keras.layers.层类; 其中 层类都继承自 tf.keras.layers.Layer类, Sequential来自Keras,其继承自 keras.Model。2、使用keras的ImageDataGenerator产生数据集等训练 ImageDataGenerator的使用总结_个人学习总结
数据集转化: 创建ImageDataGenerator类实例; 使用flow、flow_from_dataframe、flow_from_directory导入数据; 创建callback设置保存模型参数;创建模型Sequential;模型规则设定network.compile();训练网络 network.fit_generator()模型评估 network.evaluate_generator()tensorflow函数
tf.reshape() tf.cast() tf.matmul()#其中transpose_a转置等参数 tf.where(condition,x,y)#若没有x,y那么返回的是condition中TRUE的索引列表,如果有x,y那么condition中为TRUE则选对应的x元素,否则是y对应元素,若大小不对应,则会broadcast
tf.tile(input, multiples)#用于input的复制,multiples是表示dims的列表,表示沿着那个轴复制 ****************************************************************** a = tf.constant([[1,2,3,4,5]]) b = tf.constant([2,3]) c = tf.tile(a, b) output: <tf.Tensor: id=8, shape=(2, 15), dtype=int32, numpy= array([[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]])> tf.gather( params, indices, validate_indices=None, axis=None, batch_dims=0, name=None )#功能:根据提供的indices在axis这个轴上对params进行索引,拼接成一个新的张量,默认沿着axis=0 ************************************************************************ a = tf.constant(range(0,20)) a = tf.reshape(a, [4,5]) *output: <tf.Tensor: id=12, shape=(4, 5), dtype=int32, numpy= array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])> b = tf.gather(a, [1,3], axis=1) *output: <tf.Tensor: id=15, shape=(4, 2), dtype=int32, numpy= array([[ 1, 3], [ 6, 8], [11, 13], [16, 18]])>