Tensorflow可以将validate_shape
输入参数设置为False
,如果不知道输入大小或在自己的损失函数中定义输入大小的话,可以在此处的文档中看到: https://www.tensorflow.org/api_docs/python/tf/Variable
我想知道是否可以对Keras变量执行相同的操作。我在文档中找不到这样的标志,并且我想知道是否可能,因为在keras文档https://keras.io/backend/
中没有提到此标志在我的情况下,我有自己的损失函数,它将得到一个numpy数组。
def examplaryLoss(y_target,y_predicted):
numpy_in_keras = K.variable(y_target)
return y_predicted - y_target
基于这种损失编译模型:
model.compile(optimizer='nadam',loss=examplaryLoss)
我将得到错误,因为必须指定形状:
ValueError: initial_value must have shape specief: Tensor("dense_6_target_3:=0",shape=(?,?),dtype=float32)
如果我调整损失以使用张量流变量,例如:
def examplaryLoss(y_target,y_predicted):
numpy_in_keras = tf.Variable(y_target,validate_shape=False)
return y_predicted - y_target
模型的编译有效,但是当尝试使用
拟合数据时model.fit(...)
我会收到错误消息:
InvalidArguemntError (see above for traceback): You must feed a value for placeholder tensor 'dense_6_target_4' with dtype float [[Node: dense_6_target_4 = Placeholder[dtype=DT_FLOAT,shape=[],_device="/job:/localhost/replica:0/task:0/gpu:0"]()]]
我认为这是由于导入tensorflow而发生的-现在它希望模型中包含变量,但是这会破坏我的工作流程,因此我需要重新实现很多,所以我想知道是否有更简单的方法告诉Keras,它不应该验证形状,可以在Tensorflow中完成。
我正在使用GTX 1080的Ubuntu 16.04