【Softmax】操作对象(以模型最后一层生成的特征图进行softmax后得到的输出结果进行验证)

every blog every motto: You can do more than you think.

0. 前言

我们知道对于分类问题,最后一层会经过softmax,关于最后结果我们用数据进行验证。
说明: sigmoid用数据验证传送门

1. 正文

softmax公式:
在这里插入图片描述

import numpy as np
import os, sys

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from tensorflow.keras.layers import *
import tensorflow as tf

1.1 一维

# 一维情况
tensor1 = tf.constant(np.array([1, 0, 0]), dtype=tf.float32)
print('tensor1: ', tensor1)
print('tensor1 shape: ', tensor1.shape)

tensor1_soft = Softmax()(tensor1)
print(tensor1_soft)
print('-' * 50)

结果:
在这里插入图片描述
由上面结果可知,我们对一维内的数据按照上述公式进行求解,我们可以numpy加以验证:

np_soft = np.exp(tensor1) / np.sum(np.exp(tensor1))
print(np_soft)
print('-*'*20)

结果:
在这里插入图片描述
得到的结果和前面进行softmax的结果一致。

1.2 二维

# 二维情况
tensor2 = tf.constant(np.array([[1, 1], [2, 0]]), dtype=tf.float32)
print('tensor2: ', tensor2)
print('tensor2.shape: ', tensor2.shape)
tensor2_soft = Softmax()(tensor2)
print(tensor2_soft)
print('-' * 50)

结果:
在这里插入图片描述

上面的结果,我们可以发现,只对[1,1] ;[2,0] 各自做了softmax,这里暂时不过多解释。
具体过程:
以其中两个数为例:
在这里插入图片描述
在这里插入图片描述

1.3 三维

1.3.1 第一种情况

# 三维情况
tensor3 = tf.constant(np.array([[[1, 1], [1, 0], [0, 0]]]), dtype=tf.float32)
print('tensor3: ', tensor3)
print('tensor3.shape', tensor3.shape)
print('-'*29)
tensor3_soft = Softmax()(tensor3)
print(tensor3_soft)

结果:
在这里插入图片描述

1.3.2 第二种情况

# 三维情况
tensor3 = tf.constant(np.array([[[1], [0]], [[2], [1]], [[0], [0]]]), dtype=tf.float32)
print('tensor3: ', tensor3)
print('tensor3.shape', tensor3.shape)
print('-' * 29)
tensor3_soft = Softmax()(tensor3)
print(tensor3_soft)

结果:
在这里插入图片描述
由上面结果,我们发现所有的值都为1,我们再联系所有tensor的shape,可以发现,这样一个规律:
Softmax对最后一维内的数据进行求解。

1.4 ReLu(附)

在这里插入图片描述

这里我们仅列举一列,以三维为例:
结果(结论)与Softmax相同

# 三维情况
tensor3 = tf.constant(np.array([[[1], [-1]], [[2], [1]], [[0], [0]]]), dtype=tf.float32)
print('tensor3: ', tensor3)
print('tensor3.shape', tensor3.shape)
print('-' * 29)
tensor3_soft = ReLU()(tensor3)
print(tensor3_soft)

结果:
在这里插入图片描述
说明: relu,不受shape的影响,不同于softmax,对于传进的每个值进行计算,类似于sigmoid,sigmoid传送门

1.5 小结

  1. 至此,我们由上面可以发现,得到如下结论:
    激活函数对特征图的最后一维,进行求解。

  2. 这也是为什么有关分类问题,模型最后输出的特征图的shape中的最后一维的维数等于分类数 (这句话可能有点绕)

  3. 我们可以联想之前再语义分割讲解中,模型最后输入shape为(208*208,2),即(43264,2),他的最后一维维数为2,对这两个数进行softmax求取概率。

参考文献

[1] https://blog.csdn.net/weixin_39190382/article/details/106301781
[2] https://zhuanlan.zhihu.com/p/105722023
[3] https://blog.csdn.net/weixin_39190382/article/details/106516370
[4] https://blog.csdn.net/siyue0211/article/details/81017728
[5] https://blog.csdn.net/weixin_39190382/article/details/109381000

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,你需要准备一些1280*1024的片和对应的标签。然后,你需要使用TensorFlow框架来训练一个卷积神经网络(CNN)模型。以下是一个简单的示例: ```python import tensorflow as tf # 定义输入的占位符 x = tf.placeholder(tf.float32, [None, 1280, 1024, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) # num_classes表示类别数目 # 定义卷积层 conv1 = tf.layers.conv2d(inputs=x, filters=32, kernel_size=[3, 3], padding="same", activation=tf.nn.relu) pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu) pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) # 定义全连接层 flatten = tf.reshape(pool2, [-1, 320 * 256 * 64]) fc1 = tf.layers.dense(inputs=flatten, units=256, activation=tf.nn.relu) dropout1 = tf.layers.dropout(inputs=fc1, rate=0.4) fc2 = tf.layers.dense(inputs=dropout1, units=num_classes) # 定义损失函数和优化器 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=fc2)) optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) # 定义评估指标 correct_prediction = tf.equal(tf.argmax(fc2, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) ``` 接下来,你需要定义一个数据读取器,读取你准备好的片和标签。然后,你需要使用上述定义好的模型,对数据进行训练和验证,直到模型收敛并达到你的准确率要求为止。 最后,你可以使用训练好的模型来预测新的1280*1024的片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胡侃有料

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

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

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

打赏作者

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

抵扣说明:

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

余额充值