[STUDY VLOG] 勉強風景 #11

  • Tensorflow
    • LeNet模型构建
      • Model类、Sequential类 (参数layers,继承自Model)
      • 第一层卷积类:
        • filter #卷积核个数
        • kernel_size #卷积核大小
        • strides =(1,1) #步长
        • padding = ‘valid’ #其他可选项:same,前者舍弃后者为补齐
      • 第二层池化层:
        • pool_size #必须设置
        • strides = (1,1)
        • padding = ‘valid’ #其他可选项:same,前者舍弃后者为补齐
        • date_format = None
        • AveragePooling2D(pool_size = (2,2),strides=(2,2),padding=‘same’)
      • 第三层第四层(与第一第二层类似)
      • 扁平化类:tf.keras.layers.Flatten()
      • Dense全连接类
        • Dense(units=120, activation=tf.nn.relu),
        • Dense(units=84, activation=tf.nn.relu),
        • Dense(units=10, activation=tf.nn.softmax)
    • LeNet模型训练和预测
      • 超参数的设置:epoch:训练轮数;batch_size:批大小;learning_rate:学习率
      • 优化器:adam_optimizer = tf.optimizers.Adam(learning_rate)
      • 损失函数设置:optimizers:优化器;loss:损失函数;metrics:评价
      • 训练方法fit:x:训练数据;y:训练数据的标签;batch_size:批大小;epoch:训练遍数(轮数)
      • 保存模型
      • 模型预测:predict
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(filters=6, kernel_size = (5,5),padding = 'valid', activation=tf.nn.relu, input_shape=(32,32,1)),
  tf.keras.layers.AveragePooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
  tf.keras.layers.Conv2D(filters=16, kernel_size=(5,5), padding='valid', activation=tf.nn.relu),
  tf.keras.layers.AveragePooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(units=120, activation=tf.nn.relu),
  tf.keras.layers.Dense(units=84, activation=tf.nn.relu),
  tf.keras.layers.Dense(units=10, activation=tf.nn.softmax)
])
#打完一边会发现,自己的打字水平简直小学生
#.打成,
#units打成unites
#layers打成layer
#kernel_size打成kerlne_size
model.summary()
#运行结果如下:
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 28, 28, 6)         156       
_________________________________________________________________
average_pooling2d_5 (Average (None, 14, 14, 6)         0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 10, 10, 16)        2416      
_________________________________________________________________
average_pooling2d_6 (Average (None, 5, 5, 16)          0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 120)               48120     
_________________________________________________________________
dense_3 (Dense)              (None, 84)                10164     
_________________________________________________________________
dense_4 (Dense)              (None, 10)                850       
=================================================================
Total params: 61,706
Trainable params: 61,706
Non-trainable params: 0
_________________________________________________________________
import numpy as np
#超参数设置
num_epochs = 10
batch_size = 64
learning_rate = 0.001

#优化器
adam_optimizer = tf.keras.optimizers.Adam(learning_rate)

model.compile(
    optimizer = adam_optimizer,
    loss = tf.keras.losses.sparse_categorical_crossentropy,
    metrics=['accuracy']
)
import datetime
start_time = datetime.datetime.now()

model.fit(
    x=x_train,
    y=y_train,
    batch_size=batch_size,
    epochs=num_epochs
)
end_time = datetime.datetime.now()
time_cost = end_time - start_time
print ("time_cost = ", time_cost) #用GPU会快很多
#模型保存云端
from google.colab import drive
drive.mount('/gdrive')
model.save('/gdrive/My Drive/...')
  • 操作系统
    • 进程互斥的软件解决方案
      • free:临界区空闲标志:true有进程在临界区false则无
      • turn:谁进临界区的标志:true P进程进临界区false则Q
      • pturn qturn初值都为false:
        • P进临界区:pturn and not qturn
        • Q进临界区:not pturn and qturn
      • DEKKER算法,在方法三在引入turn变量
      • PETERSON算法:解决互斥访问的问题,客服强制轮流法的缺点
    • 进程互斥的硬件解决方案
      • 中断屏蔽方法:“开关中断”指令
        • 适用于操作系统本身、不适合用户进程
        • 代价高
        • 不适用于多处理器
        • 简单高效
      • “测试”并加锁指令:TSL指令:test and set lock
      • “交换”指令:XCHG指令:exchange
      • 自旋锁spin lock(多处理器)
发布了49 篇原创文章 · 获赞 5 · 访问量 3621
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览