我正在训练带有两个输出的CovNet。我的训练样本如下所示:
[0, value_a1], [0, value_a2], ...
Run Code Online (Sandbox Code Playgroud)
和
[value_b1, 0], [value_b2, 0], ....
Run Code Online (Sandbox Code Playgroud)
我想生成自己的损失函数和包含的掩码对mask_value = 0。我具有此功能,尽管不确定是否确实可以实现我想要的功能。所以,我想写一些测试。
from tensorflow.python.keras import backend as K
from tensorflow.python.keras import losses
def masked_loss_function(y_true, y_pred, mask_value=0):
'''
This model has two target values which are independent of each other.
We mask the output so that only the value that is used for training
contributes to the loss.
mask_value : is the value that is not used for training
'''
mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
return …Run Code Online (Sandbox Code Playgroud) 这是我的测试代码:
from keras import layers
input1 = layers.Input((2,3))
output = layers.Dense(4)(input1)
print(output)
Run Code Online (Sandbox Code Playgroud)
输出是:
<tf.Tensor 'dense_2/add:0' shape=(?, 2, 4) dtype=float32>
Run Code Online (Sandbox Code Playgroud)
但是什么是重要的?
文件说:
注意:如果图层的输入的排名大于2,则在使用内核的初始点积之前将其展平.
输出重塑了吗?
我正在建立一个具有多个顺序模型的模型,在训练数据集之前我需要将这些模型合并。keras.engine.topology.MergeKeras 2.0 似乎不再支持它。我尝试了keras.layers.Add,keras.layers.Concatenate但效果不佳。
这是我的代码:
model = Sequential()
model1 = Sequential()
model1.add(Embedding(len(word_index) + 1, 300, weights = [embedding_matrix], input_length = 40, trainable = False))
model1.add(TimeDistributed(Dense(300, activation = 'relu')))
model1.add(Lambda(lambda x: K.sum(x, axis = 1), output_shape = (300, )))
model2 = Sequential()
###Same as model1###
model3 = Sequential()
model3.add(Embedding(len(word_index) + 1, 300, weights = [embedding_matrix], input_length = 40, trainable = False))
model3.add(Convolution1D(nb_filter = nb_filter, filter_length = filter_length, border_mode = 'valid', activation = 'relu', subsample_length = 1)) …Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法可以将数据提供给 Keras 中的层(通过 TF)并查看返回值以进行测试,而无需实际构建完整模型并为其拟合数据?
如果没有,如何测试他们开发的自定义层?
我在 Keras 中使用 Python 并运行ImageDataGenerator和使用flow_from_directory. 我有一些有问题的图像文件,所以我可以使用数据生成器来处理读取错误吗?
我在一小部分图像上收到了一些“无效的 jpg 文件”,并希望在没有我的代码崩溃的情况下处理它。
在我的模型中,图层的形状为[None, None, 40, 64]。我想将其重塑为[None, None, 40*64]. 但是,如果我简单地执行以下操作:
reshaped_layer = Reshape((None, None, 40*64))(my_layer)
Run Code Online (Sandbox Code Playgroud)
它会抛出一个错误,抱怨None values not supported.
(需要明确的是,这不是 Keras tf.keras,这只是 Keras)。
我有一个自动编码器,我需要在输出后添加一个高斯噪声层。我需要一个自定义层来执行此操作,但我真的不知道如何生成它,我需要使用张量生成它。

如果我想在下面代码的调用部分实现上面的等式,我该怎么做?
class SaltAndPepper(Layer):
def __init__(self, ratio, **kwargs):
super(SaltAndPepper, self).__init__(**kwargs)
self.supports_masking = True
self.ratio = ratio
# the definition of the call method of custom layer
def call(self, inputs, training=None):
def noised():
shp = K.shape(inputs)[1:]
**what should I put here????**
return out
return K.in_train_phase(noised(), inputs, training=training)
def get_config(self):
config = {'ratio': self.ratio}
base_config = super(SaltAndPepper, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
Run Code Online (Sandbox Code Playgroud)
我也尝试使用 lambda 层来实现,但它不起作用。
我正在尝试实现这个合并层:
policy = merge([out1, out2], mode = lambda x: x[0]-K.mean(x[0])+x[1], output_shape = (out_node,))
Run Code Online (Sandbox Code Playgroud)
但是,Keras 2 中不再存在“合并”。您只能访问公共标准化的“合并”层,例如 Add、Multiply、Dot。
如何在 Keras 2 中实现此功能?我想制作两个合并层,但我不知道如何实现,尤其是因为“K.mean”部分。
作为参考,这里是进口:
from keras.layers import merge
from keras import backend as K
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Keras 中实现一个自定义层,我需要将浮点数张量转换[a, 1+a)为二进制张量以进行屏蔽。我可以看到 Tensorflow 有一个floor函数可以做到这一点,但 Keras 似乎没有在keras.backend. 知道我该怎么做吗?
我试图通过调用predict_proba()Keras 模型来生成班级分数,但似乎这个函数不存在!它是否已被弃用,因为我在 Google 中看到了一些示例?我正在使用 Keras 2.2.2。
keras ×10
python ×9
keras-layer ×6
tensorflow ×5
floor ×1
gaussianblur ×1
image ×1
keras-2 ×1
probability ×1
reshape ×1
testing ×1