fit_generator的损失是0.0000e + 00(使用Keras)

时间:2016-09-08 19:44:16

标签: python deep-learning keras autoencoder

我正在尝试在我的GPU的“大”数据集上使用Keras。为此,我使用了fit_generator,问题是我的损失每次都是0.0000e + 00。

我的打印类和生成器功能:

class printbatch(callbacks.Callback):
    def on_batch_end(self, batch, logs={}):
        if batch%10 == 0:
            print "Batch " + str(batch) + " ends"
    def on_epoch_begin(self, epoch, logs={}):
        print(logs)
    def on_epoch_end(self, epoch, logs={}):
        print(logs)

def simpleGenerator():
    X_train = f.get('X_train')
    y_train = f.get('y_train')
    total_examples = len(X_train)
    examples_at_a_time = 6
    range_examples = int(total_examples/examples_at_a_time)

    while 1:
        for i in range(range_examples): # samples
            yield X_train[i*examples_at_a_time:(i+1)*examples_at_a_time], y_train[i*examples_at_a_time:(i+1)*examples_at_a_time]

这就是我使用它们的方式:

f = h5py.File(cache_file, 'r')

pb = printbatch()
sg = simpleGenerator()

class_weighting = [0.2595, 0.1826, 4.5640, 0.1417, 0.5051, 0.3826, 9.6446, 1.8418, 6.6823, 6.2478, 3.0, 7.3614]

history = autoencoder.fit_generator(sg, samples_per_epoch=366, nb_epoch=10, verbose=2, show_accuracy=True, callbacks=[pb], validation_data=None, class_weight=class_weighting)

这是我输出的一部分:

{}
Epoch 1/100
Batch 0 ends
Batch 10 ends
Batch 20 ends
Batch 30 ends
Batch 40 ends
Batch 50 ends
Batch 60 ends
{'loss': 0.0}
120s - loss: 0.0000e+00
[…]
{}
Epoch 9/10
Batch 0 ends
Batch 10 ends
Batch 20 ends
Batch 30 ends
Batch 40 ends
Batch 50 ends
Batch 60 ends
{'loss': 0.0}
124s - loss: 0.0000e+00
{}
Epoch 10/10
Batch 0 ends
Batch 10 ends
Batch 20 ends
Batch 30 ends
Batch 40 ends
Batch 50 ends
Batch 60 ends
{'loss': 0.0}
127s - loss: 0.0000e+00
Training completed in 1263.76883411 seconds

X_train和y_train形状是:

X_train.shape
Out[5]: (366, 3, 360, 480)
y_train.shape
Out[6]: (366, 172800, 12)

所以我的问题是,我怎样才能解决损失:0.0000e + 00'问题

感谢您的时间。

编辑:该模型,原文来自Prady1na的cradyu1993.github.io/2016/03/08/segnet-post.html。

class UnPooling2D(Layer):
"""A 2D Repeat layer"""
def __init__(self, poolsize=(2, 2)):
    super(UnPooling2D, self).__init__()
    self.poolsize = poolsize

@property
def output_shape(self):
    input_shape = self.input_shape
    return (input_shape[0], input_shape[1],
            self.poolsize[0] * input_shape[2],
            self.poolsize[1] * input_shape[3])

def get_output(self, train):
    X = self.get_input(train)
    s1 = self.poolsize[0]
    s2 = self.poolsize[1]
    output = X.repeat(s1, axis=2).repeat(s2, axis=3)
    return output

def get_config(self):
    return {"name":self.__class__.__name__,
        "poolsize":self.poolsize}


def create_encoding_layers():
    kernel = 3
    filter_size = 64
    pad = 1
    pool_size = 2
    return [
    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(filter_size, kernel, kernel,     border_mode='valid'),
    BatchNormalization(),
    Activation('relu'),
    MaxPooling2D(pool_size=(pool_size, pool_size)),

    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(128, kernel, kernel, border_mode='valid'),
    BatchNormalization(),
    Activation('relu'),
    MaxPooling2D(pool_size=(pool_size, pool_size)),

    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(256, kernel, kernel, border_mode='valid'),
    BatchNormalization(),
    Activation('relu'),
    MaxPooling2D(pool_size=(pool_size, pool_size)),

    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(512, kernel, kernel, border_mode='valid'),
    BatchNormalization(),
    Activation('relu'),
]


def create_decoding_layers():
    kernel = 3
    filter_size = 64
    pad = 1
    pool_size = 2
    return[
    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(512, kernel, kernel, border_mode='valid'),
    BatchNormalization(),

    UpSampling2D(size=(pool_size,pool_size)),
    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(256, kernel, kernel, border_mode='valid'),
    BatchNormalization(),

    UpSampling2D(size=(pool_size,pool_size)),
    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(128, kernel, kernel, border_mode='valid'),
    BatchNormalization(),

    UpSampling2D(size=(pool_size,pool_size)),
    ZeroPadding2D(padding=(pad,pad)),
    Convolution2D(filter_size, kernel, kernel, border_mode='valid'),
    BatchNormalization(),
]

autoencoder = models.Sequential()
autoencoder.add(Layer(input_shape=(3, img_rows, img_cols)))
autoencoder.encoding_layers = create_encoding_layers()
autoencoder.decoding_layers = create_decoding_layers()
for l in autoencoder.encoding_layers:
    autoencoder.add(l)
for l in autoencoder.decoding_layers:
    autoencoder.add(l)

autoencoder.add(Convolution2D(12, 1, 1, border_mode='valid',))
autoencoder.add(Reshape((12,img_rows*img_cols), input_shape=(12,img_rows,img_cols)))
autoencoder.add(Permute((2, 1)))
autoencoder.add(Activation('softmax'))
autoencoder.compile(loss="categorical_crossentropy", optimizer='adadelta')

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题在于' .theanorc'我有浮动16:这还不够,所以我将它改为float64,现在它可以工作了。

这是我的' .theanorc'目前:

[global]
device = gpu
floatX = float64
optimizer_including=cudnn

[lib]
cnmem=0.90

[blas]
ldflags = -L/usr/local/lib -lopenblas

[nvcc]
fastmath = True

[cuda]
root = /usr/local/cuda/