finish chapter 12

This commit is contained in:
Joseph Hopfmüller
2022-10-17 16:25:41 +02:00
parent 563f0ff8ec
commit 4d121641d1
6 changed files with 215 additions and 0 deletions

17
11_01_softmax.py Normal file
View File

@@ -0,0 +1,17 @@
# softmax squashes outputs so that the sum of the outputs equals 1 while preserving the order
import torch
import torch.nn as nn
import numpy as np
def softmax(x):
return np.exp(x)/np.sum(np.exp(x), axis=0)
x = np.array([2., 1., .1])
outputs = softmax(x)
print('inputs: ', x)
print('softmax numpy:', outputs)
x = torch.tensor([2., 1., .1])
outputs = torch.softmax(x, dim=0)
print('inputs: ', x)
print('softmax numpy:', outputs)