/
keras_model.py
1299 lines (1169 loc) · 55.3 KB
/
keras_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import tensorflow as tf
import time
import logging
import os
from collections.abc import Sequence as SequenceCollection
from deepchem.data import Dataset, NumpyDataset
from deepchem.metrics import Metric
from deepchem.models.losses import Loss
from deepchem.models.models import Model
from deepchem.models.optimizers import Adam, Optimizer, LearningRateSchedule
from deepchem.trans import Transformer, undo_transforms
from deepchem.utils.evaluate import GeneratorEvaluator
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
from deepchem.utils.typing import LossFn, OneOrMany
from deepchem.models.wandblogger import WandbLogger
try:
import wandb
wandb.ensure_configured()
if wandb.api.api_key is None:
_has_wandb = False
wandb.termwarn(
"W&B installed but not logged in. Run `wandb login` or set the WANDB_API_KEY env variable."
)
else:
_has_wandb = True
except (ImportError, AttributeError):
_has_wandb = False
logger = logging.getLogger(__name__)
class KerasModel(Model):
"""This is a DeepChem model implemented by a Keras model.
This class provides several advantages over using the Keras
model's fitting and prediction methods directly.
1. It provides better integration with the rest of DeepChem,
such as direct support for Datasets and Transformers.
2. It defines the loss in a more flexible way. In particular,
Keras does not support multidimensional weight matrices,
which makes it impossible to implement most multitask
models with Keras.
3. It provides various additional features not found in the
Keras model class, such as uncertainty prediction and
saliency mapping.
Here is a simple example of code that uses KerasModel to train
a Keras model on a DeepChem dataset.
>> keras_model = tf.keras.Sequential([
>> tf.keras.layers.Dense(1000, activation='tanh'),
>> tf.keras.layers.Dense(1)
>> ])
>> model = KerasModel(keras_model, loss=dc.models.losses.L2Loss())
>> model.fit(dataset)
The loss function for a model can be defined in two different
ways. For models that have only a single output and use a
standard loss function, you can simply provide a
dc.models.losses.Loss object. This defines the loss for each
sample or sample/task pair. The result is automatically
multiplied by the weights and averaged over the batch. Any
additional losses computed by model layers, such as weight
decay penalties, are also added.
For more complicated cases, you can instead provide a function
that directly computes the total loss. It must be of the form
f(outputs, labels, weights), taking the list of outputs from
the model, the expected values, and any weight matrices. It
should return a scalar equal to the value of the loss function
for the batch. No additional processing is done to the
result; it is up to you to do any weighting, averaging, adding
of penalty terms, etc.
You can optionally provide an output_types argument, which
describes how to interpret the model's outputs. This should
be a list of strings, one for each output. You can use an
arbitrary output_type for a output, but some output_types are
special and will undergo extra processing:
- 'prediction': This is a normal output, and will be returned by predict().
If output types are not specified, all outputs are assumed
to be of this type.
- 'loss': This output will be used in place of the normal
outputs for computing the loss function. For example,
models that output probability distributions usually do it
by computing unbounded numbers (the logits), then passing
them through a softmax function to turn them into
probabilities. When computing the cross entropy, it is more
numerically stable to use the logits directly rather than
the probabilities. You can do this by having the model
produce both probabilities and logits as outputs, then
specifying output_types=['prediction', 'loss']. When
predict() is called, only the first output (the
probabilities) will be returned. But during training, it is
the second output (the logits) that will be passed to the
loss function.
- 'variance': This output is used for estimating the
uncertainty in another output. To create a model that can
estimate uncertainty, there must be the same number of
'prediction' and 'variance' outputs. Each variance output
must have the same shape as the corresponding prediction
output, and each element is an estimate of the variance in
the corresponding prediction. Also be aware that if a model
supports uncertainty, it MUST use dropout on every layer,
and dropout most be enabled during uncertainty prediction.
Otherwise, the uncertainties it computes will be inaccurate.
- other: Arbitrary output_types can be used to extract outputs
produced by the model, but will have no additional
processing performed.
"""
def __init__(self,
model: tf.keras.Model,
loss: Union[Loss, LossFn],
output_types: Optional[List[str]] = None,
batch_size: int = 100,
model_dir: Optional[str] = None,
learning_rate: Union[float, LearningRateSchedule] = 0.001,
optimizer: Optional[Optimizer] = None,
tensorboard: bool = False,
wandb: bool = False,
log_frequency: int = 100,
wandb_logger: Optional[WandbLogger] = None,
**kwargs) -> None:
"""Create a new KerasModel.
Parameters
----------
model: tf.keras.Model
the Keras model implementing the calculation
loss: dc.models.losses.Loss or function
a Loss or function defining how to compute the training loss for each
batch, as described above
output_types: list of strings
the type of each output from the model, as described above
batch_size: int
default batch size for training and evaluating
model_dir: str
the directory on disk where the model will be stored. If this is None,
a temporary directory is created.
learning_rate: float or LearningRateSchedule
the learning rate to use for fitting. If optimizer is specified, this is
ignored.
optimizer: Optimizer
the optimizer to use for fitting. If this is specified, learning_rate is
ignored.
tensorboard: bool
whether to log progress to TensorBoard during training
wandb: bool
whether to log progress to Weights & Biases during training (deprecated)
log_frequency: int
The frequency at which to log data. Data is logged using
`logging` by default. If `tensorboard` is set, data is also
logged to TensorBoard. If `wandb` is set, data is also logged
to Weights & Biases. Logging happens at global steps. Roughly,
a global step corresponds to one batch of training. If you'd
like a printout every 10 batch steps, you'd set
`log_frequency=10` for example.
wandb_logger: WandbLogger
the Weights & Biases logger object used to log data and metrics
"""
super(KerasModel, self).__init__(model=model,
model_dir=model_dir,
**kwargs)
self.loss = loss # not used
self.learning_rate = learning_rate # not used
self.output_types = output_types # not used
if isinstance(loss, Loss):
self._loss_fn: LossFn = _StandardLoss(model, loss)
else:
self._loss_fn = loss
self.batch_size = batch_size
if optimizer is None:
self.optimizer: Optimizer = Adam(learning_rate=learning_rate)
else:
self.optimizer = optimizer
self.tensorboard = tensorboard
# W&B flag support (DEPRECATED)
if wandb:
logger.warning(
"`wandb` argument is deprecated. Please use `wandb_logger` instead. "
"This argument will be removed in a future release of DeepChem."
)
if wandb and not _has_wandb:
logger.warning(
"You set wandb to True but W&B is not installed. To use wandb logging, "
"run `pip install wandb; wandb login`")
self.wandb = wandb and _has_wandb
self.wandb_logger = wandb_logger
# If `wandb=True` and no logger is provided, initialize default logger
if self.wandb and (self.wandb_logger is None):
self.wandb_logger = WandbLogger()
# Setup and initialize W&B logging
if (self.wandb_logger
is not None) and (not self.wandb_logger.initialized):
self.wandb_logger.setup()
# Update config with KerasModel params
wandb_logger_config = dict(loss=loss,
output_types=output_types,
batch_size=batch_size,
model_dir=model_dir,
learning_rate=learning_rate,
optimizer=optimizer,
tensorboard=tensorboard,
log_frequency=log_frequency)
wandb_logger_config.update(**kwargs)
if self.wandb_logger is not None:
self.wandb_logger.update_config(wandb_logger_config)
# Backwards compatibility
if "tensorboard_log_frequency" in kwargs:
logger.warning(
"tensorboard_log_frequency is deprecated. Please use log_frequency instead. This argument will be removed in a future release of DeepChem."
)
self.log_frequency = kwargs["tensorboard_log_frequency"]
else:
self.log_frequency = log_frequency
if self.tensorboard:
self._summary_writer = tf.summary.create_file_writer(self.model_dir)
if output_types is None:
self._prediction_outputs = None
self._loss_outputs = None
self._variance_outputs = None
self._other_outputs = None
else:
self._prediction_outputs = []
self._loss_outputs = []
self._variance_outputs = []
self._other_outputs = []
for i, type in enumerate(output_types):
if type == 'prediction':
self._prediction_outputs.append(i)
elif type == 'loss':
self._loss_outputs.append(i)
elif type == 'variance':
self._variance_outputs.append(i)
else:
self._other_outputs.append(i)
if len(self._loss_outputs) == 0:
self._loss_outputs = self._prediction_outputs
self._built = False
self._inputs_built = False
self._training_ops_built = False
self._output_functions: Dict[Any, Any] = {}
self._gradient_fn_for_vars: Dict[Any, Any] = {}
def _ensure_built(self) -> None:
"""The first time this is called, create internal data structures."""
if self._built:
return
self._built = True
self._global_step = tf.Variable(0, trainable=False)
self._tf_optimizer = self.optimizer._create_tf_optimizer(
self._global_step)
self._checkpoint = tf.train.Checkpoint(optimizer=self._tf_optimizer,
model=self.model)
def _create_inputs(self, example_inputs: List) -> None:
"""The first time this is called, create tensors representing the inputs and outputs."""
if self._inputs_built:
return
self._ensure_built()
self._inputs_built = True
if (self.model.inputs is not None) and len(self.model.inputs) > 0:
self._input_shapes = [t.shape for t in self.model.inputs]
self._input_dtypes = [
t.dtype.as_numpy_dtype for t in self.model.inputs
]
else:
self._input_shapes = [(None,) + i.shape[1:] for i in example_inputs]
self._input_dtypes = [
np.float32 if x.dtype == np.float64 else x.dtype
for x in example_inputs
]
def _create_training_ops(self, example_batch: Tuple[List, List,
List]) -> None:
"""The first time this is called, create tensors used in optimization."""
if self._training_ops_built:
return
self._create_inputs(example_batch[0])
self._training_ops_built = True
self._label_dtypes = [
np.float32 if x.dtype == np.float64 else x.dtype
for x in example_batch[1]
]
self._weights_dtypes = [
np.float32 if x.dtype == np.float64 else x.dtype
for x in example_batch[2]
]
def fit(self,
dataset: Dataset,
nb_epoch: int = 10,
max_checkpoints_to_keep: int = 5,
checkpoint_interval: int = 1000,
deterministic: bool = False,
restore: bool = False,
variables: Optional[List[tf.Variable]] = None,
loss: Optional[LossFn] = None,
callbacks: Union[Callable, List[Callable]] = [],
all_losses: Optional[List[float]] = None) -> float:
"""Train this model on a dataset.
Parameters
----------
dataset: Dataset
the Dataset to train on
nb_epoch: int
the number of epochs to train for
max_checkpoints_to_keep: int
the maximum number of checkpoints to keep. Older checkpoints are discarded.
checkpoint_interval: int
the frequency at which to write checkpoints, measured in training steps.
Set this to 0 to disable automatic checkpointing.
deterministic: bool
if True, the samples are processed in order. If False, a different random
order is used for each epoch.
restore: bool
if True, restore the model from the most recent checkpoint and continue training
from there. If False, retrain the model from scratch.
variables: list of tf.Variable
the variables to train. If None (the default), all trainable variables in
the model are used.
loss: function
a function of the form f(outputs, labels, weights) that computes the loss
for each batch. If None (the default), the model's standard loss function
is used.
callbacks: function or list of functions
one or more functions of the form f(model, step) that will be invoked after
every step. This can be used to perform validation, logging, etc.
all_losses: Optional[List[float]], optional (default None)
If specified, all logged losses are appended into this list. Note that
you can call `fit()` repeatedly with the same list and losses will
continue to be appended.
Returns
-------
float
The average loss over the most recent checkpoint interval
"""
return self.fit_generator(
self.default_generator(dataset,
epochs=nb_epoch,
deterministic=deterministic),
max_checkpoints_to_keep, checkpoint_interval, restore, variables,
loss, callbacks, all_losses)
def fit_generator(self,
generator: Iterable[Tuple[Any, Any, Any]],
max_checkpoints_to_keep: int = 5,
checkpoint_interval: int = 1000,
restore: bool = False,
variables: Optional[List[tf.Variable]] = None,
loss: Optional[LossFn] = None,
callbacks: Union[Callable, List[Callable]] = [],
all_losses: Optional[List[float]] = None) -> float:
"""Train this model on data from a generator.
Parameters
----------
generator: generator
this should generate batches, each represented as a tuple of the form
(inputs, labels, weights).
max_checkpoints_to_keep: int
the maximum number of checkpoints to keep. Older checkpoints are discarded.
checkpoint_interval: int
the frequency at which to write checkpoints, measured in training steps.
Set this to 0 to disable automatic checkpointing.
restore: bool
if True, restore the model from the most recent checkpoint and continue training
from there. If False, retrain the model from scratch.
variables: list of tf.Variable
the variables to train. If None (the default), all trainable variables in
the model are used.
loss: function
a function of the form f(outputs, labels, weights) that computes the loss
for each batch. If None (the default), the model's standard loss function
is used.
callbacks: function or list of functions
one or more functions of the form f(model, step) that will be invoked after
every step. This can be used to perform validation, logging, etc.
all_losses: Optional[List[float]], optional (default None)
If specified, all logged losses are appended into this list. Note that
you can call `fit()` repeatedly with the same list and losses will
continue to be appended.
Returns
-------
float
The average loss over the most recent checkpoint interval
"""
if not isinstance(callbacks, SequenceCollection):
callbacks = [callbacks]
self._ensure_built()
if checkpoint_interval > 0:
manager = tf.train.CheckpointManager(self._checkpoint,
self.model_dir,
max_checkpoints_to_keep)
avg_loss = 0.0
last_avg_loss = 0.0
averaged_batches = 0
if loss is None:
loss = self._loss_fn
var_key = None
if variables is not None:
var_key = tuple(v.ref() for v in variables)
# The optimizer creates internal variables the first time apply_gradients()
# is called for a new set of variables. If that happens inside a function
# annotated with tf.function it throws an exception, so call it once here.
zero_grads = [tf.zeros(v.shape) for v in variables]
self._tf_optimizer.apply_gradients(zip(zero_grads, variables))
if var_key not in self._gradient_fn_for_vars:
self._gradient_fn_for_vars[var_key] = self._create_gradient_fn(
variables)
apply_gradient_for_batch = self._gradient_fn_for_vars[var_key]
time1 = time.time()
# Main training loop.
for batch in generator:
self._create_training_ops(batch)
if restore:
self.restore()
restore = False
inputs, labels, weights = self._prepare_batch(batch)
# Execute the loss function, accumulating the gradients.
if len(inputs) == 1:
inputs = inputs[0]
batch_loss = apply_gradient_for_batch(inputs, labels, weights, loss)
current_step = self._global_step.numpy()
avg_loss += batch_loss
# Report progress and write checkpoints.
averaged_batches += 1
should_log = (current_step % self.log_frequency == 0)
if should_log:
avg_loss = float(avg_loss) / averaged_batches
logger.info('Ending global_step %d: Average loss %g' %
(current_step, avg_loss))
if all_losses is not None:
all_losses.append(avg_loss)
# Capture the last avg_loss in case of return since we're resetting to
# 0 now
last_avg_loss = avg_loss
avg_loss = 0.0
averaged_batches = 0
if checkpoint_interval > 0 and current_step % checkpoint_interval == checkpoint_interval - 1:
manager.save()
for c in callbacks:
c(self, current_step)
if self.tensorboard and should_log:
self._log_scalar_to_tensorboard('loss', batch_loss,
current_step)
if (self.wandb_logger is not None) and should_log:
all_data = dict({'train/loss': batch_loss})
self.wandb_logger.log_data(all_data, step=current_step)
# Report final results.
if averaged_batches > 0:
avg_loss = float(avg_loss) / averaged_batches
logger.info('Ending global_step %d: Average loss %g' %
(current_step, avg_loss))
if all_losses is not None:
all_losses.append(avg_loss)
last_avg_loss = avg_loss
if checkpoint_interval > 0:
manager.save()
time2 = time.time()
logger.info("TIMING: model fitting took %0.3f s" % (time2 - time1))
return last_avg_loss
def _create_gradient_fn(self,
variables: Optional[List[tf.Variable]]) -> Callable:
"""Create a function that computes gradients and applies them to the model.
Because of the way TensorFlow function tracing works, we need to create a
separate function for each new set of variables.
"""
@tf.function(experimental_relax_shapes=True)
def apply_gradient_for_batch(inputs, labels, weights, loss):
with tf.GradientTape() as tape:
outputs = self.model(inputs, training=True)
if tf.is_tensor(outputs):
outputs = [outputs]
if self._loss_outputs is not None:
outputs = [outputs[i] for i in self._loss_outputs]
batch_loss = loss(outputs, labels, weights)
if variables is None:
vars = self.model.trainable_variables
else:
vars = variables
grads = tape.gradient(batch_loss, vars)
self._tf_optimizer.apply_gradients(zip(grads, vars))
self._global_step.assign_add(1)
return batch_loss
return apply_gradient_for_batch
def fit_on_batch(self,
X: Sequence,
y: Sequence,
w: Sequence,
variables: Optional[List[tf.Variable]] = None,
loss: Optional[LossFn] = None,
callbacks: Union[Callable, List[Callable]] = [],
checkpoint: bool = True,
max_checkpoints_to_keep: int = 5) -> float:
"""Perform a single step of training.
Parameters
----------
X: ndarray
the inputs for the batch
y: ndarray
the labels for the batch
w: ndarray
the weights for the batch
variables: list of tf.Variable
the variables to train. If None (the default), all trainable variables in
the model are used.
loss: function
a function of the form f(outputs, labels, weights) that computes the loss
for each batch. If None (the default), the model's standard loss function
is used.
callbacks: function or list of functions
one or more functions of the form f(model, step) that will be invoked after
every step. This can be used to perform validation, logging, etc.
checkpoint: bool
if true, save a checkpoint after performing the training step
max_checkpoints_to_keep: int
the maximum number of checkpoints to keep. Older checkpoints are discarded.
Returns
-------
float
the loss on the batch
"""
self._ensure_built()
dataset = NumpyDataset(X, y, w)
return self.fit(dataset,
nb_epoch=1,
max_checkpoints_to_keep=max_checkpoints_to_keep,
checkpoint_interval=self._global_step.numpy() +
2 if checkpoint else 0,
variables=variables,
loss=loss,
callbacks=callbacks)
def _predict(self, generator: Iterable[Tuple[Any, Any, Any]],
transformers: List[Transformer],
outputs: Optional[OneOrMany[tf.Tensor]], uncertainty: bool,
other_output_types: Optional[OneOrMany[str]]):
"""
Predict outputs for data provided by a generator.
This is the private implementation of prediction. Do not
call it directly. Instead call one of the public prediction
methods.
Parameters
----------
generator: generator
this should generate batches, each represented as a tuple of the form
(inputs, labels, weights).
transformers: list of dc.trans.Transformers
Transformers that the input data has been transformed by. The output
is passed through these transformers to undo the transformations.
outputs: Tensor or list of Tensors
The outputs to return. If this is None, the model's standard prediction
outputs will be returned. Alternatively one or more Tensors within the
model may be specified, in which case the output of those Tensors will be
returned.
uncertainty: bool
specifies whether this is being called as part of estimating uncertainty.
If True, it sets the training flag so that dropout will be enabled, and
returns the values of the uncertainty outputs.
other_output_types: list, optional
Provides a list of other output_types (strings) to predict from model.
Returns
-------
a NumPy array of the model produces a single output, or a list of arrays
if it produces multiple outputs
"""
results: Optional[List[List[np.ndarray]]] = None
variances: Optional[List[List[np.ndarray]]] = None
if (outputs is not None) and (other_output_types is not None):
raise ValueError(
'This model cannot compute outputs and other output_types simultaneously.'
'Please invoke one at a time.')
if uncertainty and (other_output_types is not None):
raise ValueError(
'This model cannot compute uncertainties and other output types simultaneously.'
'Please invoke one at a time.')
if uncertainty:
assert outputs is None
if self._variance_outputs is None or len(
self._variance_outputs) == 0:
raise ValueError('This model cannot compute uncertainties')
if len(self._variance_outputs) != len(self._prediction_outputs):
raise ValueError(
'The number of variances must exactly match the number of outputs'
)
if other_output_types:
assert outputs is None
if self._other_outputs is None or len(self._other_outputs) == 0:
raise ValueError(
'This model cannot compute other outputs since no other output_types were specified.'
)
if (outputs is not None and self.model.inputs is not None and
len(self.model.inputs) == 0):
raise ValueError(
"Cannot use 'outputs' argument with a model that does not specify its inputs."
"Note models defined in imperative subclassing style cannot specify outputs"
)
if tf.is_tensor(outputs):
outputs = [outputs]
for batch in generator:
inputs, labels, weights = batch
self._create_inputs(inputs)
inputs, _, _ = self._prepare_batch((inputs, None, None))
# Invoke the model.
if len(inputs) == 1:
inputs = inputs[0]
if outputs is not None:
outputs = tuple(outputs)
key = tuple(t.ref() for t in outputs)
if key not in self._output_functions:
self._output_functions[key] = tf.keras.backend.function(
self.model.inputs, outputs)
output_values = self._output_functions[key](inputs)
else:
output_values = self._compute_model(inputs)
if tf.is_tensor(output_values):
output_values = [output_values]
output_values = [t.numpy() for t in output_values]
# Apply tranformers and record results.
if uncertainty:
var = [output_values[i] for i in self._variance_outputs]
if variances is None:
variances = [var]
else:
for i, t in enumerate(var):
variances[i].append(t)
access_values = []
if other_output_types:
access_values += self._other_outputs
elif self._prediction_outputs is not None:
access_values += self._prediction_outputs
if len(access_values) > 0:
output_values = [output_values[i] for i in access_values]
if len(transformers) > 0:
if len(output_values) > 1:
raise ValueError(
"predict() does not support Transformers for models with multiple outputs."
)
elif len(output_values) == 1:
output_values = [
undo_transforms(output_values[0], transformers)
]
if results is None:
results = [[] for i in range(len(output_values))]
for i, t in enumerate(output_values):
results[i].append(t)
# Concatenate arrays to create the final results.
final_results = []
final_variances = []
if results is not None:
for r in results:
final_results.append(np.concatenate(r, axis=0))
if uncertainty and variances is not None:
for v in variances:
final_variances.append(np.concatenate(v, axis=0))
return zip(final_results, final_variances)
if len(final_results) == 1:
return final_results[0]
else:
return final_results
@tf.function(experimental_relax_shapes=True)
def _compute_model(self, inputs: Sequence):
"""Evaluate the model for a set of inputs."""
return self.model(inputs, training=False)
def predict_on_generator(
self,
generator: Iterable[Tuple[Any, Any, Any]],
transformers: List[Transformer] = [],
outputs: Optional[OneOrMany[tf.Tensor]] = None,
output_types: Optional[OneOrMany[str]] = None
) -> OneOrMany[np.ndarray]:
"""
Parameters
----------
generator: generator
this should generate batches, each represented as a tuple of the form
(inputs, labels, weights).
transformers: list of dc.trans.Transformers
Transformers that the input data has been transformed by. The output
is passed through these transformers to undo the transformations.
outputs: Tensor or list of Tensors
The outputs to return. If this is None, the model's
standard prediction outputs will be returned.
Alternatively one or more Tensors within the model may be
specified, in which case the output of those Tensors will
be returned. If outputs is specified, output_types must be
None.
output_types: String or list of Strings
If specified, all outputs of this type will be retrieved
from the model. If output_types is specified, outputs must
be None.
Returns
-------
OneOrMany[np.ndarray]
a NumPy array of the model produces a single output, or a list of arrays
if it produces multiple outputs
"""
return self._predict(generator, transformers, outputs, False,
output_types)
def predict_on_batch(
self,
X: np.typing.ArrayLike,
transformers: List[Transformer] = [],
outputs: Optional[OneOrMany[tf.Tensor]] = None
) -> OneOrMany[np.ndarray]:
"""Generates predictions for input samples, processing samples in a batch.
Parameters
----------
X: ndarray
the input data, as a Numpy array.
transformers: list of dc.trans.Transformers
Transformers that the input data has been transformed by. The output
is passed through these transformers to undo the transformations.
outputs: Tensor or list of Tensors
The outputs to return. If this is None, the model's standard prediction
outputs will be returned. Alternatively one or more Tensors within the
model may be specified, in which case the output of those Tensors will be
returned.
Returns
-------
OneOrMany[np.ndarray]
a NumPy array of the model produces a single output, or a list of arrays
if it produces multiple outputs
"""
dataset = NumpyDataset(X=X, y=None)
return self.predict(dataset, transformers, outputs)
def predict_uncertainty_on_batch(
self,
X: Sequence,
masks: int = 50) -> OneOrMany[Tuple[np.ndarray, np.ndarray]]:
"""
Predict the model's outputs, along with the uncertainty in each one.
The uncertainty is computed as described in https://arxiv.org/abs/1703.04977.
It involves repeating the prediction many times with different dropout masks.
The prediction is computed as the average over all the predictions. The
uncertainty includes both the variation among the predicted values (epistemic
uncertainty) and the model's own estimates for how well it fits the data
(aleatoric uncertainty). Not all models support uncertainty prediction.
Parameters
----------
X: ndarray
the input data, as a Numpy array.
masks: int
the number of dropout masks to average over
Returns
-------
OneOrMany[Tuple[y_pred, y_std]]
y_pred: np.ndarray
predicted value of the output
y_std: np.ndarray
standard deviation of the corresponding element of y_pred
"""
dataset = NumpyDataset(X=X, y=None)
return self.predict_uncertainty(dataset, masks)
def predict(
self,
dataset: Dataset,
transformers: List[Transformer] = [],
outputs: Optional[OneOrMany[tf.Tensor]] = None,
output_types: Optional[List[str]] = None) -> OneOrMany[np.ndarray]:
"""
Uses self to make predictions on provided Dataset object.
Parameters
----------
dataset: dc.data.Dataset
Dataset to make prediction on
transformers: list of dc.trans.Transformers
Transformers that the input data has been transformed by. The output
is passed through these transformers to undo the transformations.
outputs: Tensor or list of Tensors
The outputs to return. If this is None, the model's standard prediction
outputs will be returned. Alternatively one or more Tensors within the
model may be specified, in which case the output of those Tensors will be
returned.
output_types: String or list of Strings
If specified, all outputs of this type will be retrieved
from the model. If output_types is specified, outputs must
be None.
Returns
-------
a NumPy array of the model produces a single output, or a list of arrays
if it produces multiple outputs
"""
generator = self.default_generator(dataset,
mode='predict',
deterministic=True,
pad_batches=False)
return self.predict_on_generator(generator,
transformers=transformers,
outputs=outputs,
output_types=output_types)
def predict_embedding(self, dataset: Dataset) -> OneOrMany[np.ndarray]:
"""
Predicts embeddings created by underlying model if any exist.
An embedding must be specified to have `output_type` of
`'embedding'` in the model definition.
Parameters
----------
dataset: dc.data.Dataset
Dataset to make prediction on
Returns
-------
a NumPy array of the embeddings model produces, or a list
of arrays if it produces multiple embeddings
"""
generator = self.default_generator(dataset,
mode='predict',
pad_batches=False)
return self._predict(generator, [], None, False, ['embedding'])
def predict_uncertainty(
self,
dataset: Dataset,
masks: int = 50) -> OneOrMany[Tuple[np.ndarray, np.ndarray]]:
"""
Predict the model's outputs, along with the uncertainty in each one.
The uncertainty is computed as described in https://arxiv.org/abs/1703.04977.
It involves repeating the prediction many times with different dropout masks.
The prediction is computed as the average over all the predictions. The
uncertainty includes both the variation among the predicted values (epistemic
uncertainty) and the model's own estimates for how well it fits the data
(aleatoric uncertainty). Not all models support uncertainty prediction.
Parameters
----------
dataset: dc.data.Dataset
Dataset to make prediction on
masks: int
the number of dropout masks to average over
Returns
-------
for each output, a tuple (y_pred, y_std) where y_pred is the predicted
value of the output, and each element of y_std estimates the standard
deviation of the corresponding element of y_pred
"""
sum_pred: List[np.ndarray] = []
sum_sq_pred: List[np.ndarray] = []
sum_var: List[np.ndarray] = []
for i in range(masks):
generator = self.default_generator(dataset,
mode='uncertainty',
pad_batches=False)
results = self._predict(generator, [], None, True, None)
if len(sum_pred) == 0:
for p, v in results:
sum_pred.append(p)
sum_sq_pred.append(p * p)
sum_var.append(v)
else:
for j, (p, v) in enumerate(results):
sum_pred[j] += p
sum_sq_pred[j] += p * p
sum_var[j] += v
output = []
std = []
for i in range(len(sum_pred)):
p = sum_pred[i] / masks
output.append(p)
std.append(
np.sqrt(sum_sq_pred[i] / masks - p * p + sum_var[i] / masks))
if len(output) == 1:
return (output[0], std[0])
else:
return list(zip(output, std))
def evaluate_generator(self,
generator: Iterable[Tuple[Any, Any, Any]],
metrics: List[Metric],
transformers: List[Transformer] = [],
per_task_metrics: bool = False):
"""Evaluate the performance of this model on the data produced by a generator.
Parameters
----------
generator: generator
this should generate batches, each represented as a tuple of the form
(inputs, labels, weights).
metric: list of deepchem.metrics.Metric
Evaluation metric
transformers: list of dc.trans.Transformers
Transformers that the input data has been transformed by. The output
is passed through these transformers to undo the transformations.
per_task_metrics: bool
If True, return per-task scores.
Returns
-------
dict
Maps tasks to scores under metric.
"""
evaluator = GeneratorEvaluator(self, generator, transformers)
return evaluator.compute_model_performance(metrics, per_task_metrics)
def compute_saliency(self, X: np.ndarray) -> OneOrMany[np.ndarray]:
"""Compute the saliency map for an input sample.
This computes the Jacobian matrix with the derivative of each output element
with respect to each input element. More precisely,
- If this model has a single output, it returns a matrix of shape
(output_shape, input_shape) with the derivatives.
- If this model has multiple outputs, it returns a list of matrices, one
for each output.
This method cannot be used on models that take multiple inputs.
Parameters
----------
X: ndarray
the input data for a single sample
Returns
-------
the Jacobian matrix, or a list of matrices
"""
input_shape = X.shape
X = np.reshape(X, [1] + list(X.shape))
self._create_inputs([X])
X_b, _, _ = self._prepare_batch(([X], None, None))
# Use a GradientTape to compute gradients.
X_c = tf.constant(X_b[0])
with tf.GradientTape(persistent=True,
watch_accessed_variables=False) as tape:
tape.watch(X_c)
outputs = self._compute_model(X_c)
if tf.is_tensor(outputs):
outputs = [outputs]
final_result = []
for output in outputs:
output_shape = tuple(output.shape.as_list()[1:])