bug6-_SymbolicException: Inputs to eager execution function cannot be Keras symbolic

1147-柳同学

发表文章数:589

热门标签

, ,
首页 » 算法 » 正文

代码错误源码-最后一行为重点

x_train shape: (25000, 80) tf.Tensor(1, shape=(), dtype=int64) tf.Tensor(0, shape=(), dtype=int64)
x_test shape: (25000, 80)
Epoch 1/4
      1/Unknown - 2s 2s/step
      1/Unknown - 2s 2s/stepTraceback (most recent call last):
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/execute.py", line 61, in quick_execute
    num_outputs)
TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: my_rnn/simple_rnn_cell/cond/Identity:0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-51fb9dfd186b>", line 1, in <module>
    runfile('D:/PycharmProjects/MyTest/深度学习/sentiment_analysis_single_layer.py', wdir='D:/PycharmProjects/MyTest/深度学习')
  File "D:/softwares/PyCharm/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:/softwares/PyCharm/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"/n", file, 'exec'), glob, loc)
  File "D:/PycharmProjects/MyTest/深度学习/sentiment_analysis_single_layer.py", line 115, in <module>
    main()
  File "D:/PycharmProjects/MyTest/深度学习/sentiment_analysis_single_layer.py", line 108, in main
    model.fit(db_train, epochs=epochs,validation_data = db_test)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
    use_multiprocessing=use_multiprocessing)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 324, in fit
    total_epochs=epochs)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 123, in run_one_epoch
    batch_outs = execution_function(iterator)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 86, in execution_function
    distributed_function(input_fn))
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in __call__
    result = self._call(*args, **kwds)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/def_function.py", line 520, in _call
    return self._stateless_fn(*args, **kwds)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/function.py", line 1823, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/function.py", line 1141, in _filtered_call
    self.captured_inputs)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/function.py", line 1224, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/function.py", line 511, in call
    ctx=ctx)
  File "D:/softwares/anaconda3/envs/your_env_name/lib/site-packages/tensorflow_core/python/eager/execute.py", line 75, in quick_execute
    "tensors, but found {}".format(keras_symbolic_tensors))
tensorflow.python.eager.core._SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'my_rnn/simple_rnn_cell/cond/Identity:0' shape=(None, 100) dtype=float32>]

代码如下

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,optimizers,Sequential,losses

tf.random.set_seed(22)
np.random.seed(22)
assert tf.__version__.startswith('2.')

# 数据集加载
# imdb是一个电影评论的数据集
# 将生僻单词归结为一个单词(未知单词),total_words表示常见单词的数量
total_words = 10000
max_review_len = 80 # 设置句子长度
batchsz = 64
embedding_len = 100
(x_train,y_train),(x_test,y_test) = keras.datasets.imdb.load_data(num_words = total_words)

# pad,将句子长度padding成一个长度,方便用一个线性层来处理
# x_train :[b,80]  x_test : [b,80]
x_train = keras.preprocessing.sequence.pad_sequences(x_train,maxlen = max_review_len)
x_test = keras.preprocessing.sequence.pad_sequences(x_test,maxlen = max_review_len)

# 构建数据集
db_train = tf.data.Dataset.from_tensor_slices((x_train,y_train))
# drop_remainder=True表示当最后一个bath小于batchsz时,会将最后一个batch丢弃掉
db_train = db_train.shuffle(1000).batch(batchsz,drop_remainder=True)
db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
db_test = db_test.batch(batchsz,drop_remainder=True)
print('x_train shape:',x_train.shape,tf.reduce_max(y_train),tf.reduce_min(y_train))
print('x_test shape:',x_test.shape)


# 构建网络结构
class MyRNN(keras.Model):
    def __init__(self,units):
        super(MyRNN, self).__init__()
        # [b,64]
        self.state0 = [tf.zeros([batchsz,units])]

        # transfrom test to embedding representation
        # [b,80] => [b,80,100]     每个单词用一个100维的向量来表示
        self.embedding = layers.Embedding(total_words,embedding_len,input_length=max_review_len)

        # 将句子单词的数量在时间轴上展开
        # [b,80,100] => [b,64]  ,h_dim : 64
        # RNN : cell1.cell2.cell3
        # SimpleRNN已经在内部完成了时间轴上的展开
        self.rnn_cell0 = layers.SimpleRNNCell(units,dropout=0.5)

        # outlayer , [b,64] => [b,1]
        self.outlayer = layers.Dense(1)  # 1个输出结点

    #  定义前向传播
    def call(self,inputs,training=None):
        """
        train mode : net(x),net(x,training=True)
        test mode : net(x,training=False)
        :param inputs: [b,80]
        :param training: 通过设置training参数来设置是否断掉短路连接(dropout是否起作用)
        :return:
        """
        # [b,80]
        x = inputs
        # embedding : [b,80] => [b,80,100]
        x = self.embedding(x)
        # rnn cell compute
        # [b,80,100] => [b,64]
        state0 = self.state0
        # word : [b,100]
        for word in tf.unstack(x,axis=1):   # tf.unstack(x,axis=1)表示对x在1维上进行展开
            # h_t = x_t*w_xh + h_t-1 * w_hh , 输入状态word,历史化状态state
            # out,state1是相同的,只是为了与rnn作match,这种情况两者返回是不同的
            out,state1 = self.rnn_cell0(word,state0)
            # 重新赋值做循环
            state0 = state1

        # 累积的所有的单词的语义信息
        # out : [b,64] => [b,1]
        x = self.outlayer(out)
        prob = tf.sigmoid(x)

        return prob


def main():
    units = 64
    epochs = 4

    model = MyRNN(units)
    # 模型训练
    model.compile(optimizer = optimizers.Adam(lr = 1e-3),
                  loss = tf.losses.BinaryCrossentropy(),
                  metrics = ['accuracy'],
                  experimental_run_tf_function=False)

    model.fit(db_train, epochs=epochs,validation_data = db_test)

    model.evaluate(db_test)



if __name__ == '__main__':
    main()

1.出现问题代码处为model.compile**

    model.compile(optimizer = optimizers.Adam(lr = 1e-3),
                  loss = tf.losses.BinaryCrossentropy(),
                  metrics = ['accuracy'],
                  experimental_run_tf_function=False   # 增加这一行即可解决)

2.出现问题的原因及解决

首先,大家应该都知道,dropout参数控制层与层之间连接断掉的概率,当代码中存在dropout参数时,训练时起作用(dropout应该有),测试时不起作用。
这部分代码出现问题,我猜测是在验证时,dropout起了作用出现了错误,所以要如上设置

你也可以参考这个网址

未经允许不得转载:作者:1147-柳同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《bug6-_SymbolicException: Inputs to eager execution function cannot be Keras symbolic》 发布于2020-10-14

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录