简体   繁体   中英

keras activation function layer: model.add Activation('relu') gives invalid syntax error

Trying to build layer by following code and an error come out

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D

X = pickle.load(open("X.pickle","rb"))
y = pickle.load(open("y.pickle","rb"))
X = X/255.0

model = Sequential()

model.add(Conv2D(64,(3,3) , input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add((Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss="binary_crossentropy",
             optimizer="adam",
             metrics=['accuracy'])

model.fit(X, y, batch_size=1, validation_split=0.1)

please someone should help out. I use jupyter notebook in python 2.7 environment and the above code returns:

File "<ipython-input-37-f4c444b06108>", line 16
    model.add(Activation("relu"))
        ^
SyntaxError: invalid syntax

This error usually means that the line above the reported line didn't end correctly. Notice that you have an extra parenthesis on line 15. ;)

model.add((Conv2D(64, (3,3))) # <--- 4 open, 3 closed
model.add(Activation("relu"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM