Repository: keras-team/autokeras

Issue:
DataTransformerMlp doesn't work for batch iteration
<!---
**If you are reporting a bug:**
*  Verify that your issue is not being currently addressed by other issues or pull requests.
*  Please note that Auto-Keras is only compatible with **Python 3.6**.
*  Tag the issue with the `bug report` tag.
-->

### Bug Description
<!---
A clear and concise description of what the bug is.
-->

DataTransformerMlp doesn't work for batch iteration

```
Using TensorFlow backend.
Saving Directory: /tmp/autokeras_ASE3DJ

Initializing search.
Initialization finished.


+----------------------------------------------+
|               Training model 0               |
+----------------------------------------------+
Using TensorFlow backend.
Epoch-1, Current Metric - 0:   0%|                                      | 0/469 [00:00<?, ? batch/s]Process SpawnProcess-1:
Traceback (most recent call last):
  File "/home/boyuan/anaconda3/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/boyuan/anaconda3/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/home/boyuan/workspace/lab/autokeras/autokeras/search.py", line 276, in train
    verbose=verbose).train_model(**trainer_args)
  File "/home/boyuan/workspace/lab/autokeras/autokeras/nn/model_trainer.py", line 126, in train_model
    self._train()
  File "/home/boyuan/workspace/lab/autokeras/autokeras/nn/model_trainer.py", line 157, in _train
    for batch_idx, (inputs, targets) in enumerate(deepcopy(loader)):
  File "/home/boyuan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 314, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/boyuan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 314, in <listcomp>
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/boyuan/workspace/lab/autokeras/autokeras/preprocessor.py", line 302, in __getitem__
    return self.compose(feature), self.target[index]
  File "/home/boyuan/anaconda3/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 49, in __call__
    img = t(img)
  File "/home/boyuan/anaconda3/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 143, in __call__
    return F.normalize(tensor, self.mean, self.std)
  File "/home/boyuan/anaconda3/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 165, in normalize
    raise TypeError('tensor is not a torch image.')
TypeError: tensor is not a torch image.

```

### Reproducing Steps
Steps to reproduce the behavior:
Use

`for batch_idx, (inputs, targets) in enumerate(deepcopy(loader)):`

and the loader is DataTransformerMlp.transform_train


### Setup Details
* same as the requirements.txt
* Linux
* GPU



Discussion:

Resolving patch:
diff --git a/autokeras/preprocessor.py b/autokeras/preprocessor.py
--- a/autokeras/preprocessor.py
+++ b/autokeras/preprocessor.py
@@ -263,13 +263,13 @@ def _transform(self, compose_list, data, targets):
 class DataTransformerMlp(DataTransformer):
     def __init__(self, data):
         super().__init__()
-        self.max_val = data.max()
-        data = data / self.max_val
-        self.mean = np.mean(data, axis=0, keepdims=True).flatten()
-        self.std = np.std(data, axis=0, keepdims=True).flatten()
+        self.mean = np.mean(data, axis=0)
+        self.std = np.std(data, axis=0)
 
     def transform_train(self, data, targets=None, batch_size=None):
-        dataset = self._transform([Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))], data, targets)
+        data = (data - self.mean) / self.std
+        data = np.nan_to_num(data)
+        dataset = self._transform([], data, targets)
 
         if batch_size is None:
             batch_size = Constant.MAX_BATCH_SIZE
@@ -281,7 +281,6 @@ def transform_test(self, data, target=None, batch_size=None):
         return self.transform_train(data, targets=target, batch_size=batch_size)
 
     def _transform(self, compose_list, data, targets):
-        data = data / self.max_val
         args = [0, len(data.shape) - 1] + list(range(1, len(data.shape) - 1))
         data = torch.Tensor(data.transpose(*args))
         data_transforms = Compose(compose_list)


Test patch: