Ai Edogawa 2020-08-06 17:18 采纳率: 0%
浏览 200

Tensorflow: tf.add()的一个奇怪报错

执行以下代码,为什么会输出两次perturb?而且第二次的类型为什么变成None了?

import sys
sys.path.append('../')
from keras import Input
from keras.layers import Dense
import keras.backend as K
from keras.layers import Lambda
from keras.layers import LSTM
import tensorflow as tf

class RNN_SEPARATE_2(object):

    def __init__(self,
                 time_step=64,
                 fearure_dim=2,
                 hidden_size=256, # 128
                 dropout_rate=0.2,
                 name='rnn'):
        self.name = name
        self.time_step = time_step
        self.fearure_dim = fearure_dim
        self.hidden_size = hidden_size
        self.dropout_rate = dropout_rate

    def __call__(self, inputs):
        x = inputs
        x_1 = Lambda(lambda x: x[:,:,0])(x)
        x_1 = Lambda(lambda x: K.expand_dims(x, axis=-1))(x_1)
        x_2 = Lambda(lambda x: x[:,:,1:])(x)

        h_2 = LSTM(self.hidden_size, return_sequences=True)(x_2) # batch, seq2, dim
        h_2_first = Lambda(lambda x: x[:,1:,])(h_2)
        h_2_last = Lambda(lambda x: x[:,-1,:])(h_2)

        h_1 = LSTM(self.hidden_size, return_sequences=True)(x_1) # batch, seq1, dim
        h_1_first = Lambda(lambda x:x[:1:,:])(h_1)
        h_1_last = Lambda(lambda x: x[:,-1,:])(h_1) # batch, dim

        h = Lambda(lambda x: K.concatenate([x[0], x[1]], axis=-1))([h_1, h_2]) # batch, seq, dim
        h = LSTM(self.hidden_size, return_sequences=True)(h) # batch, seq, dim
        h = LSTM(self.hidden_size, return_sequences=True)(h) # batch, seq, dim
        h_first = Lambda(lambda x: x[:,:-1,:])(h) # batch, seq-1, dim
        h_last = Lambda(lambda x: x[:,-1,:])(h) # batch, dim

        y = Lambda(lambda x:x[0]+x[1]+x[2])([h_1_last, h_2_last, h_last])
        return y

def gradient_operation(args):
    y_true = args[0]
    y_pred = args[1]
    v_final = args[2]
    pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
    pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
    loss = -K.mean(0.75 * K.pow(1. - pt_1, 0) * K.log(pt_1)) - K.mean((1 - 0.75) * K.pow(pt_0, 0) * K.log(1. - pt_0))
    perturb = tf.gradients(loss, [v_final])[0]
    print(perturb)
    v_final_adv = tf.add(v_final, perturb)
    return v_final_adv

time_step = 10
feature_dim = 7
trace_input = Input(shape=(time_step, feature_dim))
label_input = Input(shape=(1,))
v_final = RNN_SEPARATE_2(time_step, feature_dim)(trace_input)
pred = Dense(1, activation='sigmoid', name='pred')(v_final)
v_final_adv = Lambda(gradient_operation)([label_input, pred, v_final])

输出结果:

Tensor("lambda_12/gradients/pred/MatMul_grad/MatMul:0", shape=(?, 256), dtype=float32)
None

报错信息:

Traceback (most recent call last):
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 527, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 1224, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 305, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 246, in constant
    allow_broadcast=True)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 284, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 454, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 541, in _apply_op_helper
    values, as_ref=input_arg.is_ref).dtype.name
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 1224, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 305, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 246, in constant
    allow_broadcast=True)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 284, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 454, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/PycharmProjects/turningpoint-master/main.py", line 67, in <module>
    modelName=modelName)
  File "D:\PycharmProjects\turningpoint-master\baseline_main_rnn.py", line 37, in train_rnn_turning_point
    clf = rnn_turning_point.build_train(datas, machineID, modelName)
  File "D:\PycharmProjects\turningpoint-master\model\rnn_turning_point.py", line 55, in build_train
    v_final_adv = Lambda(gradient_operation)([label_input, pred, v_final])
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\engine\base_layer.py", line 474, in __call__
    output_shape = self.compute_output_shape(input_shape)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 649, in compute_output_shape
    x = self.call(xs)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 687, in call
    return self.function(inputs, **arguments)
  File "D:\PycharmProjects\turningpoint-master\model\rnn_turning_point.py", line 37, in gradient_operation
    v_final_adv = tf.add(v_final, perturb)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 386, in add
    "Add", x=x, y=y, name=name)
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 545, in _apply_op_helper
    (input_name, err))
ValueError: Tried to convert 'y' to a tensor and failed. Error: None values not supported.
  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题