Repository: keras-team/autokeras

Issue:
PyTorch size mismatch error
I use Windows10, Python 3.6.5, torch 0.4.1 and tensorflow-gpu==1.8.0. I followed requirements.txt to install others.
I tried to train with autokeras on a custom dataset (6332, 40, 40, 3) with following codes.

```
from autokeras.image_supervised import load_image_dataset
from autokeras import ImageClassifier

if __name__ == '__main__':
    x_train, y_train = load_image_dataset(csv_file_path="train/label.csv", images_path="train")
    print(x_train.shape)
    print(y_train.shape)

    clf = ImageClassifier(verbose=True, augment=True, path="@my_path", resume=True)
    clf.fit(x_train, y_train, time_limit=12 * 60 * 60)
```

I found it generally works. However, sometimes, it showed a PyTorch size mismatch error.

```
.....
Epoch 14: loss 0.9113655090332031, metric_value 0.9318181818181818
No loss decrease after 3 epochs
Father ID:  2
[('to_dense_deeper_model', 14), ('to_conv_deeper_model', 1, 5), ('to_add_skip_model', 20, 5), ('to_concat_skip_model', 1, 5), ('to_add_skip_model', 1, 20), ('to_wider_model', 5, 64), ('to_concat_skip_model', 20, 5), ('to_conv_deeper_model', 20, 5), ('to_dense_deeper_model', 18), ('to_concat_skip_model', 1, 20)]
Saving model.
Model ID: 7
Loss: tensor(0.9246, device='cuda:0')
Metric Value: 0.92739898989899
Training model  8
Traceback (most recent call last):
  File "ak_my.py", line 15, in <module>
    clf.fit(x_train, y_train, time_limit=12 * 60 * 60)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\image_supervised.py", line 238, in fit
    run_searcher_once(train_data, test_data, self.path, int(time_remain))
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\image_supervised.py", line 41, in run_searcher_once
    searcher.search(train_data, test_data, timeout)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\search.py", line 173, in search
    self.metric, self.loss, self.verbose)]))
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\search.py", line 269, in train
    verbose).train_model(**trainer_args)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\utils.py", line 101, in train_model
    self._train()
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\utils.py", line 122, in _train
    outputs = self.model(inputs)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\autokeras\graph.py", line 607, in forward
    temp_tensor = torch_layer(edge_input_tensor)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\torch\nn\modules\linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Users\kmbmjn\Anaconda3\lib\site-packages\torch\nn\functional.py", line 1024, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [128 x 2], m2: [64 x 64] at c:\programdata\miniconda3\conda-bld\pytorch_1533090623466\work\aten\src\thc\generic/THCTensorMathBlas.cu:249
```




Discussion:
@kmbmjn Please specify which version of autokeras are you using.
Thanks.
I met the same error once with autokeras 0.2.4.
But what is strange is that when I re-run the program, the error disappeared, then memory overflow appeared...
@jhfjhfj1 I appreciate your interests. I checked I am using autokeras 0.2.4.
@eagleoflqj That's exactly the same situation. This error appears occasionally.

Resolving patch:
diff --git a/autokeras/graph.py b/autokeras/graph.py
--- a/autokeras/graph.py
+++ b/autokeras/graph.py
@@ -363,7 +363,7 @@ def _block_end_node(self, layer_id, block_size):
         return ret
 
     def _dense_block_end_node(self, layer_id):
-        return self._block_end_node(layer_id, Constant.DENSE_BLOCK_DISTANCE)
+        return self.layer_id_to_input_node_ids[layer_id][0]
 
     def _conv_block_end_node(self, layer_id):
         """Get the input node ID of the last layer in the block by layer ID.
@@ -507,6 +507,7 @@ def extract_descriptor(self):
 
         # The position of each node, how many Conv and Dense layers before it.
         pos = [0] * len(topological_node_list)
+        print(sorted(topological_node_list))
         for v in topological_node_list:
             layer_count = 0
             for u, layer_id in self.reverse_adj_list[v]:
diff --git a/autokeras/net_transformer.py b/autokeras/net_transformer.py
--- a/autokeras/net_transformer.py
+++ b/autokeras/net_transformer.py
@@ -71,7 +71,7 @@ def to_deeper_graph(graph):
     for layer_id in deeper_layer_ids:
         layer = graph.layer_list[layer_id]
         if is_layer(layer, 'Conv'):
-            graph.to_conv_deeper_model(layer_id, randint(1, 2) * 2 + 1)
+            graph.to_conv_deeper_model(layer_id, 3)
         else:
             graph.to_dense_deeper_model(layer_id)
     return graph


Test patch: