-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gpu batch #35
base: master
Are you sure you want to change the base?
Gpu batch #35
Changes from all commits
e2d398a
c87ece7
089e0c5
a9cdbd6
bd45a8f
37ab640
e9dbd3e
1c9850a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,9 +74,9 @@ def pretrain_dsm(model, t_train, e_train, t_valid, e_valid, | |
valid_loss = 0 | ||
for r in range(model.risks): | ||
valid_loss += unconditional_loss(premodel, t_valid, e_valid, str(r+1)) | ||
valid_loss = valid_loss.detach().cpu().numpy() | ||
valid_loss = valid_loss.item() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets use float |
||
costs.append(valid_loss) | ||
#print(valid_loss) | ||
|
||
if np.abs(costs[-1] - oldcost) < thres: | ||
patience += 1 | ||
if patience == 3: | ||
|
@@ -112,7 +112,7 @@ def train_dsm(model, | |
x_train, t_train, e_train, | ||
x_valid, t_valid, e_valid, | ||
n_iter=10000, lr=1e-3, elbo=True, | ||
bs=100): | ||
bs=100, cuda=False): | ||
"""Function to train the torch instance of the model.""" | ||
|
||
logging.info('Pretraining the Underlying Distributions...') | ||
|
@@ -132,6 +132,10 @@ def train_dsm(model, | |
lr=1e-2, | ||
thres=1e-4) | ||
|
||
if cuda: | ||
x_valid, t_valid_, e_valid_ = x_valid.cuda(),\ | ||
t_valid_.cuda(), e_valid_.cuda() | ||
|
||
for r in range(model.risks): | ||
model.shape[str(r+1)].data.fill_(float(premodel.shape[str(r+1)])) | ||
model.scale[str(r+1)].data.fill_(float(premodel.scale[str(r+1)])) | ||
|
@@ -154,6 +158,10 @@ def train_dsm(model, | |
tb = t_train[j*bs:(j+1)*bs] | ||
eb = e_train[j*bs:(j+1)*bs] | ||
|
||
|
||
if cuda: | ||
xb, tb, eb = xb.cuda(), tb.cuda(), eb.cuda() | ||
|
||
if xb.shape[0] == 0: | ||
continue | ||
|
||
|
@@ -166,7 +174,6 @@ def train_dsm(model, | |
_reshape_tensor_with_nans(eb), | ||
elbo=elbo, | ||
risk=str(r+1)) | ||
#print ("Train Loss:", float(loss)) | ||
loss.backward() | ||
optimizer.step() | ||
|
||
|
@@ -179,8 +186,7 @@ def train_dsm(model, | |
elbo=False, | ||
risk=str(r+1)) | ||
|
||
valid_loss = valid_loss.detach().cpu().numpy() | ||
costs.append(float(valid_loss)) | ||
costs.append(valid_loss.item()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets use float() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .item() automatically puts on cpu if necessary and cast it |
||
dics.append(deepcopy(model.state_dict())) | ||
|
||
if costs[-1] >= oldcost: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think this needs to be detached and put to CPU. torch doesnt track this variable.