gpt4 book ai didi

python-3.x - TensorFlow 2.5.0 与 NumPy 1.21+ 不兼容? (2021-10-05)

转载 作者:行者123 更新时间:2023-12-05 03:37:05 24 4
gpt4 key购买 nike

致所有偶然发现此问题的人:

我最近在做图像分类(将 CNN 拟合到一些标记数据上),我想使用 keras 的模块进行数据增强。但是,我收到了一个 NotImplementedError。更具体地说,它逐字逐句地说:

NotImplementedError: Cannot convert a symbolic Tensor (sequential_3/sequential/random_rotation/rotation_matrix/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

这是我为增强层编写的代码:

angle = 15
data_augmentation = keras.Sequential([
layers.experimental.preprocessing.RandomFlip('horizontal'),
layers.experimental.preprocessing.RandomRotation(angle/360)
])

所以我想要水平翻转所有图像并随机旋转 15 度。我直接将其插入我的 CNN:

layers_2 = [
#image augmentation layer
data_augmentation,

#convolution layer
keras.layers.Conv2D(16, 3, padding = 'same', activation = 'relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(32, 3, padding = 'same', activation = 'relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(64, 3, padding = 'same', activation = 'relu'),
keras.layers.MaxPooling2D(),
keras.layers.Flatten(),

#dropout for regularization
keras.layers.Dropout(0.2),

#MLP layer
keras.layers.Dense(128, activation = 'relu'),
keras.layers.Dense(64, activation = 'relu'),
keras.layers.Dense(3, activation = 'softmax')
]

model_2 = keras.Sequential(layers_2)

model_2.compile(optimizer = tf.optimizers.Adam(),
loss = tf.keras.losses.SparseCategoricalCrossentropy(),
metrics = [tf.metrics.SparseCategoricalAccuracy()]
)

epochs_2 = 15

#fitting

history_2 = model_2.fit(
normalized_train_ds,
validation_data = normalized_val_ds,
epochs = epochs_2
)

其中 normalized_train_dsnormalized_val_ds 都是标准化的 tensorflow.data.Dataset 对象。

必要的上下文:我在我的本地机器上运行它,在 Python 3.9.7 配置的环境中。我的 NumPy 版本是 1.21.2,我的 TensorFlow 版本是 2.5.0。早在 2021 年 2 月,我就遇到了类似的问题,只是他们注意到有人在运行 Python 3.9.1 和 TensorFlow 2.4.1 以及 NumPy 1.20+ 时遇到了同样的问题(该问题的链接:https://github.com/tensorflow/tensorflow/issues/47360)。

实际问题:我只是写了一些错误的代码,还是我的 Python、TensorFlow、NumPy 版本不兼容?我尝试安装以前版本的 numpy (1.20+),但它引发了同样的问题。如果我在 google Colab notebooks 上运行它,这将不再是一个问题。

最佳答案

根据 github 上的这个问题,tensorflow 2.5 和 numpy 1.20+ 不兼容。 https://github.com/tensorflow/tensorflow/issues/47691

关于python-3.x - TensorFlow 2.5.0 与 NumPy 1.21+ 不兼容? (2021-10-05),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69455899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com