tensorflow - Keras 中的扁平化函数

标签 tensorflow keras

定义 NN 架构的问题

我正在尝试使用 Keras 为 CIFAR-10 图像数据集 ( https://keras.io/datasets/ ) 创建一个 CNN,但即使它出现在 Keras 库中,我也无法使用 Flatten 函数:https://keras.io/layers/core/#flatten

这是错误消息:

NameError                                 Traceback (most recent call last)
<ipython-input-9-aabd6bce9082> in <module>()
     12 nn.add(Conv2D(64, 3, 3, activation='relu'))
     13 nn.add(MaxPooling2D(pool_size=(2, 2)))
---> 14 nn.add(Flatten())
     15 nn.add(Dense(128, activation='relu'))
     16 nn.add(Dense(10, activation='softmax'))

NameError: name 'Flatten' is not defined

我正在使用运行 Python 2.7 和 Keras 1.1.1 的 Jupyter。下面是神经网络的代码:
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation

nn = Sequential()
nn.add(Conv2D(32, 3, 3, activation='relu', input_shape=(32, 32, 3)))

# Max-pool reduces the size of inputs, by taking the largest pixel-value from a grid
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Flatten())
nn.add(Dense(128, activation='relu'))
nn.add(Dense(10, activation='softmax'))

提前致谢,

- 约翰 B.

最佳答案

尝试先导入图层:

from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten

nn = Sequential()
nn.add(Conv2D(32, 3, 3, activation='relu', input_shape=(32, 32, 3)))

# Max-pool reduces the size of inputs, by taking the largest pixel-value from a grid
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Flatten())
nn.add(Dense(128, activation='relu'))
nn.add(Dense(10, activation='softmax'))

关于tensorflow - Keras 中的扁平化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426713/

相关文章:

python - tensorflow 随机森林回归

Python Keras - Windows 和 Linux 之间的兼容性

python - 在 keras 层中包装 tensorflow 函数

具有自定义损失函数的 Keras 准确性

python - Keras Tensorflow 二元交叉熵损失大于 1

python - 使用 tensorflow 图转换工具时出错

python - Tensorflow==2.0.0a0 - 属性错误 : module 'tensorflow' has no attribute 'global_variables_initializer'

python - 使用 py_func 时,数据集 API 不会为其输出张量传递维度信息

python - 是否可以使用 CNN 对同一对象的图像 block 进行分类,但区域不同?

c++ - Windows 10 上的 CppFlow