CNN Keras使用Hyperas错误:AttributeError:'str'对象没有属性'ndim'

时间:2019-07-12 15:55:11

标签: python keras conv-neural-network hyperopt hyperas

编译并运行模型,直到代码末尾到达评估模型的行。我认为错误是它收到了一个字符串,但没想到有一个字符串,但是我不知道如何解决它。预先感谢。

def data():

train_data_dir = '/home/bjorn/Downloads/CATS_DOGS2/train'
validation_data_dir = '/home/bjorn/Downloads/CATS_DOGS2/test'
return train_data_dir, validation_data_dir


def model_one(train_data_dir, validation_data_dir):

img_width, img_height = 150, 150

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = Sequential()

model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 input_shape=input_shape, activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Conv2D({{choice([32, 64, 128, 256])}}, 3, 3, border_mode='same',
                 activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense({{choice([32, 64, 128, 256, 512])}},
                activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Dropout({{uniform(0, 0.75)}}))

model.add(Dense({{choice([32, 64, 128, 256, 512])}},
                activation={{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))
model.add(Dropout({{uniform(0, 0.75)}}))

model.add(Dense(1))
model.add(Activation({{choice(['relu', 'sigmoid', 'softmax', 'tanh'])}}))

model.compile(loss='binary_crossentropy',
              optimizer={{choice(['rmsprop', 'adam', 'sgd'])}},
              metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='binary')

result = model.fit_generator(
    train_generator,
    steps_per_epoch=125,
    epochs=5,
    verbose=2,
    validation_data=validation_generator,
    validation_steps=50)

# get the highest validation accuracy of the training epochs
validation_acc = np.amax(result.history['val_acc'])
print('Best validation acc of epoch:', validation_acc)
return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=model_one,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=10,
                                          trials=Trials())

train_data_dir, validation_data_dir = data()
print('Evaluation of best performing model:')
print(best_model.evaluate(validation_data_dir))
print('Best performing model chosen hyper-parameters:')
print(best_run)

最佳表现模型评估:     数据= [数据中x的standardize_single_array(x)]   在第92行的“ /home/bjorn/PycharmProjects/Test/venv/lib/python3.5/site-packages/keras/engine/training_utils.py”文件中     数据= [数据中x的standardize_single_array(x)]   文件“ /home/bjorn/PycharmProjects/Test/venv/lib/python3.5/site-packages/keras/engine/training_utils.py”,第27行,位于standardize_single_array中     elif x.ndim == 1: AttributeError:“ str”对象没有属性“ ndim”

以退出代码1完成的过程

1 个答案:

答案 0 :(得分:0)

我从没用过喀拉拉邦,但从我的眼中看, 很清楚,best_model.evaluate(arg)函数期望将numpy数组作为参数。