当前位置:网站首页>Ml (machine learning) softmax function to realize the classification of simple movie categories
Ml (machine learning) softmax function to realize the classification of simple movie categories
2022-07-03 17:42:00 【Mr.Pan_ Academic mania】
I'll share a wave with you softmax function . The expression is as follows :
y = exp(input_data) / sum(exp(input_data))
A brief explanation , Is to index the input data , Then divide by the sum of the input data after indexation .
The code implementation is as follows :
import numpy as np
import matplotlib.pyplot as plt
import os
import time
def softmax(inx):
exp_input = np.exp(inx)
sum_exp = np.sum(exp_input)
y = exp_input / sum_exp
print(y)
if __name__ == '__main__':
inx = np.array([0.2,3,10])
softmax(inx)
Running results :
Let's change to a larger data input , Find the following problems :
if __name__ == '__main__':
inx = np.array([1000,5000,10000])
softmax(inx)

In order to solve the problem of large input data nan Value problem ( Value overflow ), Introduce a constant C, The expression becomes :
y = exp(input_data)*C / sum(exp(input_data))*C
take C Move in , obtain y = exp(input_data+log(C)) / sum(exp(input_data+log(C)))
Make θ = log(C), be y = exp(input_data+θ) / sum(exp(input_data+θ)).
here , because θ You can choose any value , However, in order to prevent numerical overflow ,θ Value will be selected input_data Maximum of , And because θ The value itself is a large positive number , therefore , We need to θ The value becomes negative , Introduce bias b=-θ
that , The final expression can be written as y = exp(input_data+b) / sum(exp(input_data+b))[ When θ Positive number ].
that , The above code is modified to :
import numpy as np
import matplotlib.pyplot as plt
import os
import time
def softmax(inx):
theta = np.max(inx)
b = -theta# bias
exp_input = np.exp(inx+b)
sum_exp = np.sum(exp_input)
y = exp_input / sum_exp
print(y)
if __name__ == '__main__':
inx = np.array([1000,5000,10000])
softmax(inx)
Running results :
For our convenience, we will call softmax(inx) function , modify print by return, Here's the picture :


We find that this calculation result is very similar to probability , The sum is 1( Because of the accuracy error of the computer itself , So it's not 1). therefore , We can use softmax(inx) Function calculation probability ,softmax(inx) Function can be used in machine learning classification problems .
I use softmax(inx) Function implements a simple binary classification problem . The data set I set is as follows :
def Data_load():
dataset = {
" Action movies ":{
" The fight scenes ":105," Kissing scene ":15},
" Love story ":{
" The fight scenes ":5," Kissing scene ":85}
}
return dataset
def classification(dataset):
DataSet = Data_load()
labels = [k for k in DataSet.keys()] # [' Action movies ',' Love story ']
result_array = softmax(dataset)
result_ls = list(result_array)
# print(result_ls)
Max = max(result_ls)
Max_Index = result_ls.index(Max)
# print(Max_Index)
movie = labels[Max_Index]
print(' The film is :', movie)
if __name__ == '__main__':
# testset = np.array([14,37])# Test set
# testset2 = np.array([78,15])
# classification(testset)
# classification(testset2)
testset_ls = [np.array([78,15]),np.array([14,37]),np.array([57,21]),np.array([31,87])
,np.array([23,65]),np.array([26,73])]
for testset in testset_ls:
classification(testset)
Running results :
In this way, we have completed the simple two classification according to our needs .
Last , Thank you for coming to watch my article , There may be many inadequacies in the text , I also hope to point out and sea culvert .
边栏推荐
- Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
- Leetcode Valentine's Day Special - looking for a single dog
- Leetcode 669 pruning binary search tree -- recursive method and iterative method
- VM11289 WAService. js:2 Do not have __ e handler in component:
- PR second time
- QT学习日记9——对话框
- STM32实现74HC595控制
- Design e-commerce spike
- [combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
- A day's work list of an ordinary programmer
猜你喜欢

Leetcode Valentine's Day Special - looking for a single dog

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you

Research on Swift

聊聊支付流程的设计与实现逻辑

TensorBoard快速入门(Pytorch使用TensorBoard)

聊聊支付流程的设计与实现逻辑

vs2013已阻止安装程序,需安装IE10

Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028

【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use
随机推荐
SWM32系列教程4-端口映射及串口应用
Leetcode540: a single element in an ordered array
Introduction to SolidWorks gear design software tool geartrax
1164 Good in C
How to train mask r-cnn model with your own data
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
TCP拥塞控制详解 | 3. 设计空间
Unity notes unityxr simple to use
QT learning diary 9 - dialog box
Applet setting multi account debugging
Vs2013 has blocked the installer, and ie10 needs to be installed
数学公式(测试)
VM11289 WAService. js:2 Do not have __ e handler in component:
Where is the database account used when running SQL tasks in data warehouse tasks configured
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
[UE4] brush Arctic pack high quality Arctic terrain pack
鸿蒙第三次培训
聊聊支付流程的设计与实现逻辑
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
面试官:值为 nil 为什么不等于 nil ?