[PYTHON] [Hyperas] Type Error: Resolving'function' object is not subscriptable

Introduction

I myself had a troubled error, so I hope it helps someone.

TypeError: 'function' object is not subscriptable

This error is probably due to a problem with the function part.
To fix this issue, let's look back at what we added in the hyperas module.

environment

Module import

from tensorflow.keras import layers
from tensorflow.keras import Input
from hyperas.distributions import choice
from hyperas.distributions import uniform

Before

def create_model(X_train, y_train, X_test, y_test):
    input_tensor = Input(shape=(X_train.shape[1],))
    x = layers.Dense({{choice[128, 256, 512]}}, activation="relu", kernel_regularizer = regularizers.l2({{choice[0.01, 0.001, 0.0001]}}))(input_tensor)
    x = layers.Dropout({{uniform(0, 1)}})(x)
        :

Do you know what's wrong with this?
Next, I will display the After with the modified code.

After

def create_model(X_train, y_train, X_test, y_test):
    input_tensor = Input(shape=(X_train.shape[1],))
    x = layers.Dense({{choice([128, 256, 512])}}, activation="relu", kernel_regularizer = regularizers.l2({{choice([0.01, 0.001, 0.0001])}}))(input_tensor)
    x = layers.Dropout({{uniform(0, 1)}})(x)
        :

Notice how to handle the choice of {{}}.

{{choice[128, 256, 512]}}
{{choice([128, 256, 512])}}

If you don't add () to choice like this, you will get the above error.
This error is

TypeError                                 Traceback (most recent call last)
<ipython-input-55-c060629fd2f2> in <module>
      7         max_evals=5,
      8         trials=trials,
----> 9         notebook_name="sample_notebook"
     10 )
     11 

In this way, the error is pointed out in [optim.minimize ()] in the main function, so it is difficult to identify where the problem is.

The error came from a very simple mistake, but I couldn't figure out what was wrong by reading the error code.

If you get an error similar to mine, it's a good idea to first check if the function is written correctly.

Reference site

【GitHub】TypeError: 'function' object is not subscriptable

How to write in Keras

The source code below is the same as the After source code.

(Since the part to be converted to the model is not displayed, it cannot be said that it is exactly the same, but please note that the model configuration is the same.)

from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import regularizers

def create_model(X_train, y_train, X_test, y_test):
    model = Sequential()
    model.add(layers.Dense({{choice([128, 256, 512])}}, activation="relu", kernel_regularizer = regularizers.l2({{choice([0.01, 0.001, 0.0001])}}), input_shape=(X_train.shape[1],))
    model.add(layers.Dropout({{uniform(0, 1)}})
           :

Recommended Posts

[Hyperas] Type Error: Resolving'function' object is not subscriptable
TypeError:'int' object is not subscriptable
[Django error]'RenameAttributes' object is not iterable
[Python] Name Error: name'urlparse' is not defined
When the selected object in bpy.context.selected_objects is not returned
Python memo using perl-Dictionary type (case is not valid)
When "ERROR: HTTP is not supported." Is displayed in mpsyt