当前位置:网站首页>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 .
边栏推荐
- 聊聊支付流程的設計與實現邏輯
- 聊聊支付流程的设计与实现逻辑
- [combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- [combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
- Leetcode13. Roman numeral to integer (three solutions)
- First day of rhcsa study
- [mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
- IntelliJ 2021.3 short command line when running applications
- TCP拥塞控制详解 | 3. 设计空间
猜你喜欢

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

Hongmeng third training
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack

Applet setting multi account debugging

Interviewer: why is the value nil not equal to nil?
![[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela](/img/df/a034032e203e7935dafaf8a71cb6c8.jpg)
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela

Baiwen.com 7 days Internet of things smart home learning experience punch in the next day

PS screen printing brush 131, many illustrators have followed suit

Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
![[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.](/img/a0/4fc0e0741aad2885873e60f2af3387.jpg)
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
随机推荐
【JokerのZYNQ7020】DDS_ Compiler。
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
VM11289 WAService. js:2 Do not have __ e handler in component:
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
Where is the database account used when running SQL tasks in data warehouse tasks configured
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
1164 Good in C
Remote office tools sharing | community essay solicitation
Research on Swift
1147_ Makefile learning_ Target files and dependent files in makefile
Stm32h7 Hal library SPI DMA transmission has been in busy solution
AcWing 3438. 数制转换
c# .net 工具生态
AcWing 3438. Number system conversion
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
The third day of writing C language by Yabo people
Qt调节Win屏幕亮度和声音大小
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
List的stream中Long对象与long判等问题记录
[UE4] brush Arctic pack high quality Arctic terrain pack