深度学习知识点整理(国科大)

程序计算

1. 卷积计算(Valid )

import tensorflow as tf

input_x = tf.constant([
    [[[5, 6, 0, 1, 8, 2],
      [0, 9, 8, 4, 6, 5],
      [2, 6, 5, 3, 8, 4],
      [6, 3, 4, 9, 1, 0],
      [7, 5, 9, 1, 6, 7],
      [2, 5, 9, 2, 3, 7]

      ]]])
filters = tf.constant([
    [[[0, -1, 1], [1, 0, 0], [0, -1, 1]]]
])

input_x=tf.reshape(input_x,(1,6,6,1))
filters=tf.reshape(filters,[3,3,1,1])

res = tf.nn.conv2d(input_x, filters, strides=1, padding='VALID')
print('Valid 无激活函数下的输出',res)
res=tf.squeeze(res)
print('Valid 条件下可视化的输出:',res)
print('Valid 激活函数下可视化输出:',tf.squeeze(tf.nn.relu(res)))

2. 卷积计算(FULL)

因为在tensorflow中,没有full这一项,所以,先添加0,再使用SAME模式计算

input_x = tf.constant([
    [[[0,0,0,0,0,0,0,0],
  [0,5,6,0,1,8,2,0],
  [0,2,5,7,2,3,7,0],
  [0,0,7,2,4,5,6,0],
  [0,5,3,6,9,3,1,0],
  [0,6,5,3,1,4,6,0],
  [0,5,2,4,0,8,7,0],
    [0,0,0,0,0,0,0,0]
]]])
input_x=tf.reshape(input_x,(1,8,8,1))

res = tf.nn.conv2d(input_x, filters, strides=1,padding='SAME')
print('Full(加0)未使用激活之前的输出',res)

print('Full(加0)未使用激活函数之前的可视化输出,',tf.squeeze(res))

out = tf.nn.relu(res)
print('Full 激活的输出',out)
print('Full 激活之后的可视化输出,',tf.squeeze(out))

3. 卷积计算(SAME)

import tensorflow as tf

input_x = tf.constant([
    [[[5, 6, 0, 1, 8, 2],
      [0, 9, 8, 4, 6, 5],
      [2, 6, 5, 3, 8, 4],
      [6, 3, 4, 9, 1, 0],
      [7, 5, 9, 1, 6, 7],
      [2, 5, 9, 2, 3, 7]

      ]]])

filters = tf.constant([
    [[[0, -1, 1], [1, 0, 0], [0, -1, 1]]]
])
input_x=tf.reshape(input_x,(1,6,6,1))
filters=tf.reshape(filters,[3,3,1,1])

res = tf.nn.conv2d(input_x, filters, strides=1, padding='SAME')
print('无激活函数下的输出',res)

print('激活函数下输出',tf.nn.relu(res))

4. 参数说明(SAME)

conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, 
       data_format="NHWC", dilations=[1, 1, 1, 1], name=None):

 input:输入的tensor,被卷积的图像,conv2d要求input必须是四维的。四个维度分别为[batch, in_height, in_width, in_channels],即batch size,输入图像的高和宽以及单张图像的通道数。

 filter:卷积核,也要求是四维,[filter_height, filter_width, in_channels, out_channels]四个维度分别表示卷积核的高、宽,输入图像的通道数和卷积输出通道数。其中in_channels大小需要与 input 的in_channels一致。

strides:步长,即卷积核在与图像做卷积的过程中每次移动的距离,一般定义为[1,stride_h,stride_w,1],stride_h与stride_w分别表示在高的方向和宽的方向的移动的步长,第一个1表示在batch上移动的步长,最后一个1表示在通道维度移动的步长,而目前tensorflow规定:strides[0] = strides[3] = 1,即不允许跳过bacth和通道,前面的动态图中的stride_h与stride_w均为1。

padding:边缘处理方式,值为“SAME” 和 “VALID”,熟悉图像卷积操作的朋友应该都熟悉这两种模式;由于卷积核是有尺寸的,当卷积核移动到边缘时,卷积核中的部分元素没有对应的像素值与之匹配。此时选择“SAME”模式,则在对应的位置补零,继续完成卷积运算,在strides为[1,1,1,1]的情况下,卷积操作前后图像尺寸不变即为“SAME”。
若选择 “VALID”模式,则在边缘处不进行卷积运算,若运算后图像的尺寸会变小。

5. 损失计算(二分类交叉熵和多分类交叉熵)

#注意,在python中计算时,log是默认为ln。

import torch
import torch.nn as nn

criterion = nn.BCELoss()#默认是求均值,数据需要是浮点型数据
pre=torch.tensor([0.1,0.2,0.3,0.4]).float()
tar=torch.tensor([0,0,0,1]).float()
l=criterion(pre,tar)
print('二分类交叉熵损失函数计算(均值)',l)


pre=torch.tensor([0.2,0.8,0.4,0.1,0.9]).float()
tar=torch.tensor([0,1,0,0,1]).float()

pre=torch.tensor([0.1,0.2,0.3,0.4]).float()
tar=torch.tensor([0,0,0,1]).float()
criterion = nn.BCELoss(reduction="sum")#求和
l=criterion(pre,tar)
print('二分类交叉熵损失函数计算(求和)',l)

loss=nn.BCELoss(reduction="none")#reduction="none"得到的是loss向量#对每一个样本求损失
l=loss(pre,tar)
print('每个样本对应的loss',l)
criterion2=nn.CrossEntropyLoss()
import numpy as np
pre1=torch.tensor([np.log(20),np.log(40),np.log(60),np.log(80)]).float()
# soft=nn.Softmax(dim=0)
# pre=soft(pre).float()#bs*label_nums
pre1=pre1.reshape(1,4)
tar=torch.tensor([3])
loss2=criterion2(pre1,tar)
print('多分类交叉熵损失函数pre1条件下',loss2)

pre2=torch.tensor([np.log(10),np.log(30),np.log(50),np.log(90)]).float()
pre2=pre2.reshape(1,4)
tar=torch.tensor([3])
loss2=criterion2(pre2,tar)
print('多分类交叉熵损失函数pre2条件下',loss2)

**以上的github链接为:国科大深度学习知识点整理
(已分类整理)
github中包括了

  1. 国科大深度学习期间整理的经典CV模型(图像分类、识别、文本生成模型)
  2. NLP经典模型(主要集中在阅读理解部分)
  3. 相关求导推导
  4. 国科大考试题

整理的较为粗浅,欢迎大家批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YingJingh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值