简体   繁体   English

为什么在转换为张量流估计量后,这种简单的tf.keras模型无法训练?

[英]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.

相关问题 Problem using estimator.train() after converting keras model to estimator model using tf.keras.estimator.model_to_estimator - Problem using estimator.train() after converting keras model to estimator model using tf.keras.estimator.model_to_estimator 为什么 tf.keras 不起作用,但 tensorflow.keras 是否有效? - Why tf.keras dosen't work but tensorflow.keras does even though tensorflow is imported as tf? 如何使用tf.train训练使用tf.Keras模型创建的模型? - How to train model created with tf.Keras model using tf.train? 使用tf.keras.estimator.model_to_estimator将keras模型转换为估计器模型时出现问题 - Problem converting keras model to estimator model using tf.keras.estimator.model_to_estimator 将冷冻的TensorFlow pb包装在tf.keras模型中 - Wrapping a frozen TensorFlow pb in a tf.keras Model 简单的 tf.keras Resnet50 model 不收敛 - Simple tf.keras Resnet50 model not converging tf.keras和tf.estimator和tf.dataset - tf.keras & tf.estimator & tf.dataset 使用 tf.keras 保存模型 - Saving a model with tf.keras 为什么这个 tf.keras model 在切片输入上的行为与预期不同? - Why does this tf.keras model behave differently than expected on sliced inputs? 无法在Tensorflow估算器中训练Keras预训练模型 - Cannot Train Keras Pre-trained Model in Tensorflow Estimator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM