Keras求和层表现怪异,对训练集求和 [英] Keras summation Layer acting weird, summing over training set

查看:92
本文介绍了Keras求和层表现怪异,对训练集求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解Keras的基本工作方式.我正在尝试一个单一的求和层,该层使用tensorflow作为后端实现为Lambda层:

I am having trouble understanding the basic way Keras works. I am experimenting with a single summation layer, implemented as a Lambda layer using tensorflow as a backend:

from keras import backend as K

test_model = Sequential()
test_model.add( Lambda( lambda x: K.sum(x, axis=0), input_shape=(2,3)) )

x = np.reshape(np.arange(12), (2,2,3))
test_model.predict(x)

这将返回:

array([[  6.,   8.,  10.],
   [ 12.,  14.,  16.]], dtype=float32)

这很奇怪,因为它累加了第一个索引,据我所知,它对应于训练数据的索引.另外,如果我将轴更改为axis=1,则总和将接管第二个坐标,这是我希望为axis=0获得的坐标.

Which is very weird, as it sums over the first index, which to my understanding corresponds to the index of the training data. Also, if I change the axis to axis=1 then the sum is taken over the second coordinate, which is what I would expect to get for axis=0.

这是怎么回事?为什么看起来像axis选择的效果如何将数据传递到lambda层?

What is going on? Why does it seem like the axis chosen effects how the data is passed to the lambda layer?

推荐答案

input_shape是批次的一个样品的形状.
批次中有200个或10000个样本都没有关系,所有样本都应为(2,3).

The input_shape is the shape of one sample of the batch.
It doesn't matter if you have 200 or 10000 samples in a batch, all the samples should be (2,3).

但是批次本身就是从一层传递到另一层的东西.
一批包含"n"个样本,每个样本带有input_shape:

But the batch itself is what is passed along from one layer to another.
A batch contains "n" samples, each sample with the input_shape:

  • 批量形状为(n,2,3)-n个样本,每个样本的input_shape =(2,3)

当需要input_shape时,您无需定义"n",因为当您将fit或其他训练命令与batch_size一起使用时,将定义"n". (在您的示例中,n = 2)

You don't define "n" when input_shape is required, because "n" will be defined when you use fit or another training command, with the batch_size. (In your example, n = 2)

这是原始数组:

[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]

Sample 1 = [ 0  1  2], [ 3  4  5]
Sample 2 = [ 6  7  8], [ 9 10 11]

对索引0(批次大小维度)求和将对样本1和样本2求和:

Summing on index 0 (the batch size dimension) will sum sample 1 with sample 2:

[ 6 8 10], [12 14 16]

对索引1求和将对一个样本的输入形状的第一维求和:

Summing on index 1 will sum the first dimension of one sample's input shape:

[ 3, 5, 7 ], [15, 17, 19]

这篇关于Keras求和层表现怪异,对训练集求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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