当前位置:网站首页>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 .
边栏推荐
- PHP returns 500 errors but no error log - PHP return 500 error but no error log
- Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
- Hongmeng fourth training
- Draw some simple graphics with MFC
- 毕业总结
- Test your trained model
- RDS数据库的监测页面在哪看?
- 聊聊支付流程的設計與實現邏輯
- vs2013已阻止安装程序,需安装IE10
- Mathematical formula (test)
猜你喜欢
How to purchase Google colab members in China
Research on Swift
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
STM32 realizes 74HC595 control
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
QT learning diary 9 - dialog box
TCP拥塞控制详解 | 3. 设计空间
TensorBoard快速入门(Pytorch使用TensorBoard)
Automata and automatic line of non-standard design
[RT thread] NXP rt10xx device driver framework -- pin construction and use
随机推荐
When absolutely positioned, the element is horizontally and vertically centered
Tensorboard quick start (pytoch uses tensorboard)
kubernetes资源对象介绍及常用命令(三)
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Hongmeng fourth training
WEB-UI自动化测试-最全元素定位方法
Servlet specification Part II
How to read the source code [debug and observe the source code]
绝对定位时元素水平垂直居中
Analyse ArrayList 3: suppression d'éléments
Introduction to SolidWorks gear design software tool geartrax
Brief introduction to the core functions of automatic penetration testing tool
First day of rhcsa study
[UE4] brush Arctic pack high quality Arctic terrain pack
vs2013已阻止安装程序,需安装IE10
Play with fancy special effects. This AE super kit is for you
Records of long objects and long judgments in the stream of list
Five problems of database operation in commodity supermarket system
Draw some simple graphics with MFC
Remote office tools sharing | community essay solicitation