我想使用Keras'运行外部优化器。 TFOptimizer
包装并给它tf.contrib.opt.ScipyOptimizerInterface
,但我没有成功。
我的模型是这个(非常小的例子):
model = Sequential()
model.add(InputLayer(input_shape=(6,))
model.add(Dense(3, activation='sigmoid'))
model.add(Dense(5, activation='linear'))
我尝试编译:
model.compile(optimizer=TFOptimizer(ScipyOptimizerInterface()))
但是,当然,它不起作用,因为ScipyOptimizerInterface
构造函数在初始化期间需要loss
参数。
所以,我创建了两个自己的类:
class CGTFOptimizer(object):
def compute_gradients(self, loss, params):
optimizer = ScipyOptimizerInterface(loss, method='cg')
result = optimizer.minimize(loss)
return result
class CGTF(TFOptimizer):
""" Wrapper for TFOptimizer """
def __init__(self):
super(CGTF, self).__init__(optimizer=CGTFOptimizer())
使用model.compile(optimizer=CGTF())
编译模型成功但经过培训后,我从ScipyOptimizerInterface
调用收到以下错误:
Traceback (most recent call last):
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 455, in _apply_op_helper
as_ref=input_arg.is_ref)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 991, in internal_convert_n_to_tensor
ctx=ctx))
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 774, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("training/TFOptimizer/Reshape_25:0", shape=(1,), dtype=int64)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "directly_with_tensorflow.py", line 136, in <module>
cgd_model.fit(X_reference, y_reference, epochs=50, batch_size=5)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 841, in fit
initial_epoch=initial_epoch)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1590, in fit
self._make_train_function()
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 952, in _make_train_function
params=self._collected_trainable_weights, loss=self.total_loss)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/optimizers.py", line 674, in get_updates
grads = self.optimizer.compute_gradients(loss, params)
File "directly_with_tensorflow.py", line 113, in compute_gradients
optimizer = ScipyOptimizerInterface(loss, method='cg')
File "venv/py3/lib/python3.6/site-packages/tensorflow/contrib/opt/python/training/external_optimizer.py", line 126, in __init__
self._packed_var = self._pack(self._vars)
File "venv/py3/lib/python3.6/site-packages/tensorflow/contrib/opt/python/training/external_optimizer.py", line 259, in _pack
return array_ops.concat(flattened, 0)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1099, in concat
return gen_array_ops._concat_v2(values=values, axis=axis, name=name)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 706, in _concat_v2
"ConcatV2", values=values, axis=axis, name=name)
File "venv/py3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 483, in _apply_op_helper
raise TypeError("%s that don't all match." % prefix)
TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [float32, float32, float32, float32, float32, float32, float32, float32, float32, float32, float32, float32, int64] that don't all match.
这可能是TensorFlow中的问题,而不是Keras?我尝试了将loss
转换为float32
,float64
或int64
的不同方法,但没有任何帮助。
X_reference
和y_reference
的形状为(10,6)
,(10,5)
。类型float32
。
有人能指出我如何在Keras中使用ScipyOptimizers吗?