TypeError:__init __()缺少1个必需的位置参数:'units'

时间:2019-05-13 06:05:04

标签: python tensorflow machine-learning deep-learning artificial-intelligence

我正在使用python和张量流,但是我错过了'units'参数,并且我不知道如何解决它,看来您的帖子大部分是代码;请添加更多详细信息。看来您的帖子大部分是代码;请添加更多详细信息。

此处是代码

def createModel():
    model = Sequential()
    # first set of CONV => RELU => MAX POOL layers
    model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=inputShape))
    model.add(Conv2D(32, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25)) 
    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))
    # returns our fully constructed deep learning + Keras image classifier 
    opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
    # use binary_crossentropy if there are two classes
    model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
    return model

print("Reshaping trainX at..."+ str(datetime.now()))
#print(trainX.sample()) 
print(type(trainX)) # <class 'pandas.core.series.Series'>
print(trainX.shape) # (750,)
from numpy import zeros
Xtrain = np.zeros([trainX.shape[0],HEIGHT, WIDTH, DEPTH])
for i in range(trainX.shape[0]): # 0 to traindf Size -1
    Xtrain[i] = trainX[i]
print(Xtrain.shape) # (750,128,128,3)
print("Reshaped trainX at..."+ str(datetime.now()))

print("Reshaping valX at..."+ str(datetime.now()))
print(type(valX)) # <class 'pandas.core.series.Series'>
print(valX.shape) # (250,)
from numpy import zeros
Xval = np.zeros([valX.shape[0],HEIGHT, WIDTH, DEPTH])
for i in range(valX.shape[0]): # 0 to traindf Size -1
    Xval[i] = valX[i]
print(Xval.shape) # (250,128,128,3)
print("Reshaped valX at..."+ str(datetime.now()))

# initialize the model
print("compiling model...")
sys.stdout.flush()
model = createModel()

# print the summary of model
from keras.utils import print_summary
print_summary(model, line_length=None, positions=None, print_fn=None)

# add some visualization
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model).create(prog='dot', format='svg'))

3 个答案:

答案 0 :(得分:3)

Keras Dense层文档如下:

keras.layers.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

使用以下内容:

classifier.add(Dense(6, activation='relu', kernel_initializer='glorot_uniform',input_dim=11))

将起作用,因为这里的单位表示output_dim,表示在隐藏层中需要6个神经元。权重使用统一函数进行初始化,并且输入层具有数据集中的11个独立变量(input_dim),以喂入上述隐藏层。

答案 1 :(得分:2)

尝试更改此行:

model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))

model.add(Dense(NUM_CLASSES, activation='softmax'))

我没有keras的经验,但是在Dense的文档页面上找不到名为output_dim的参数。我想您打算提供单位,但将其标记为output_dim

答案 2 :(得分:2)

我认为这是版本问题。在Dense的更新版keras中,没有“ output_dim”参数。

您可以在此文档链接中找到有关Dense参数的信息。

https://keras.io/api/layers/core_layers/dense/

tf.keras.layers.Dense(
    units,
    activation=None,
    use_bias=True,
    kernel_initializer="glorot_uniform",
    bias_initializer="zeros",
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)

第一个参数是“ units”,这是强制性的。

代替此行:

model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))

使用此:

model.add(Dense(units=NUM_CLASSES, activation='softmax'))

model.add(Dense(NUM_CLASSES, activation='softmax'))