Skip to content
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

add batch_size #37

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions solo/solo.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,16 @@ def main():
'ignore_batch', False) else scvi_data.n_batches

# training parameters
batch_size = params.get('batch_size', 128)
valid_pct = params.get('valid_pct', 0.1)
learning_rate = params.get('learning_rate', 1e-3)
stopping_params = {'patience': params.get('patience', 10), 'threshold': 0}

# protect against single example batch
while num_cells % batch_size == 1:
batch_size = int(np.round(1.25*batch_size))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be wrong with?:

batch_size = int(np.round(1 + batch_size))

Also wouldn't need to put this in a while loop then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's probably fine. GPUs tend to achieve better efficiency with batches divisible by powers of two, so I tried to come up with a strategy that would tend to maintain that. Either way is fine with me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I figured there might be something like that. This is fine with me. Merge away!

print('Increasing batch_size to %d to avoid single example batch.' % batch_size)

##################################################
# VAE

Expand All @@ -193,13 +199,14 @@ def main():
frequency=2,
metrics_to_monitor=['reconstruction_error'],
use_cuda=args.gpu,
early_stopping_kwargs=stopping_params)
early_stopping_kwargs=stopping_params,
batch_size=batch_size)

full_posterior = utrainer.create_posterior(
utrainer.model,
singlet_scvi_data,
indices=np.arange(len(singlet_scvi_data)))
latent, _, _ = full_posterior.sequential().get_latent()
latent, _, _ = full_posterior.sequential(batch_size).get_latent()
np.save(os.path.join(args.out_dir, 'latent.npy'),
latent.astype('float32'))

Expand All @@ -214,7 +221,8 @@ def main():
frequency=2,
metrics_to_monitor=['reconstruction_error'],
use_cuda=args.gpu,
early_stopping_kwargs=stopping_params)
early_stopping_kwargs=stopping_params,
batch_size=batch_size)
utrainer.history['reconstruction_error_test_set'].append(0)
# initial epoch
utrainer.train(n_epochs=2000, lr=learning_rate)
Expand All @@ -231,7 +239,7 @@ def main():
utrainer.model,
singlet_scvi_data,
indices=np.arange(len(singlet_scvi_data)))
latent, _, _ = full_posterior.sequential().get_latent()
latent, _, _ = full_posterior.sequential(batch_size).get_latent()
np.save(os.path.join(args.out_dir, 'latent.npy'),
latent.astype('float32'))

Expand Down Expand Up @@ -303,7 +311,8 @@ def main():
frequency=2, metrics_to_monitor=['accuracy'],
use_cuda=args.gpu,
sampling_model=vae, sampling_zl=True,
early_stopping_kwargs=stopping_params)
early_stopping_kwargs=stopping_params,
batch_size=batch_size)

# initial
strainer.train(n_epochs=1000, lr=learning_rate)
Expand Down Expand Up @@ -331,7 +340,8 @@ def main():
metrics_to_monitor=['accuracy'],
use_cuda=args.gpu,
sampling_model=vae, sampling_zl=True,
early_stopping_kwargs=stopping_params)
early_stopping_kwargs=stopping_params,
batch_size=batch_size)

# models evaluation mode
vae.eval()
Expand Down