estimator.predict引发“ ValueError:不支持任何值”

Wer*_*sch 2 python tensorflow tensorflow-estimator

所以我基本上从适用于此模型的tensorflow教程中复制了代码:

https://gormanalysis.com/wp-content/uploads/2017/11/intro-to-nnets_sketch2-3.png

它试图对神经网络建模以识别“楼梯”形状,如下所示:


(来源:gormanalysis.com

import numpy as np
import tensorflow as tf
import _pickle as cPickle

with open("var_x.txt", "rb") as fp:   # Unpickling
    var_x = cPickle.load(fp)

with open("var_y.txt", "rb") as fp:   # Unpickling
    var_y = cPickle.load(fp)

# Declare list of features, we only have one real-valued feature
def model_fn(features, labels, mode):

  # Build a linear model and predict values
  W = tf.get_variable("W", [4], dtype=tf.float64)
  b = tf.get_variable("b", [1], dtype=tf.float64)
  y = tf.sigmoid( W*features['x'] + b)
  # Loss sub-graph
  loss = tf.reduce_sum(tf.square(y - labels))
  # Training sub-graph
  global_step = tf.train.get_global_step()
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = tf.group(optimizer.minimize(loss),
                   tf.assign_add(global_step, 1))
  # EstimatorSpec connects subgraphs we built to the
  # appropriate functionality.
  return tf.estimator.EstimatorSpec(
      mode=mode,
      predictions=y,
      loss=loss,
      train_op=train)

estimator = tf.estimator.Estimator(model_fn=model_fn)
# define our data sets
x_train = np.array(var_x)
y_train = np.array(var_y)

input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=10, shuffle=True)

# train
estimator.train(input_fn=input_fn, steps=1000)
# Here we evaluate how well our model did.

print(estimator.get_variable_value("b"))
print(estimator.get_variable_value("W"))

new_samples = np.array(
    [255., 1., 255., 255.], dtype=np.float64)
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": new_samples},
    num_epochs=1,
    shuffle=False)

predictions = list(estimator.predict(input_fn=predict_input_fn))

print(predictions)
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试预测一个显然应该是阶梯的图形时:[255.,1.,255.,255.]我收到“ ValueError:不支持任何值”。培训工作得很好(除了发现的权重与此处的权重不太相似的事实外,请访问以下网址http : //blog.kaggle.com/2017/11/27/introduction-to-neural-networks/)。但是预测方法不起作用。这段代码必须只是张量流示例的副本,适用于x的四维向量。

Oli*_*rot 5

在中model_fn,您可以loss在每种模式(训练/评估/预测)中定义。这意味着即使在预测模式下,labels也将使用并且需要提供。

当您处于预测模式时,实际上您只需要返回预测,以便可以从函数中早返回:

def model_fn(features, labels, mode):
    #...
    y = ...
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=y)
    #...
Run Code Online (Sandbox Code Playgroud)

顺便说一句,W * features返回shape的张量(4,),您需要将其求和后再加上偏差。