Merge branch 'main' of https://github.com/sisl/InteractionImitation into main
This commit is contained in:
38
scratch/johannes/normalization.py
Normal file
38
scratch/johannes/normalization.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import torch
|
||||||
|
import torchvision
|
||||||
|
|
||||||
|
from torchvision import transforms
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
|
||||||
|
train_set = torchvision.datasets.FashionMNIST(
|
||||||
|
root='./data'
|
||||||
|
,train=True
|
||||||
|
,download=True
|
||||||
|
,transform=transforms.Compose([
|
||||||
|
transforms.ToTensor()
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
|
loader = DataLoader(train_set, batch_size=len(train_set), num_workers=1)
|
||||||
|
# load whole dataset
|
||||||
|
input_data, out_data = next(iter(loader))
|
||||||
|
out_data = out_data.float()
|
||||||
|
# compute mean and std only over batch dimension
|
||||||
|
m_in, s_in = input_data.mean(dim=0), input_data.std(dim=0)
|
||||||
|
m_out, s_out = out_data.mean(dim=0), out_data.std(dim=0)
|
||||||
|
|
||||||
|
input_tf = transforms.Normalize(m_in, s_in)
|
||||||
|
out_tf = transforms.Normalize(m_out, s_out)
|
||||||
|
|
||||||
|
transformed_input = input_tf(input_data)
|
||||||
|
transformed_output = torch.sigmoid(out_tf(out_data))
|
||||||
|
|
||||||
|
# scale sigmoid output [0, 1] to acceleration interval [a_min, a_max]
|
||||||
|
a_min, a_max = (-4, 2)
|
||||||
|
# compute m and s such that normalization with m and s results in desired scaling
|
||||||
|
s = 1 / (a_max - a_min)
|
||||||
|
m = - s * a_min
|
||||||
|
scaling = transforms.Normalize(m, s)
|
||||||
|
|
||||||
|
# DOES NOT WORK SINCE TORCHVISION NORMALIZE WORKS ONLY ON IMAGES
|
||||||
|
scaled_output = scaling(transformed_output)
|
||||||
@@ -2,13 +2,11 @@ import torch
|
|||||||
from torch.nn import functional
|
from torch.nn import functional
|
||||||
|
|
||||||
def parse_functional(functional_config):
|
def parse_functional(functional_config):
|
||||||
if functional_config is None:
|
if isinstance(functional_config, str):
|
||||||
return None
|
|
||||||
elif isinstance(functional_config, str):
|
|
||||||
if functional_config == 'relu':
|
if functional_config == 'relu':
|
||||||
return functional.relu
|
return functional.relu
|
||||||
elif functional_config == 'sigmoid':
|
elif functional_config == 'sigmoid':
|
||||||
return functional.sigmoid
|
return torch.sigmoid
|
||||||
elif functional_config == 'softmax':
|
elif functional_config == 'softmax':
|
||||||
return functional.softmax
|
return functional.softmax
|
||||||
|
return None
|
||||||
@@ -24,7 +24,11 @@ def test_constructor():
|
|||||||
phi_config["output_dim"] = 2
|
phi_config["output_dim"] = 2
|
||||||
phi_config["final_activation"] = "sigmoid"
|
phi_config["final_activation"] = "sigmoid"
|
||||||
phi = ds.Phi.from_config(phi_config)
|
phi = ds.Phi.from_config(phi_config)
|
||||||
assert phi.final_activation == torch.nn.functional.sigmoid
|
assert phi.final_activation == torch.sigmoid
|
||||||
|
|
||||||
|
phi_config["final_activation"] = "relu"
|
||||||
|
phi = ds.Phi.from_config(phi_config)
|
||||||
|
assert phi.final_activation == torch.nn.functional.relu
|
||||||
|
|
||||||
def test_phi():
|
def test_phi():
|
||||||
input_dim = 5
|
input_dim = 5
|
||||||
|
|||||||
Reference in New Issue
Block a user