张量流中使用ScipyOptimizerInterface时的TypeError

时间:2017-08-16 17:44:00

标签: python tensorflow tensorflow-gpu

我正在尝试使用ScipyOptimizerInterface来优化某些功能。我尝试运行以下代码片段(稍微修改一下tensorflow文档)以获得有关它如何工作的一些想法。

vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))

optimizer = tf.contrib.opt.ScipyOptimizerInterface(loss, options={'maxiter': 100})

with tf.Session() as session:
    optimizer.minimize(session)
# The value of vector should now be [0., 0.].

但是我收到以下错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-e81545f4bb15> in <module>()
      4 loss = tf.reduce_sum(tf.square(vector))
      5 
----> 6 optimizer = tf.contrib.opt.ScipyOptimizerInterface(loss, options={'maxiter': 100})
      7 
      8 with tf.Session() as session:

/s/anaconda/....../miniconda2/lib/python2.7/site-packages/tensorflow/contrib/opt/python/training/external_optimizer.pyc in __init__(self, loss, var_list, equalities, inequalities, var_to_bounds, **optimizer_kwargs)
    124     self.optimizer_kwargs = optimizer_kwargs
    125 
--> 126     self._packed_var = self._pack(self._vars)
    127     self._packed_loss_grad = self._pack(loss_grads)
    128     self._packed_equality_grads = [

/s/anaconda/...../miniconda2/lib/python2.7/site-packages/tensorflow/contrib/opt/python/training/external_optimizer.pyc in _pack(cls, tensors)
    257     else:
    258       flattened = [array_ops.reshape(tensor, [-1]) for tensor in tensors]
--> 259       return array_ops.concat(flattened, 0)
    260 
    261   def _make_eval_func(self, tensors, session, feed_dict, fetches,

/s/anaconda/...../miniconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(values, axis, name)
   1064   return gen_array_ops._concat_v2(values=values,
   1065                                   axis=axis,
-> 1066                                   name=name)
   1067 
   1068 

/s/anaconda/...../miniconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.pyc in _concat_v2(values, axis, name)
    493   """
    494   result = _op_def_lib.apply_op("ConcatV2", values=values, axis=axis,
--> 495                                 name=name)
    496   return result
    497 

/s/anaconda/...../miniconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    461                                 (prefix, dtype.name))
    462               else:
--> 463                 raise TypeError("%s that don't all match." % prefix)
    464             else:
    465               raise TypeError("%s that are invalid." % prefix)

TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [float64, float32, float32, float32, float32, float32, float32, float32, float32, float64, float64, float32, float32, float32, float32, float32, float32, float32, float32, float32, float64, float32, float32] that don't all match.

我正在使用Continuum Analytics的tensorflow 1.3.0-rc0和python 2.7.12。你能告诉我如何解决这个问题并防止以后发生这个问题吗?

1 个答案:

答案 0 :(得分:0)

以下代码适用于tensorflow版本1.2.0。您还需要初始化变量。

import tensorflow as tf
vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))


optimizer = tf.contrib.opt.ScipyOptimizerInterface(
    loss, options={'maxiter': 100})

with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    print(session.run(vector))  # results: [7.0, 7.0]
    optimizer.minimize(session)
    print(session.run(vector))  # results: [ -1.88996808e-06  -1.88996808e-06]