【发布时间】:2021-01-12 23:34:21
【问题描述】:
由于某种原因,我所有的卷积神经网络的准确度都非常差。无论编译的模型如何。这是在本地机器上使用带有 CUDA 11.1 的 RTX 3060 TI GPU 的 jupyter notebook。
当我使用 Google Colab 时,我的所有代码都能以高精度正常运行。应该注意的是,这仅适用于卷积神经网络。只有密集连接层的神经网络可以正常工作。
一些细节:
Tensor Flow Version: 2.1.0
Keras Version: 2.2.4-tf
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)]
Pandas 1.2.0
Scikit-Learn 0.24.0
GPU is available
这是一个示例代码(二进制分类 50/50 拆分):
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras import optimizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer = optimizers.RMSprop(lr=1e-6), #decrease learning rate
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(150, 150),batch_size=20,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),
batch_size=20, class_mode='binary')
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data=validation_generator,
validation_steps=50)
结果
WARNING:tensorflow:From <ipython-input-8-f61a1535c537>:6: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
WARNING:tensorflow:sample_weight modes were coerced from
...
to
['...']
WARNING:tensorflow:sample_weight modes were coerced from
...
to
['...']
Train for 100 steps, validate for 50 steps
Epoch 1/30
100/100 [==============================] - 1158s 12s/step - loss: 0.7020 - accuracy: 0.4945 - val_loss: 0.8541 - val_accuracy: 0.4980
Epoch 2/30
100/100 [==============================] - 5s 47ms/step - loss: 0.6987 - accuracy: 0.5105 - val_loss: 0.6930 - val_accuracy: 0.5000 2s - loss: 0.6931 - accura - ETA: 2s - loss: 0.6931 - accura - ETA: 1s - loss: 0.6931 - ETA: 1s - loss: 0.6926 - accuracy: 0. - ETA: 1s - loss: 0.6939 - accuracy - ETA: 0s - los
Epoch 3/30
100/100 [==============================] - 5s 47ms/step - loss: 0.7000 - accuracy: 0.4985 - val_loss: 0.8449 - val_accuracy: 0.5000s - loss: 0.6983 - accuracy - ETA: 0s - loss:
Epoch 4/30
100/100 [==============================] - 5s 47ms/step - loss: 0.6967 - accuracy: 0.4975 - val_loss: 0.7162 - val_accuracy: 0.4800
Epoch 5/30
100/100 [==============================] - 5s 47ms/step - loss: 0.6931 - accuracy: 0.4945 - val_loss: 0.8477 - val_accuracy: 0.49900.6931 - accura - ETA: 0s - loss: 0.6931 - ac - ETA: 0s - loss: 0.6931 -
Epoch 6/30
100/100 [==============================] - 5s 47ms/step - loss: 0.6931 - accuracy: 0.4895 - val_loss: 0.7846 - val_accuracy: 0.5000: 0.6931 - ac
Epoch 7/30
100/100 [==============================] - 5s 47ms/step - loss: 0.6933 - accuracy: 0.4860 - val_loss: 0.7468 - val_accuracy: 0.5000- ETA: 1s - loss: 0.6 - ETA: 0s - loss: 0.6938
【问题讨论】:
-
我已经尝试改变学习率/改变 model.fit() 以及重新安装各种版本我仍然遇到同样的问题
标签: python tensorflow keras deep-learning neural-network