TensorFlow提供了将ValidationMonitors与tf.contrib.learn.DNNClassifier等几个预定义估算器结合使用的可能性 . 但我想使用ValidationMonitor为我自己的估算器,我根据1创建 .
对于我自己的估算工具,我首先初始化一个ValidationMonitor:
validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(testX,testY,every_n_steps=50)
estimator = tf.contrib.learn.Estimator(model_fn=model,model_dir=direc,config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1))
input_fn = tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=1000)
Here I pass the monitor as shown in 2 for tf.contrib.learn.DNNClassifier:
estimator.fit(input_fn=input_fn, steps=1000,monitors=[validation_monitor])
此操作失败并打印出以下错误:
ValueError:功能与给定信息不兼容 . 给定的特征:Tensor(“input:0”,shape =(?,1),dtype = float64),必需的签名:{'x':TensorSignature(dtype = tf.float64,shape = TensorShape([Dimension(None)] ),is_sparse = False)} .
如何将监视器用于我自己的估算器?
谢谢 .
2 回答
将包含testX和testY的input_fn传递给ValidationMonitor而不是直接传递张量testX和testY时,问题就解决了 .
对于记录,您的错误是由ValidationMonitor要求x为
{ 'feature_name_as_a_string' : feature_tensor }
之类的字典引起的,这在input_fn
中是通过调用tf.contrib.learn.io.numpy_input_fn(...)
在内部完成的 .有关如何构建功能字典的更多信息,请参见Building Input Functions with tf.contrib.learn article of the documentation .