当前位置:网站首页>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 .
边栏推荐
- Loop through JSON object list
- [combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
- TensorBoard快速入门(Pytorch使用TensorBoard)
- [mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
- VM11289 WAService. js:2 Do not have __ e handler in component:
- Select 3 fcpx plug-ins. Come and see if you like them
- Deops入门
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- Enterprise custom form engine solution (XI) -- form rule engine 1
- 聊聊支付流程的设计与实现逻辑
猜你喜欢

互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼

Swm32 series Tutorial 4 port mapping and serial port application

IntelliJ 2021.3 short command line when running applications

PS screen printing brush 131, many illustrators have followed suit

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

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

Hongmeng fourth training

Qt调节Win屏幕亮度和声音大小

Leetcode 108 converts an ordered array into a binary search tree -- recursive method

聊聊支付流程的设计与实现逻辑
随机推荐
Notes on problems -- watching videos on edge will make the screen green
A day's work list of an ordinary programmer
STM32实现74HC595控制
Unity notes unityxr simple to use
ArrayList分析3 : 删除元素
Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
【JokerのZYNQ7020】DDS_ Compiler。
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
The difference between i++ and ++i: tell their differences easily
Research on Swift
聊聊支付流程的设计与实现逻辑
Getting started with deops
Leetcode540: a single element in an ordered array
Kubernetes resource object introduction and common commands (III)
kubernetes资源对象介绍及常用命令(三)
Talk about the design and implementation logic of payment process
国内如何购买Google Colab会员
1164 Good in C
TCP congestion control details | 3 design space