[英]Why does this simple tf.keras model not train after converting to a tensorflow estimator?
I'm trying to convert a tf.keras model to a tensorflow estimator using tf.keras.estimator.model_to_estimator
, but the resulting estimator doesn't appear to be trainable. 我正在尝试使用tf.keras.estimator.model_to_estimator
将tf.keras模型转换为张量流估计器,但所得的估计器似乎不是可训练的。
I've tried modelling y = (x_1 + x_2)/2 using both sequential and functional tf.keras API's, and while the tf.keras models work perfectly fine, neither work after converting to estimators. 我尝试使用顺序和功能性tf.keras API来建模y =(x_1 + x_2)/ 2,尽管tf.keras模型工作得非常好,但在转换为估计量后都无法正常工作。 Using a tf.estimator.LinearRegressor
with the same input functions does work, so I don't think the problem is with the input functions. 将tf.estimator.LinearRegressor
与相同的输入函数一起使用确实有效,因此我认为输入函数没有问题。
Here's a minimal working example for the sequentially defined tf.keras model: 这是依次定义的tf.keras模型的最小工作示例:
import numpy as np
import tensorflow as tf
import functools
sample_size = 1000
x_train = np.random.randn(sample_size, 2).astype(np.float32)
y_train = np.mean(x_train, axis=1).astype(np.float32)
x_test = np.random.randn(sample_size, 2).astype(np.float32)
y_test = np.mean(x_test, axis=1).astype(np.float32)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(1, input_shape=(2,), name="Prediction"))
adam = tf.keras.optimizers.Adam(lr=0.1)
model.compile(loss='MSE', optimizer=adam)
#model.fit(x=x_train, y=y_train, epochs=10, batch_size=64) # This works
est = tf.keras.estimator.model_to_estimator(keras_model=model)
def train_input_fn(batch_size):
dataset = tf.data.Dataset.from_tensor_slices(({"Prediction_input": x_train}, y_train))
return dataset.shuffle(sample_size).batch(batch_size).repeat()
def eval_input_fn(batch_size):
dataset = tf.data.Dataset.from_tensor_slices(({"Prediction_input": x_test}, y_test))
return dataset.batch(batch_size)
est.train(input_fn=functools.partial(train_input_fn, 64), steps=10)
eval_metrics = est.evaluate(input_fn=functools.partial(eval_input_fn, 1))
print('Evaluation metrics:', eval_metrics)
The estimator is trained for 10 steps, which should be more than enough to bring the loss down. 估算器经过10个步骤的训练,应该足以减少损失。 Increasing the number of steps makes no difference, as far as I can tell. 据我所知,增加步骤数没有什么区别。
When I run this on tensorflow 1.5.0, I get a warning about calling reduce_mean
with keep_dims
being deprecated when the tf.keras model is compiled, but it trains perfectly well as is. 当我在tensorflow 1.5.0上运行此代码时,我得到一个警告,关于在调用tf.keras模型时不推荐使用keep_dims
调用reduce_mean
的警告,但它可以很好地进行训练。
Is this a bug, or am I missing something? 这是一个错误,还是我缺少什么?
It turns out all I needed to do was reshape the target to have shape (sample_size, 1)
, and increase the number of training steps. 事实证明,我要做的就是将目标重塑为形状(sample_size, 1)
并增加训练步骤。 I'm still not sure what the estimator was doing when the target had shape (sample_size, )
, or why this isn't a problem for the canned estimator, but at least I know how to avoid this. 我仍然不确定在目标具有形状(sample_size, )
时估算器在做什么,或者为什么这对于固定估算器来说不是问题,但是至少我知道如何避免这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.