使用ScipyOptimizerInterface當我嘗試使用ScipyOptimizerInterface優化某些功能。我試圖運行下面的代碼片段(從tensorflow文檔稍微修改),以獲得它如何工作的一些想法。類型錯誤在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.
我使用tensorflow從連續分析1.3.0-RC0和python 2.7.12。你能否告訴我們如何解決這個問題,並防止它發生?
錯誤似乎很清楚,您傳遞的張量列表不匹配類型。您可能需要找到某種方法將丟失對象轉換爲所有Float64的列表(以保持完整的精度)。 – csunday95
嗨csunday 95!謝謝你的評論!我的確嘗試過使用下面的代碼:loss = tf.cast(tf.reduce_sum(tf.square(vector)),dtype = tf.float32)但編譯器仍然抱怨第一個參數和其他兩個在initilization優化器是float64! – Kratos1808