keras自定义丢失纯python(无keras后端) [英] keras custom loss pure python (without keras backend)

查看:56
本文介绍了keras自定义丢失纯python(无keras后端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为图像压缩编写自动编码器.我想使用以纯python编写的自定义损失函数,即不使用keras后端函数.这有可能吗?如果可以,怎么办? 如果可能的话,我将非常感谢您提供一个最低限度的工作示例(MWE). 请查看此MWE,尤其是mse_keras函数:

I am currently programming an autoencoder for image compression. I would like to use a custom loss function written in pure python, i.e. without making use of keras backend functions. Is this at all possible and if so how? If it is possible I'd be very grateful for a minimum working example (MWE). Please look at this MWE, in particular the mse_keras function:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import keras.backend as K
from keras.datasets import mnist
from keras.models import Model, Sequential
from keras.layers import Input, Dense


def mse_keras(A,B):
    mse = K.mean(K.square(A - B), axis=-1)
    return mse


# Loads the training and test data sets (ignoring class labels)
(x_train, _), (x_test, _) = mnist.load_data()

# Scales the training and test data to range between 0 and 1.
max_value = float(x_train.max())
x_train = x_train.astype('float32') / max_value
x_test = x_test.astype('float32') / max_value


x_train.shape, x_test.shape
# ((60000, 28, 28), (10000, 28, 28))


x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

(x_train.shape, x_test.shape)
# ((60000, 784), (10000, 784))


# input dimension = 784
input_dim = x_train.shape[1]
encoding_dim = 32

compression_factor = float(input_dim) / encoding_dim
print("Compression factor: %s" % compression_factor)

autoencoder = Sequential()
autoencoder.add(Dense(encoding_dim, input_shape=(input_dim,), activation='relu'))
autoencoder.add(Dense(input_dim, activation='sigmoid'))

autoencoder.summary()

input_img = Input(shape=(input_dim,))
encoder_layer = autoencoder.layers[0]
encoder = Model(input_img, encoder_layer(input_img))

encoder.summary()


autoencoder.compile(optimizer='adam', loss=mse_keras, metrics=['mse'])
history=autoencoder.fit(x_train, x_train,
                        epochs=3,
                        batch_size=256,
                        shuffle=True,
                        validation_data=(x_test, x_test))

num_images = 10
np.random.seed(42)
random_test_images = np.random.randint(x_test.shape[0], size=num_images)

decoded_imgs = autoencoder.predict(x_test)


#print(history.history.keys())

plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test', 'mse1', 'val_mse1'], loc='upper left')
plt.show()


plt.figure(figsize=(18, 4))

for i, image_idx in enumerate(random_test_images):
    # plot original image
    ax = plt.subplot(3, num_images, i + 1)
    plt.imshow(x_test[image_idx].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # plot reconstructed image
    ax = plt.subplot(3, num_images, 2*num_images + i + 1)
    plt.imshow(decoded_imgs[image_idx].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

上面的代码是使用Keras后端的自定义损失函数的MWE.但是,这不是我想要的!我想用以下代码代替代码中的mse_keras函数:

The code above is a MWE for a custom loss function using the Keras backend. However, this is not what I want! I would like to substitute the mse_keras function in my code with something like this:

def my_mse(A,B):
    mse = ((A - B) ** 2).mean(axis=None)
    return mse

这又只是一个MWE.它是纯python和scipy.没有KERAS后端! 是否可以使用纯python函数作为损失函数(我尝试使用py_func,但对我而言不起作用.) 我问的原因是因为最终我想使用已经在python中实现的更复杂的损失函数.而且,我看不到如何使用keras后端来重新实现它. (老实说,我也没有时间这样做)

This is again just a MWE. It is pure python and scipy. NO KERAS BACKEND! Is it possible to to use pure python functions as loss functions (I tried with py_func, but it didn't work for me.) The reason why I am asking is because eventually I would like to use a way more complicated loss function which is already implemented in python. And, I don't see how I could reimplement it using the keras backend. (I also don't have the time to do that, to be honest)

(出于好奇:我想用作损失函数的函数可以在这里看到: https://github.com/aizvorski/video-quality )

(For the curious: The functions which I would like to use as a loss function can be seen here: https://github.com/aizvorski/video-quality)

任何帮助将不胜感激.后端可以是theano,tensorflow,我不在乎.如果可能的话,请在python 3.X中为我提供MWE.

Any help would be greatly appreciated. Backend can be theano, tensorflow, I dodn't care. If it is possible please provide me with a MWE in python 3.X.

非常感谢.非常感谢您的帮助.

Many thanks in advance. Your help is much appreciated.

推荐答案

您不能使用纯Python函数作为Keras的损失.当您可能在GPU上训练并且python使用CPU时,这会通过从GPU内存传输结果到GPU内存而产生开销.

You cannot use a pure Python function as a loss for Keras. As you probably train on a GPU and python uses the CPU this would create overhead by transferring results from/to the GPU memory.

来自 https://keras.io/losses/

您可以传递现有损失函数的名称,也可以传递TensorFlow/Theano 符号函数,该函数为每个数据点返回一个标量,并采用以下两个参数:y_true,y_pred

You can either pass the name of an existing loss function, or pass a TensorFlow/Theano symbolic function that returns a scalar for each data-point and takes the following two arguments: y_true, y_pred

您的功能将是(与原始功能相同)

Your function would be (same as the original one)

def my_mse(A,B):
    mse = K.mean(K.pow(A - B, 2), axis=None)
    return mse

但是,请检查Keras API,它需要每个数据点都有一个标量,因此对于axis=None,取平均值可能无法这样工作.

However, check the Keras API, it wants a scalar for each data point, so taking the mean will probably not work like this with axis=None.

我快速了解了您链接的损失函数,并应该在Keras中实现它们,而不是太困难. Keras(或实际上是后端Tensorflow)具有与numpy类似的接口.了解后端的计算图(即张量流)如何实现损失可能很有用.

I had a quick look at the loss functions you linked and implementing them in Keras should be possible and not too difficult. Keras (or actually the backend Tensorflow) has a similar interface to numpy. It might be useful to understand how the computational graph of the backend (i.e. tensorflow) works to implement the losses.

这篇关于keras自定义丢失纯python(无keras后端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆