猪仔很忙 发表于 2020-8-6 17:09:01

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

本帖最后由 猪仔很忙 于 2020-8-7 18:12 编辑

执行以下代码,为什么会输出两次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], axis=-1))() # 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+x+x)()
      return y

def get_adversary(args):
    y_true = args
    y_pred = args
    v_final = args
    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, )
    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(get_adversary)()

输出结果:
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 56, in build_train
    v_final_adv = Lambda(get_adversary)()
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 get_adversary
    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.

陈尚涵 发表于 2020-8-6 17:12:33

无能为力{:5_94:}

Twilight6 发表于 2020-8-6 17:41:27

代码都不完整...我们测试不了

猪仔很忙 发表于 2020-8-6 17:55:56

Twilight6 发表于 2020-8-6 17:41
代码都不完整...我们测试不了

抱歉..以下是代码:
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], axis=-1))() # 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+x+x)()
      return y

def gradient_operation(args):
    y_true = args
    y_pred = args
    v_final = args
    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, )
    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)()

猪仔很忙 发表于 2020-8-6 18:09:08

我用的tensorflow版本是1.14.0,keras是2.2.4

陈尚涵 发表于 2020-8-7 10:20:43

猪仔很忙 发表于 2020-8-6 18:09
我用的tensorflow版本是1.14.0,keras是2.2.4

更加无能为力{:10_285:}
页: [1]
查看完整版本: tf.add()的一个奇怪报错