Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用张量流正确地建立估计量_Python_Tensorflow_Tensorflow Estimator - Fatal编程技术网

Python 如何用张量流正确地建立估计量

Python 如何用张量流正确地建立估计量,python,tensorflow,tensorflow-estimator,Python,Tensorflow,Tensorflow Estimator,我想用Python创建一个神经网络,但是我对估计器有一些问题 首先,我阅读了一些关于估计器规范的内容,我认为我正确地创建了估计器类型: estimate_train = tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.TRAIN, loss=loss, train_op=train_op) estimate_test = tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.EVA

我想用Python创建一个神经网络,但是我对估计器有一些问题

首先,我阅读了一些关于估计器规范的内容,我认为我正确地创建了估计器类型:

estimate_train = tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.TRAIN, loss=loss, train_op=train_op)

estimate_test = tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.EVAL, loss=loss) 
但当我想创建用于训练网络的估计器时:

estimator_ = tf.estimator.Estimator(model_fn= estimate_train, model_dir="Path")
出现以下错误:

TypeError                                 Traceback (most recent call last)
/usr/lib/python3.5/inspect.py in getfullargspec(func)
   1088                                        skip_bound_arg=False,
-> 1089                                        sigcls=Signature)
   1090     except Exception as ex:

/usr/lib/python3.5/inspect.py in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, sigcls)
   2155     if not callable(obj):
-> 2156         raise TypeError('{!r} is not a callable object'.format(obj))
   2157 

TypeError: EstimatorSpec(mode='eval', predictions={}, loss=<tf.Tensor 'mean_squared_error/value:0' shape=() dtype=float32>, train_op=None, eval_metric_ops={}, export_outputs=None, training_chief_hooks=(), training_hooks=(), scaffold=<tensorflow.python.training.monitored_session.Scaffold object at 0x7f9ddc083748>, evaluation_hooks=(), prediction_hooks=()) is not a callable object

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-c86a69b4da46> in <module>()
     39 
     40 
---> 41 estimatorn = tf.estimator.Estimator(model_fn= estimate_test, model_dir="/home/jabou/Bureau")

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in __init__(self, model_fn, model_dir, config, params, warm_start_from)
    221     if model_fn is None:
    222       raise ValueError('model_fn must be provided to Estimator.')
--> 223     _verify_model_fn_args(model_fn, params)
    224     self._model_fn = model_fn
    225     self._params = copy.deepcopy(params or {})

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in _verify_model_fn_args(model_fn, params)
   1212 def _verify_model_fn_args(model_fn, params):
   1213   """Verifies model fn arguments."""
-> 1214   args = set(util.fn_args(model_fn))
   1215   if 'features' not in args:
   1216     raise ValueError('model_fn (%s) must include features argument.' % model_fn)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/util.py in fn_args(fn)
     58     if _is_callable_object(fn):
     59       fn = fn.__call__
---> 60     args = tf_inspect.getfullargspec(fn).args
     61     if _is_bounded_method(fn):
     62       args.remove('self')

/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/tf_inspect.py in getfullargspec(obj)
     88   decorators, target = tf_decorator.unwrap(obj)
     89   return next((d.decorator_argspec for d in decorators
---> 90                if d.decorator_argspec is not None), spec_fn(target))
     91 
     92 

/usr/lib/python3.5/inspect.py in getfullargspec(func)
   1093         # else. So to be fully backwards compatible, we catch all
   1094         # possible exceptions here, and reraise a TypeError.
-> 1095         raise TypeError('unsupported callable') from ex
   1096 
   1097     args = []

TypeError: unsupported callable
你能帮我吗

编辑

在回答@f4之后,我更正了我的代码,但仍然有相同的错误:

import tensorflow as tf

sess = tf.Session()

tf.reset_default_graph()

batch_size = 20

def model(param):

    # Values needed to create the network

    input_ = tf.placeholder(tf.float32, shape=(batch_size, 1 , 1 ,1))

    filter_ = tf.placeholder(tf.float32, shape=(batch_size, 1 , 2700 ,1))

    output_network = tf.placeholder(tf.int32, shape=(4,))

    output_real = tf.placeholder(tf.float32)

    x_var = tf.get_variable(name = 'x_var', dtype = tf.float32, initializer = tf.random_normal((batch_size,1,1,1), 0, 0.001)) # Initialised values

    bias = tf.Variable(tf.zeros([2700]))


    # Network

    logits = tf.nn.conv2d_transpose(x_var, filter_ ,output_network,[1,1,3,1],'SAME') + bias

    loss = tf.losses.mean_squared_error(output_real,logits)  # loss function

    optimizer = tf.train.AdamOptimizer(learning_rate=0.001) 

    train_op = optimizer.minimize(loss=loss,global_step=tf.train.get_global_step())

    # Estimators specification
    if param == "train":
        return tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.TRAIN, loss=loss, train_op=train_op)

    if param == "test":
        return tf.estimator.EstimatorSpec(mode=tf.estimator.ModeKeys.EVAL, loss=loss)



# Estimator

estimator_ = tf.estimator.Estimator(model_fn= model("train"), model_dir="Path") 

又怎么了?

您直接给了估计器一个
估计器spec
,它是不正确的

model\u fn
应该是一个函数,它返回
estimator spec
的实例。 稍后将调用此函数。因此,它抱怨说,你所付出的是不可收回的

编辑

不,您再次给出了函数的返回值,您需要传递的是函数本身,它是可调用的:

estimator_ = tf.estimator.Estimator(model_fn = model, model_dir="Path") 
而且你的型号不好。我建议您阅读文档

应具有以下签名:

def my_model_fn(
   features, # This is batch_features from input_fn
   labels,   # This is batch_labels from input_fn
   mode,     # An instance of tf.estimator.ModeKeys
   params):  # Additional configuration
前两个参数是功能和标签的批次 从输入函数返回;也就是说,要素和标签是 模型将使用的数据的句柄。mode参数指示 呼叫方是否要求培训、预测或评估

您应该使用特征和标签,而不是创建palceholder。 此外,在您的型号_fn中,您不应在不处于培训模式时创建培训操作

def my_model_fn(
   features, # This is batch_features from input_fn
   labels,   # This is batch_labels from input_fn
   mode,     # An instance of tf.estimator.ModeKeys
   params):  # Additional configuration