question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NameError: name 'Input' is not defined

See original GitHub issue

I was getting this same error w/ my custom neural network so I wanted to try it out on the example dataset (w/ minor edits). I am still getting this error.

import keras, hyperas, hyperopt, tensorflow
keras.__version__,  hyperopt.__version__, tensorflow.__version__
('2.1.2', '0.1', '1.4.0')

(btw, hyperas doesn’t have a __version__ I just realized)

from __future__ import print_function

from hyperopt import Trials, STATUS_OK, tpe
from keras.datasets import mnist
from keras.layers.core import Dense, Dropout, Activation
from keras.models import Sequential
from keras.utils import np_utils

from hyperas import optim
from hyperas.distributions import choice, uniform, conditional


def data():
    """
    Data providing function:

    This function is separated from create_model() so that hyperopt
    won't reload data for each evaluation run.
    """
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape(60000, 784)
    x_test = x_test.reshape(10000, 784)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    nb_classes = 10
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    return x_train, y_train, x_test, y_test


def create_model(x_train, y_train, x_test, y_test):
    """
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    """
    model = Sequential()
    model.add(Input(784,))
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense({{choice([128, 256])}}))
    model.add(Activation("relu"))
    model.add(Dropout(0.2))

    # If we choose 'four', add an additional fourth layer
    if conditional({{choice(['three', 'four'])}}) == 'four':
        model.add(Dense(100))

        # We can also choose between complete sets of layers


    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
                  optimizer=SGD(0.01))

    model.fit(x_train, y_train,
              batch_size={{choice([64, 128])}},
              epochs=1,
              verbose=2,
              validation_data=(x_test, y_test))
    score, acc = model.evaluate(x_test, y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}


best_run, best_model = optim.minimize(model=create_model,
                                      data=data,
                                      algo=tpe.suggest,
                                      max_evals=5,
                                      trials=Trials(), 
                                      notebook_name="Untitled")

X_train, Y_train, X_test, Y_test = data()
print("Evalutation of best performing model:")
print(best_model.evaluate(X_test, Y_test))
print("Best performing model chosen hyper-parameters:")
print(best_run)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
laxmikantGcommented, Apr 23, 2018

from keras.layers import Input

3reactions
maxpumperlacommented, Apr 4, 2018

@jolespin how about adding Input to imports in your script? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

input() error - NameError: name '...' is not defined
input accepts a string from the user and evaluates the string in the current Python context. When I type dude as input, it...
Read more >
Input error - NameError name is not defined
I am getting an error when I try to run this simple script: input_variable = input ("Enter your ... NameError: name 'Niraj' is...
Read more >
Fix the NameError: Input Name Is Not Defined in Python
The above code has caused a Name Error because input wasn't used to read the string in the older version of Python but...
Read more >
input() error - NameError: name '…' is not defined - Intellipaat
I am getting an error when I try to run this simple python script: input_variable = input ("Enter your name: ... with 2.7....
Read more >
NameError: name 'raw_input' is not defined in Python
The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input() function in Python 3. To solve the error, use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found