当前位置:网站首页>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 .
边栏推荐
- Luogu: p2685 [tjoi2012] Bridge
- AcWing 4489. 最长子序列
- Qt调节Win屏幕亮度和声音大小
- RDS数据库的监测页面在哪看?
- SWM32系列教程4-端口映射及串口应用
- [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
- 1164 Good in C
- A day's work list of an ordinary programmer
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- PS screen printing brush 131, many illustrators have followed suit
猜你喜欢

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

Automata and automatic line of non-standard design

MySQL has been stopped in the configuration interface during installation

鸿蒙第四次培训

聊聊支付流程的設計與實現邏輯

Unity notes unityxr simple to use

Research on Swift
![Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)](/img/be/4ef38f711e7319a2cc83db2bee3a07.jpg)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)

Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13

Records of long objects and long judgments in the stream of list
随机推荐
[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
PHP returns 500 errors but no error log - PHP return 500 error but no error log
SQL injection database operation foundation
Draw some simple graphics with MFC
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
[RT thread] NXP rt10xx device driver framework -- pin construction and use
AcWing 3438. 数制转换
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Getting started with deops
[vscode] convert tabs to spaces
Inheritance of ES6 class
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
Kubernetes resource object introduction and common commands (4)
一位普通程序员一天工作清单
MinGW compile boost library
ArrayList分析3 : 删除元素
Leetcode540: a single element in an ordered array
Loop through JSON object list
Y is always discrete and can't understand, how to solve it? Answer: read it several times