Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 在迁移学习:接收v3中,我的损失为“nan”,准确度为“0.0000e+00”_Tensorflow_Keras_Deep Learning_Computer Vision_Transfer Learning - Fatal编程技术网

Tensorflow 在迁移学习:接收v3中,我的损失为“nan”,准确度为“0.0000e+00”

Tensorflow 在迁移学习:接收v3中,我的损失为“nan”,准确度为“0.0000e+00”,tensorflow,keras,deep-learning,computer-vision,transfer-learning,Tensorflow,Keras,Deep Learning,Computer Vision,Transfer Learning,我正在进行迁移学习。我的用例是对两类图像进行分类。我使用InceptionV3对图像进行分类。在训练我的模型时,我得到了nan作为损失,0.0000e+00作为每个历元的精度。我使用20个纪元,因为我的数据量很小:我得到1000张用于训练的图像,100张用于测试和每批5条记录 from keras.applications.inception_v3 import InceptionV3 from keras.preprocessing import image from keras.models

我正在进行迁移学习。我的用例是对两类图像进行分类。我使用InceptionV3对图像进行分类。在训练我的模型时,我得到了nan作为损失,0.0000e+00作为每个历元的精度。我使用20个纪元,因为我的数据量很小:我得到1000张用于训练的图像,100张用于测试和每批5条记录

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)

x = Dense(512, activation='relu')(x)
x = Dense(32, activation='relu')(x)
# and a logistic layer -- we have 2 classes
predictions = Dense(1, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)


for layer in base_model.layers:
    layer.trainable = False

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
        'C:/Users/Desktop/Transfer/train/',
        target_size=(64, 64),
        batch_size=5,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'C:/Users/Desktop/Transfer/test/',
        target_size=(64, 64),
        batch_size=5,
        class_mode='binary')

model.fit_generator(
        training_set,
        steps_per_epoch=1000,
        epochs=20,
        validation_data=test_set,
        validation_steps=100)

听起来你的梯度在爆炸。这可能有几个原因:

检查您的输入是否正确生成。例如,使用flow_from_directory的save_to_dir参数 由于批处理大小为5,请将每个历元的步长从1000固定到1000/5=200 使用sigmoid激活而不是softmax 在Adam中设置较低的学习率;为此,您需要单独创建优化器,如adam=Adam0.0001,并将其传递到model.compile…,optimizer=adam 尝试VGG16而不是InceptionV3
当您尝试以上所有方法时,请告知我们。

听起来您的梯度正在爆炸。这可能有几个原因:

检查您的输入是否正确生成。例如,使用flow_from_directory的save_to_dir参数 由于批处理大小为5,请将每个历元的步长从1000固定到1000/5=200 使用sigmoid激活而不是softmax 在Adam中设置较低的学习率;为此,您需要单独创建优化器,如adam=Adam0.0001,并将其传递到model.compile…,optimizer=adam 尝试VGG16而不是InceptionV3 当您尝试以上所有操作时,请告知我们。

在单个类的情况下,用于激活没有意义。您的输出值将始终由自身进行规范化,因此等于1。softmax的目的是使数值总和为1。如果是单个值,您将得到它==1。我相信在某个时刻,你得到了0作为预测值,这导致了零除法和NaN损失值

您可以通过以下方式将类数更改为2:

预测=Dense2,激活=softmax'x 类\u mode='classifical'在来自\u目录的流\u中 损失=分类熵 或者对最后一层使用sigmoid激活功能。

在单个类的情况下,用于激活没有意义。您的输出值将始终由自身进行规范化,因此等于1。softmax的目的是使数值总和为1。如果是单个值,您将得到它==1。我相信在某个时刻,你得到了0作为预测值,这导致了零除法和NaN损失值

您可以通过以下方式将类数更改为2:

预测=Dense2,激活=softmax'x 类\u mode='classifical'在来自\u目录的流\u中 损失=分类熵
或者对最后一层使用sigmoid激活函数。

对于softmax输出和二进制交叉熵的组合,应在最后一个密集层中使用2个输出神经元,而不是一个。对于softmax输出和二进制交叉熵的组合,您应该在最后一个密集层中使用2个输出神经元,而不是一个。和数据生成器的“分类”类模式。使用了Sigmoid激活功能…问题仍然存在。您最好尝试两个类。此外,marco给了您非常好的建议。尝试使用hyperparams并找到问题的根源。使用了Sigmoid激活功能…问题仍然存在。您最好尝试两个类。此外,marco给了您非常好的建议。尝试使用hyperparams并找到问题的根源。