fixing bugs in transform, expert demo processing, main train function, and behavior cloning class. need to get bc class parameters to return nonempty list
This commit is contained in:
20
src/bc/bc.py
20
src/bc/bc.py
@@ -2,7 +2,7 @@ import torch
|
||||
import torch.nn as nn
|
||||
from torch.utils.data import DataLoader, RandomSampler
|
||||
import pickle
|
||||
from torch.utils.tensorboard import SummaryWriter
|
||||
#from torch.utils.tensorboard import SummaryWriter
|
||||
|
||||
from src.policies import DeepSetsPolicy
|
||||
from src.util.transform import SciKitMinMaxScaler
|
||||
@@ -13,7 +13,7 @@ class BehaviorCloningPolicy():
|
||||
Class for (continuous) behavior cloning policy
|
||||
"""
|
||||
|
||||
def __init__(self, config: dict transforms: dict={}):
|
||||
def __init__(self, config: dict, transforms: dict={}):
|
||||
"""
|
||||
Initialize BehaviorCloningPolicy
|
||||
Args:
|
||||
@@ -29,7 +29,7 @@ class BehaviorCloningPolicy():
|
||||
return self._transforms
|
||||
|
||||
@transforms.setter
|
||||
def transforms(self, transforms)
|
||||
def transforms(self, transforms):
|
||||
self._transforms=transforms
|
||||
|
||||
def __call__(self, ob):
|
||||
@@ -93,11 +93,11 @@ def generate_transforms(dataset):
|
||||
dataset (Dataset): dataset of demo observations and actions
|
||||
"""
|
||||
transforms = {
|
||||
'action': SciKitMinMaxScaler()
|
||||
'state': SciKitMinMaxScaler()
|
||||
'relative_state': SciKitMinMaxScaler(reduce_dim=2)
|
||||
'path_x': SciKitMinMaxScaler(reduce_dim=2)
|
||||
'path_y': SciKitMinMaxScaler(reduce_dim=2)
|
||||
'action': SciKitMinMaxScaler(),
|
||||
'state': SciKitMinMaxScaler(),
|
||||
'relative_state': SciKitMinMaxScaler(reduce_dim=2),
|
||||
'path_x': SciKitMinMaxScaler(reduce_dim=2),
|
||||
'path_y': SciKitMinMaxScaler(reduce_dim=2),
|
||||
}
|
||||
for key in transforms.keys():
|
||||
transforms[key].fit(dataset[:][key])
|
||||
@@ -126,6 +126,8 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
||||
|
||||
# generate loss function, optimizer
|
||||
loss_fn = nn.HuberLoss(reduction='sum')
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
optimizer = torch.optim.Adam(policy.parameters(), lr=learning_rate, weight_decay=weight_decay)
|
||||
|
||||
for i in train_epochs:
|
||||
@@ -156,5 +158,5 @@ def train(train_dataset, cv_dataset, policy, filestr, **kwargs):
|
||||
loss = loss_fn(pred_action, batch['action'])
|
||||
cv_loss += loss.item() / len(cv_dataset)
|
||||
print('Epoch: {}, CV Loss: {}'.format(i, cv_loss))
|
||||
|
||||
|
||||
policy.save_model(filestr)
|
||||
|
||||
@@ -43,7 +43,8 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
|
||||
env_state = env.projected_state
|
||||
nni = ~torch.isnan(env_state[:,0])
|
||||
norms = torch.norm(env_state[nni,:2]-states[i,nni,:2], dim=1)
|
||||
max_devs.append(norms.max())
|
||||
if len(norms)>0:
|
||||
max_devs.append(norms.max())
|
||||
|
||||
# propagate environment
|
||||
ob, r, done, info = env.step(env.target_state(svt.simstate[i+1]))
|
||||
|
||||
22
src/main.py
22
src/main.py
@@ -37,7 +37,7 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
||||
# make prefix of output files
|
||||
outdir = opj('output',method,'loc%02i'%(loc))
|
||||
if not os.path.isdir(outdir):
|
||||
os.mkdir(outdir)
|
||||
os.makedirs(outdir)
|
||||
filestr = opj(outdir, basestr(**kwargs))
|
||||
|
||||
# load config
|
||||
@@ -60,7 +60,7 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
||||
|
||||
# make policy, train and test datasets, and send to
|
||||
policy = policy_class(config)
|
||||
train_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[0,1,2])
|
||||
train_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[0])#,1,2])
|
||||
cv_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[3])
|
||||
train_fn(train_dataset, cv_dataset, policy, filestr, **kwargs)
|
||||
|
||||
@@ -71,11 +71,11 @@ def main(method='bc', train=False, test=False, loc=0, config_path=None, **kwargs
|
||||
policy.eval()
|
||||
|
||||
# simulate policy
|
||||
test_track = 4
|
||||
track = 4
|
||||
simulate_policy(policy, loc=loc, track=track, filestr=filestr)
|
||||
|
||||
# run test metrics
|
||||
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[4])
|
||||
test_dataset = InteractionDatasetSingleAgent(loc=loc, tracks=[track])
|
||||
metrics(filestr, test_dataset, policy)
|
||||
|
||||
|
||||
@@ -132,16 +132,14 @@ def parse_args():
|
||||
default='config/networks.json5', type=str)
|
||||
parser.add_argument('--seed', default=0, type=int,
|
||||
help='seed')
|
||||
parser.add_argument()
|
||||
parser.add_argument()
|
||||
args = parser.parse_args()
|
||||
kwargs = {
|
||||
'train'=args.train,
|
||||
'test'=args.test,
|
||||
'method'=args.method,
|
||||
'loc'=args.loc,
|
||||
'config'=args.config,
|
||||
'seed'=args.seed
|
||||
'train':args.train,
|
||||
'test':args.test,
|
||||
'method':args.method,
|
||||
'loc':args.loc,
|
||||
'config_path':args.config,
|
||||
'seed':args.seed
|
||||
}
|
||||
return kwargs
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ def average_velocity(x):
|
||||
"""
|
||||
pass
|
||||
|
||||
def divergence(p, q, type='kl')
|
||||
def divergence(p, q, type='kl'):
|
||||
"""
|
||||
Calculate a divergence between p and q
|
||||
Args:
|
||||
|
||||
@@ -52,27 +52,27 @@ class SciKitTransform(Transform):
|
||||
e.g. with reduce_dim=2, (A, B, C, D, E) will be reshaped to (A*B, C*D*E)
|
||||
"""
|
||||
self.tf = tf
|
||||
self.reduce_dim
|
||||
self.reduce_dim = reduce_dim
|
||||
super(SciKitTransform, self).__init__()
|
||||
|
||||
def fit(self, X):
|
||||
nd = X.ndim
|
||||
if self.reduce_dim:
|
||||
self.nfeatures = X.shape[reduce_dim:].prod()
|
||||
self.nfeatures = int(torch.tensor(X.shape[self.reduce_dim:]).prod())
|
||||
else:
|
||||
assert nd==2, 'Invalid ndim'
|
||||
self.nfeatures = X.shape[1]
|
||||
|
||||
self.tf.fit(X.reshape((-1,selfnfeatures)))
|
||||
self.tf.fit(X.reshape((-1,self.nfeatures)))
|
||||
|
||||
def transform(self, X):
|
||||
shape = X.shape
|
||||
t = torch.tensor(self.tf.transform(X.reshape((-1,selfnfeatures))), dtype=torch.float)
|
||||
t = torch.tensor(self.tf.transform(X.reshape((-1,self.nfeatures))), dtype=torch.float)
|
||||
return t.reshape(shape)
|
||||
|
||||
def inverse_transform(self, X):
|
||||
shape = X.shape
|
||||
it = torch.tensor(self.tf.inverse_transform(X.reshape((-1,selfnfeatures))), dtype=torch.float)
|
||||
it = torch.tensor(self.tf.inverse_transform(X.reshape((-1,self.nfeatures))), dtype=torch.float)
|
||||
return it.reshape(shape)
|
||||
|
||||
class SciKitStandardScaler(SciKitTransform):
|
||||
|
||||
Reference in New Issue
Block a user