autonomio / talos

Hyperparameter Experiments with TensorFlow and Keras
https://autonom.io
MIT License
1.62k stars 270 forks source link

Index out of range #428

Closed iverleigh closed 4 years ago

iverleigh commented 4 years ago

Hi! I have no idea what's going wrong with my code, please help. This is my first time building a deep learning model so I'm sorry if it's a rookie issue!

params = dict(epochs = [10,20,30])

def create_model(X_train, y_train, X_test, y_test, params):
    model = Sequential()
    model.add(Conv2D(32, (3, 3), padding='same', input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2))) #40x40
    model.add(Dropout(0.25))

    model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2))) #20x20
    model.add(Dropout(0.25))

    model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2))) #10x10
    model.add(Dropout(0.25))

    model.add(Conv2D(32, (10, 10), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2))) #5x5
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))

    model.add(Dense(2, activation='softmax'))

    sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
    history = model.fit(X_train, y_train,batch_size=32,epochs=20,validation_data=(X_test,y_test),class_weight=class_weights)
    return history, model

model = create_model(X_train, y_train, X_test, y_test, params)

This is what I am trying to run, my model instantiation is successful. My Hyperparameter optimization using ta.scan is not:

t = ta.Scan(x=X, y=y, model= create_model, params=params, experiment_name = 'ConvNNets')

This is the output:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-163-75bfb822ac57> in <module>
      3             model= create_model,
      4             params=params,
----> 5             experiment_name = 'ConvNNets')

/anaconda3/lib/python3.7/site-packages/talos/scan/Scan.py in __init__(self, x, y, params, model, experiment_name, x_val, y_val, val_split, random_method, seed, performance_target, fraction_limit, round_limit, time_limit, boolean_limit, reduction_method, reduction_interval, reduction_window, reduction_threshold, reduction_metric, minimize_loss, disable_progress_bar, print_params, clear_session, save_weights)
    194         # input parameters section ends
    195 
--> 196         self._runtime()
    197 
    198     def _runtime(self):

/anaconda3/lib/python3.7/site-packages/talos/scan/Scan.py in _runtime(self)
    199 
    200         from .scan_run import scan_run
--> 201         self = scan_run(self)

/anaconda3/lib/python3.7/site-packages/talos/scan/scan_run.py in scan_run(self)
     24         # otherwise proceed with next permutation
     25         from .scan_round import scan_round
---> 26         self = scan_round(self)
     27         self.pbar.update(1)
     28 

/anaconda3/lib/python3.7/site-packages/talos/scan/scan_round.py in scan_round(self)
     17     # fit the model
     18     from ..model.ingest_model import ingest_model
---> 19     self.model_history, self.round_model = ingest_model(self)
     20     self.round_history.append(self.model_history.history)
     21 

/anaconda3/lib/python3.7/site-packages/talos/model/ingest_model.py in ingest_model(self)
      8                       self.x_val,
      9                       self.y_val,
---> 10                       self.round_params)

<ipython-input-162-2102ec36c34a> in create_model(X_train, y_train, X_test, y_test, params)
      2     model = Sequential()
      3 
----> 4     model.add(Conv2D(32, (3, 3), padding='same', input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]), activation='relu'))
      5     model.add(MaxPooling2D(pool_size=(2, 2))) #40x40
      6     model.add(Dropout(0.25))

IndexError: tuple index out of range
github-actions[bot] commented 4 years ago

Welcome to Talos community! Thanks so much for creating your first issue :)

PurenBITeam commented 4 years ago

Am I wrong or did you never use your params in your create_model function?

mikkokotila commented 4 years ago

Your error seems to come from your input model from this line:

model.add(Conv2D(32, (3, 3), padding='same', input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]), activation='relu'))

One of the shapes you are referencing is out of range.