-
Notifications
You must be signed in to change notification settings - Fork 394
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
Feature: support dict-like input for to_numpy and to_device #657
Conversation
dict like input is possible for user customized dataset or output function,but will get error in current code such as forward_iter And current to_tensor support dict input, but to_device doesn't support
Actually,I overwrite some function to solve this,
|
Hi, thanks for contributing. Could you please explain what exactly you need to achieve? I couldn't quite understand that. E.g., if you could give a minimal code sample that fails with the current code but works with your fix, it would be very helpful (this code sample could also be a unit test, which this PR would need to provide eventually). The main reason why I'm asking is because as you said, |
If I have different data input format,a list batch need to change the index to get Expected results,this is hard for complex model or input format. Surely,dict input have no conflicts with to_numpy or to_device in train phrase,but in predict phrase
This code,model input will through to_tensor to cuda,but for model output,it only support list-like format,so if someone have multi output(such as my model have 50+ output for text-to-sql task 😂) and want to use dict to make things easier,this feature will really help them and does't has influence for normally using skorch . |
Thanks for explaining your use case. I agree that it shouldn't hurt to add these possibilities. Would you please add unit tests for the new additions? That means, tests with dict as input that would currently fail but work with your added changes. |
Do you mean write a test function like this?
|
@sakuranew exactly, same for |
Sorry,I don't see unittest for to_numpy ,and I don't know how to write a test function |
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.
Thanks for adding the tests. You are right, there are currently no tests for to_numpy
(oops), so no need to write a test for that.
When it comes to your current test, I would like to see one change. At the moment, you do:
x_dict = to_device(x_dict, device=device_from)
I.e. you overwrite the x_dict
variable. Instead, I would like for you to create a new variable and check that. In addition, you should check that the original x_dict
is unchanged.
The reason why it's needed here and not, e.g., when testing tuple
, is that dict
is mutable. But we don't want to mutate the original input. Therefore, the test needs to make sure that the original input is not affected by to_numpy
. You could add a comment that explains this as well.
Apart from that 👍
@BenjaminBossan |
Thanks for making the corrections. At the moment, some tests fail (see here). The reason is this line:
which fails with
So what you could do is check the keys and the values separately. For the values, since they are tensors, you need to call You can also run the tests locally first to make sure they pass (if you don't do that already): # in skorch root directory
# only your new tests:
py.test -k test_check_device_dict_torch_tensor
# all tests
py.test This way, you get immediate feedback and don't have to wait for travis CI. Please ask if you have any questions. |
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.
Thanks @sakuranew this looks good.
@ottonemo is adding tests for to_numpy
in #658 but we can add the test for dicts later.
This release of skorch contains a few minor improvements and some nice additions. As always, we fixed a few bugs and improved the documentation. Our [learning rate scheduler](https://skorch.readthedocs.io/en/latest/callbacks.html#skorch.callbacks.LRScheduler) now optionally logs learning rate changes to the history; moreover, it now allows the user to choose whether an update step should be made after each batch or each epoch. If you always longed for a metric that would just use whatever is defined by your criterion, look no further than [`loss_scoring`](https://skorch.readthedocs.io/en/latest/scoring.html#skorch.scoring.loss_scoring). Also, skorch now allows you to easily change the kind of nonlinearity to apply to the module's output when `predict` and `predict_proba` are called, by passing the `predict_nonlinearity` argument. Besides these changes, we improved the customization potential of skorch. First of all, the `criterion` is now set to `train` or `valid`, depending on the phase -- this is useful if the criterion should act differently during training and validation. Next we made it easier to add custom modules, optimizers, and criteria to your neural net; this should facilitate implementing architectures like GANs. Consult the [docs](https://skorch.readthedocs.io/en/latest/user/neuralnet.html#subclassing-neuralnet) for more on this. Conveniently, [`net.save_params`](https://skorch.readthedocs.io/en/latest/net.html#skorch.net.NeuralNet.save_params) can now persist arbitrary attributes, including those custom modules. As always, these improvements wouldn't have been possible without the community. Please keep asking questions, raising issues, and proposing new features. We are especially grateful to those community members, old and new, who contributed via PRs: ``` Aaron Berk guybuk kqf Michał Słapek Scott Sievert Yann Dubois Zhao Meng ``` Here is the full list of all changes: ### Added - Added the `event_name` argument for `LRScheduler` for optional recording of LR changes inside `net.history`. NOTE: Supported only in Pytorch>=1.4 - Make it easier to add custom modules or optimizers to a neural net class by automatically registering them where necessary and by making them available to set_params - Added the `step_every` argument for `LRScheduler` to set whether the scheduler step should be taken on every epoch or on every batch. - Added the `scoring` module with `loss_scoring` function, which computes the net's loss (using `get_loss`) on provided input data. - Added a parameter `predict_nonlinearity` to `NeuralNet` which allows users to control the nonlinearity to be applied to the module output when calling `predict` and `predict_proba` (#637, #661) - Added the possibility to save the criterion with `save_params` and with checkpoint callbacks - Added the possibility to save custom modules with `save_params` and with checkpoint callbacks ### Changed - Removed support for schedulers with a `batch_step()` method in `LRScheduler`. - Raise `FutureWarning` in `CVSplit` when `random_state` is not used. Will raise an exception in a future (#620) - The behavior of method `net.get_params` changed to make it more consistent with sklearn: it will no longer return "learned" attributes like `module_`; therefore, functions like `sklearn.base.clone`, when called with a fitted net, will no longer return a fitted net but instead an uninitialized net; if you want a copy of a fitted net, use `copy.deepcopy` instead;`net.get_params` is used under the hood by many sklearn functions and classes, such as `GridSearchCV`, whose behavior may thus be affected by the change. (#521, #527) - Raise `FutureWarning` when using `CyclicLR` scheduler, because the default behavior has changed from taking a step every batch to taking a step every epoch. (#626) - Set train/validation on criterion if it's a PyTorch module (#621) - Don't pass `y=None` to `NeuralNet.train_split` to enable the direct use of split functions without positional `y` in their signatures. This is useful when working with unsupervised data (#605). - `to_numpy` is now able to unpack dicts and lists/tuples (#657, #658) - When using `CrossEntropyLoss`, softmax is now automatically applied to the output when calling `predict` or `predict_proba` ### Fixed - Fixed a bug where `CyclicLR` scheduler would update during both training and validation rather than just during training. - Fixed a bug introduced by moving the `optimizer.zero_grad()` call outside of the train step function, making it incompatible with LBFGS and other optimizers that call the train step several times per batch (#636) - Fixed pickling of the `ProgressBar` callback (#656)
dict like input is possible for user customized dataset or output function,but will get error in current code such as forward_iter
And current to_tensor support dict input, but to_device doesn't support