python - estimator.predict 引发 "ValueError: None values not supported"

标签 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)

问题是,当我尝试预测一个显然应该是楼梯的图形时:[255., 1., 255., 255.] 我得到一个“ValueError:不支持任何值。”。训练效果很好(除了它发现的权重与此处的权重不太相似: http://blog.kaggle.com/2017/11/27/introduction-to-neural-networks/ )。但预测方法不起作用。该代码肯定只是 tensorflow 示例的副本,适用于 x 的四维向量。

最佳答案

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

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

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

顺便说一句,W * features 返回形状为 (4,) 的张量,您需要在添加偏差之前对其进行求和。

关于python - estimator.predict 引发 "ValueError: None values not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48373104/

相关文章:

python - 如何使用具有逗号分隔符和空格的 pandas 解析 csv?

Python3 将 Unicode String 转换为 int 表示

python - 在 Django 模板(object.x)中动态显示字段值

python - 张量板嵌入 : "parsing metadata" hangs

python - 为什么我的 TensorFlow Convnet(尝试)训练会导致 NaN 梯度?

tensorflow - 如何评估 tensorflow 估计器每个时期的测试数据集

python子进程和grep命令

python - Tensorflow:损失值与精度不一致

python - tf.estimator shuffle - 随机种子?

python - Tensorflow 在经过训练的 native Keras 模型上提供服务,并具有输入预处理功能