Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras,models.add() missing 1 required positional argument: 'layer'

I'm classifying digits of the MNIST dataset using a simple feed forward neural net with Keras. So I execute the code below.

import os
import tensorflow as tf

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('/tmp/data', one_hot=True)

# Path to Computation graphs
LOGDIR = './graphs_3'

# start session
sess = tf.Session()

#Hyperparameters
LEARNING_RATE = 0.01
BATCH_SIZE = 1000
EPOCHS = 10

# Layers
HL_1 = 1000
HL_2 = 500

# Other Parameters
INPUT_SIZE = 28*28
N_CLASSES = 10

model = Sequential
model.add(Dense(HL_1, input_dim=(INPUT_SIZE,), activation="relu"))
#model.add(Activation(activation="relu"))
model.add(Dense(HL_2, activation="relu"))
#model.add(Activation("relu"))
model.add(Dropout(rate=0.9))
model.add(Dense(N_CLASSES, activation="softmax"))

model.compile(
    optimizer="Adam",
    loss="categorical_crossentropy",
    metrics=['accuracy'])



# one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

model.fit(
    x=mnist.train.images, 
    y=mnist.train.labels, 
    epochs=EPOCHS, 
    batch_size=BATCH_SIZE)


score = model.evaluate(
    x=mnist.test.images,
    y=mnist.test.labels)

print("score = ", score)

However, I get the following error:

model.add(Dense(1000, input_dim=(INPUT_SIZE,), activation="relu"))
   TypeError: add() missing 1 required positional argument: 'layer'

The syntax is exactly as shown in the keras docs. I am using keras 2.0.9, so I don't think it's a version control problem. Did I do something wrong?

like image 552
Ajay H Avatar asked Dec 11 '17 17:12

Ajay H


1 Answers

It seems perfect indeed....

But I noticed you're not creating "an instance" of a sequential model, your using the class name instead:

#yours: model = Sequential 
#correct:
model = Sequential()

Since the methods in a class are always declared containing self as the first argument, calling the methods without an instance will probably require the instance as the first argument (which is self).

The method's definition is def add(self,layer,...):

like image 74
Daniel Möller Avatar answered Nov 24 '22 01:11

Daniel Möller