话不多说,直接上代码
import numpy
import math
def Softmax(inMatrix):
# Use a breakpoint in the code line below to debug your script.
# print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
m, n = numpy.shape(inMatrix)
OutMatrix = numpy.mat(numpy.zeros((m, n)))
soft_sum = 0
for idx in range(0, n):
OutMatrix[0, idx] = math.exp(inMatrix[0, idx])
soft_sum += OutMatrix[0, idx]
for idx in range(0, n):
OutMatrix[0, idx] = OutMatrix[0, idx] / soft_sum
return OutMatrix
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
a = numpy.array([[1, 2, 3, 2, 1, 4, 3]])
b = Softmax(a)
print(b)