2017-07-11 3 views
0

私はテンソルフローが初めてです。独自の見積もりを作成し、モデルをトレーニングする方法に関するドキュメントのいくつかに従っています。カスタムモデルを定義してモデルをトレーニングすることができました。しかし、値を予測して読み取ろうとすると、estimator.predictによって返された値を読み取ることができません。以下は、誰かが私を助けてください可能性があり、これは私がTF1.2を使用していますエラーテンソルフローでestimator.predictから返された予測を読み取ることができません

Traceback (most recent call last): 
    File "/Users/hdattada/PycharmProjects/TensorFlowIntro/custom_model_linear.py", line 49, in <module> 
    predictions = list(itertools.islice(estimate_metrics, 15)) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 339, in predict 
    model_fn_lib.ModeKeys.PREDICT) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 545, in _call_model_fn 
    features=features, labels=labels, **kwargs) 
    File "/Users/hdattada/PycharmProjects/TensorFlowIntro/custom_model_linear.py", line 12, in model_fn 
    loss = tf.reduce_sum(tf.square(y - labels)) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 829, in binary_op_wrapper 
    y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y") 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 676, in convert_to_tensor 
    as_ref=False) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 741, in internal_convert_to_tensor 
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 113, in _constant_tensor_conversion_function 
    return constant(v, dtype=dtype, name=name) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 102, in constant 
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 
    File "/Users/hdattada/.virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 364, in make_tensor_proto 
    raise ValueError("None values not supported.") 
ValueError: None values not supported. 

をスロー

import numpy as np 
import tensorflow as tf 

# 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", [1], dtype=tf.float64) 
    b = tf.get_variable("b", [1], dtype=tf.float64) 
    y = 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([1., 2., 3., 4.]) 
y_train = np.array([0., -1., -2., -3.]) 
x_eval = np.array([2., 5., 8., 1.]) 
y_eval = np.array([-1.01, -4.1, -7, 0.]) 
input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x":x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True) 
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x":x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False) 
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x":x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False) 

estimate_input_fn = tf.estimator.inputs.numpy_input_fn({"x": x_eval},shuffle=False) 

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

estimate_metrics = estimator.predict(input_fn=estimate_input_fn, predict_keys=['y']) 

print(list(estimate_metrics)) 

私のコードサンプルですか?

+0

あなたはより多くの手掛かりのために、すべてのエラー・スタックを投稿する必要がありますあなたの問題。 –

+0

@ TianjinGu私は完全なエラースタックを追加しました –

答えて

2

Estimator.predictを呼び出すときにlabelsはNoneになりますので、グラフはmodel_fnを呼び出すときに、次のように、あなたはあなたのmodel_fnを変更することができ、エラーを構築します:

def model_fn(features, labels, mode): 
    # Build a linear model and predict values 
    W = tf.get_variable("W", [1], dtype=tf.float64) 
    b = tf.get_variable("b", [1], dtype=tf.float64) 
    y = W*features['x'] + b 
    loss = None 
    train = None 
    if labels is not None: 
     # 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":y}, 
     loss=loss, 
     train_op=train) 
+0

驚くばかりです。ありがとうございました –

関連する問題