Keras自定义指标给出了不正确的张量形状 [英] Keras custom metric gives incorrect tensor shape
问题描述
我想通过定义自己的自定义指标(使用Theano后端)来监控y_pred
的尺寸
I want to monitor the dimension of y_pred
by defining my own custom metric (using the Theano backend)
def shape_test(y_true, y_pred):
return K.shape(y_pred)[0]
我假设自定义指标函数中y_pred
的尺寸等于最小批处理大小.但是,我得到了奇怪的输出.请参见下面的一个可重现的小示例.
I was assuming that the dimension of y_pred
in the custom metric function is equal to the mini batch size. However, I get weird output. See a small reproducible example below.
#imports and definitions
import numpy
numpy.random.seed(1234)
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
neuron_num=20
dim_input=2
#batch size will be important below!
batch_size=2048
TT=int(1e4)
#sample data
X=numpy.random.randn(TT,dim_input)
eps=numpy.random.randn(TT)
Y=0.3*X[:,0]+0.5*X[:,1]+eps
x={"is":X[:(TT/2),:],"os":X[(TT/2+1):,:]}
y={"is":Y[:(TT/2)],"os":Y[(TT/2+1):]}
这是上面给出的自定义指标
This is the custom metric as given above
def shape_test(y_true, y_pred):
return K.shape(y_pred)[0]
现在定义一个简单的NN
Now define a simple NN
sgd=SGD(lr=1e-2,nesterov=True)
model=Sequential()
model.add(Dense(neuron_num,
input_dim=x["is"].shape[1],
init="glorot_normal",
activation="tanh"))
model.add(Dense(neuron_num,init="glorot_normal",activation="tanh"))
model.add(Dense(1,init="glorot_normal",activation="linear"))
model.compile(loss="mean_squared_error",
optimizer=sgd,
metrics=["mean_squared_error",shape_test])
model.fit(x["is"],
y["is"],
validation_data=(x["os"],y["os"]),
nb_epoch=1,
batch_size=batch_size,
verbose=False).history
这给
#{'loss': [1.834826689338684],
# 'mean_squared_error': [1.834826689338684],
# 'shape_test': [1841],
# 'val_loss': [1.4931119817522769],
# 'val_mean_squared_error': [1.4931119817522769],
# 'val_shape_test': [1841.1716343268654]}
我希望看到的是'shape_test': [2048]
而不是'shape_test': [1841]
,因为批处理大小为2048.
I would have expected to see 'shape_test': [2048]
instead of 'shape_test': [1841]
, as the batch size is 2048.
这看起来很奇怪.这可能是错误吗?
我正在使用Python 2.7.6
,Keras==1.0.8
,Theano==0.8.2
和CPU.
This seems very weird. Is this possibly a bug?
I am using Python 2.7.6
, Keras==1.0.8
, Theano==0.8.2
and the CPU.
推荐答案
使用neuron_num=2000
和verbose=True
,这是我可以通过您的示例生成的内容:
Using neuron_num=2000
and verbose=True
, here is what I was able to produce with your example:
Epoch 1/1
2048/5000 [========>............] - ETA: 9s - loss: 1.4507 - shape_test: 2048.000
4096/5000 [=================>...] - ETA: 3s - loss: 1.3577 - shape_test: 2048.000
5000/5000 [=====================] - 26s - loss: 1.3087 - shape_test: 1841.1648 - val_shape_test: 1841.1716
如您所见,形状函数似乎工作正常.但是由于batch_size不是训练集大小的除数,因此最后一批仅包含904个示例.我似乎无法猜测Keras当时是如何提出1841的,但这可能并不复杂.
As you can see, your shape function seems to work fine. But since batch_size is not a divisor of the training set size, the last batch only contains 904 examples. I can't seem to be able to guess how Keras comes up with 1841 at the minute, but it's probably not complicated.
再次尝试使用batch_size=2500
看起来更好:
Another try with batch_size=2500
looks better:
2500/5000 [==========>..........] - ETA: 9s - loss: 1.4292 - shape_test: 2500.0000
5000/5000 [=====================] - 24s - loss: 1.3311 - shape_test: 2500.0000 - val_shape_test: 2499.5001
这篇关于Keras自定义指标给出了不正确的张量形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!