2017-12-11 11 views
0

私はKerasを使用した簡単なフィードフォワードニューラルネットを使用してMNISTデータセットの数字を分類しています。そこで以下のコードを実行します。Keras、models.add()missing 1必要な位置引数: 'layer'

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) 

はしかし、私は次のエラーを取得する:

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

構文はkerasのドキュメントに示されているとおりにあります。私はkeras 2.0.9を使用しているので、バージョンコントロールの問題ではないと思います。私は間違ったことをしましたか?

答えて

4

それは

....確かに完璧なようだが、私はあなたが、シーケンシャルモデルの「インスタンス」を作成し、あなたの代わりにクラス名を使用していない気づい:

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

方法ので、あるクラスの中では常に最初の引数としてselfが含まれていると宣言されているため、インスタンスを持たないメソッドを呼び出すと、インスタンスが最初の引数(つまりself)として必要になります。

このメソッドの定義は、def add(self,layer,...):

です。
関連する問題