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.
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.
【GitHub】TypeError: 'function' object is not subscriptable
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