Machine Learning
-tensorflow
–环境
编辑器上安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade tensorflow-gpu
国内镜像
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
华为:https://repo.huaweicloud.com/repository/pypi/simple
使用免费线上资源,免费CPU,GPU
Colab
Kaggle
kaggle使用时的小细节
文件压缩包的下载和解压
!wget --no-check-certificate \
http://storage.googleapis.com/laurencemoroney-blog.appspot.com/validation-horse-or-human.zip \
-O /tmp/validation-horse-or-human.zip
!wget --no-check-certificate \
http://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip \
-O /tmp/horse-or-human.zip
import zipfile
train = zipfile.ZipFile("/tmp/horse-or-human.zip")
train.extractall(path="/tmp/horse-or-human")
validation = zipfile.ZipFile("/tmp/validation-horse-or-human.zip")
validation.extractall(path="/tmp/validation-horse-or-human")
kaggle的详细目录结果,_注意_并非只有working和input目录,并且kaggle也不是根目录,只是一个子目录
–基础程序结构
下列小总结大部分来源于------Google官方TensorFlow系列视频
import tensorflow as tf
##载入数据
data_x = []
data_y = []
##模型建立
model = tf.keras.Sequential()
model.add()
##优化
model.compile(loss= '', optimizer='')
##训练
model.fit(data_x, data_y, epochs = )
–构建神经元模型
#构造神经元模型
model = keras.Sequential([
keras.layers.Flatten(input_shape= (28,28)),#输入层
keras.layers.Dense(128,activation=tf.nn.relu),#中间层,128个神经元
keras.layers.Dense(10, activation = tf.nn.softmax)#输出层,10个神经元
])
模型示意图
relu函数和softmax函数
过拟合(overfitting)
由于训练次数过多,导致训练的loss出现如下变化
因此可使用callbacks进行处理,当满足需要的精度后,就跳出训练
在拟合某些非线性模型(此处主要指二次曲线,三次曲线等等)
目前可以得出,通过调整神经元的数量units,激活函数activation,以及层数layers,最主要是训练素材的数量,
以下便是一个训练的草图
–CNN(convolutional neural network)
convolution and pooling
convolution
过滤器中的数值与像素对应位置相乘
2,3图分别为着重提取纵向与横向特征
其过滤器如下
# filter = [[-1, -2, -1],[0, 0, 0],[1, 2, 1]]
# filter = [[-1, 0 ,1],[-2, 0, 2],[-1, 0, 1]]
下面便是分别是原图,提取纵向,提取横向,池化的结果
pooling
Max pooling 增强特征,减少数据
–图像增强
此操作可以避免由于数据量不够,而引起的过拟合
# 此操作可以避免由于数据量不够,而引起的过拟合
# train_datagen = ImageDataGenerator(
# rescale = 1./255, 统一图像尺寸
# rotation_range=40, 随机旋转每个图像,图像角度 -40度~40度
# width_shift_range=0.2, width与height让图像在画框中,安装设定的范围平移
# height_shift_range=0.2, 此处,图像对多平移宽度或高度的20%
# shear_range=0.2, 值为0~1,最多可以剪切图像的20%
# zoom_range=0.2, 值为0~1,提供0~20%的范围随机放大
# horizontal_flip = True, 图像将被随机翻转
# fill_mode = 'nearest' 补全图像在操作中的丢失部分,比如倾斜后超出原图的像素
# )
未完,待续、、、
均为个人见解,勿喷、、、