将使用迭代的函数转换为keras

时间:2018-08-24 12:06:33

标签: python tensorflow keras loss-function

我正在使用VAE模型进行网格布局重新生成(图像)。该代码是用Keras用python 3编写的。我正在计算其他损失函数。对于最终结果(在x_test和x_decoded之间),它工作正常。但是,当我尝试将此损失函数集成为附加损失时,将按批次进行计算。我得到一个错误。

这是我的损失函数

def detect_blocks(x):
outputs=[]
for i, row in enumerate(x):
    last_ele=row[0]
    for j, val in enumerate(row[1:]):
        if val == last_ele:
            continue
        outputs.append([i,j, last_ele])
        last_ele=val
    outputs.append([i,len(row)-1, last_ele])
return outputs

def calculate_accuracy(l1,l2): #should be rescaled !!!
acc = 0
cmp = 0
j = 0
i = 0
len_l1 = len(l1)
len_l2 = len(l2)
initial_length = len_l1
while i < len_l1:
    while j < len_l2:
        if np.array_equal(l1[i], l2[j]):
            cmp += 1
            l1.remove(l1[i])
            l2.remove(l2[j])
            len_l1 = len(l1)
            len_l2 = len(l2)
        elif abs(l1[i][2] - l2[j][2]) < neighborhood_constant:
            if (l1[i][0] == l2[j][0]) and (l1[i][1] == l2[j][1]):
                cmp += 1
                l1.remove(l1[i])
                l2.remove(l2[j])
                len_l1 = len(l1)
                len_l2 = len(l2)
                j = 0
        j += 1
    i += 1
acc = cmp/initial_length
return acc

因此,基本上,我正在检测地面真实情况和再生数据上的方框。然后计算我有多少个匹配框。

这是我对keras的改编,以便将其集成到损失函数中

def calculate_difference_boxes(inputs,outputs):
#this function is responsible of calculating the difference between the 
 detected box from ground truth
#to the regenerated ones
loss = tf.constant(0, dtype= tf.float32)
#sess = tf.Session();
for i in range(batch_size):
    input_reshaped = tf.reshape(inputs[i], [DIM,DIM])
    output_reshaped = tf.reshape(outputs[i], [DIM,DIM])
    with tf.Session() as sess:
        sess.run(input_reshaped)
        sess.run(output_reshaped)
        rectanglesL1 = sess.run([detect_blocks], feed_dict = {x: 
        input_reshaped})
        rectanglesL2 = sess.run([detect_blocks], feed_dict = {x: 
        output_reshaped})
    loss += (len(rectanglesL1) - len(rectanglesL2)) / len(rectanglesL2)
    sess.close()
return loss

但是我遇到此错误:

InvalidArgumentError                      Traceback (most recent call last)
/opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site- 
packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
 1321     try:
-> 1322       return fn(*args)
1323     except errors.OpError as e:

/opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site- 
packages/tensorflow/python/client/session.py in _run_fn(feed_dict, 
fetch_list, target_list, options, run_metadata)
 1306       return self._call_tf_sessionrun(
 -> 1307           options, feed_dict, fetch_list, target_list, 
 run_metadata)
 1308 

  /opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site- 
   packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, 
   options, feed_dict, fetch_list, target_list, run_metadata)
   1408           self._session, options, feed_dict, fetch_list, 
   target_list,
    -> 1409           run_metadata)
    1410     else:

InvalidArgumentError:您必须使用dtype float和shape [?,36]输入占位符张量'encoder_input'的值      [[[节点:encoded_input =占位符类型= DT_FLOAT,形状= [?, 36],_ device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”]]

0 个答案:

没有答案