我的神经网络完全连接的内核初始化错误

时间:2019-04-12 08:11:51

标签: python tensorflow initialization sumo

我正在尝试将神经网络与SUMO交通仿真器和TracI(SUMO工具)配合使用,但是我的网络初始化出现错误

我当前添加了:

with tf.Session() as sess:
        # Initialize the variables or load a network
        sess.run(tf.global_variables_initializer())

使用此行创建网络后:

DQNetwork = DQNetwork(state_size, action_size, learning_rate)

这是我的DQNetwork的代码:

class DQNetwork:
    def __init__(self, state_size, action_size, learning_rate, name='DQNetwork'):
        self.state_size = state_size
        self.action_size = action_size
        self.learning_rate = learning_rate

        with tf.variable_scope(name):
            # We create the placeholders
            # *state_size means that we take each elements of state_size in tuple hence is like if we wrote
            # [None, 84, 84, 4]
            self.inputs_ = tf.placeholder(tf.float32, shape=[state_size[1], state_size[0]], name="inputs")
            self.actions_ = tf.placeholder(tf.float32, [None, self.action_size], name="actions_")

            # Remember that target_Q is the R(s,a) + ymax Qhat(s', a')
            self.target_Q = tf.placeholder(tf.float32, [None], name="target")



            self.fc = tf.layers.dense(inputs = self.inputs_,
                                  units = 100,
                                  activation = tf.nn.elu,
                                  kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                  name="fc1")

            self.output = tf.layers.dense(inputs = self.fc, 
                                        kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                        units = self.action_size, 
                                        activation=None)



            # Q is our predicted Q value.
            self.Q = tf.reduce_sum(tf.multiply(self.output, self.actions_))

            # The loss is the difference between our predicted Q_values and the Q_target
            # Sum(Qtarget - Q)^2
            self.loss = tf.reduce_mean(tf.square(self.target_Q - self.Q))

            self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)

出现问题的错误是:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
         [[{{node DQNetwork/fc1/kernel/read}}]]

这里是错误日志:(很抱歉,我提供了警告)

WARNING:tensorflow:From C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.dense instead.
WARNING:tensorflow:From C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-04-12 10:08:47.635411: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
$$$ TracI tools loaded $$$
Loading configuration... done.
Step #45.00Traceback (most recent call last):icles TOT 26 ACT 26 BUF 84)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
    return fn(*args)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
         [[{{node DQNetwork/fc1/kernel/read}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\Train.py", line 350, in <module>
    train()
  File ".\Train.py", line 301, in train
    action, explore_probability = predict_action(explore_start, explore_stop, decay_rate, decay_step, state)
  File ".\Train.py", line 155, in predict_action
    Qs = sess.run(DQNetwork.output, feed_dict = {DQNetwork.inputs_: state})
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
    run_metadata)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value DQNetwork/fc1/kernel
         [[node DQNetwork/fc1/kernel/read (defined at C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33) ]]

Caused by op 'DQNetwork/fc1/kernel/read', defined at:
  File ".\Train.py", line 111, in <module>
    DQNetwork = DQNetwork(state_size, action_size, learning_rate)
  File "C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py", line 33, in __init__
    name="fc1")
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\core.py", line 188, in dense
    return layer.apply(inputs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1227, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\base.py", line 530, in __call__
    outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 538, in __call__
    self._maybe_build(inputs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1603, in _maybe_build
    self.build(input_shapes)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\layers\core.py", line 949, in build
    trainable=True)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\layers\base.py", line 435, in add_weight
    getter=vs.get_variable)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 349, in add_weight
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 607, in _add_variable_with_custom_getter
    **kwargs_for_getter)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1479, in get_variable
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1220, in get_variable
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 547, in get_variable
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 499, in _true_getter
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 911, in _get_single_variable
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 213, in __call__
    return cls._variable_v1_call(*args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 176, in _variable_v1_call
    aggregation=aggregation)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 155, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 2495, in default_variable_creator
    expected_shape=expected_shape, import_scope=import_scope)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 217, in __call__
    return super(VariableMetaclass, cls).__call__(*args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 1395, in __init__
    constraint=constraint)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\variables.py", line 1557, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\array_ops.py", line 81, in identity
    ret = gen_array_ops.identity(input, name=name)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 4537, in identity
    "Identity", input=input, name=name)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\ops.py", line 3300, in create_op
    op_def=op_def)
  File "C:\Users\Odeven poste 1\Documents\[Python-3.6.8\python-3.6.8.amd64\lib\site-packages\tensorflow\python\framework\ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value DQNetwork/fc1/kernel
         [[node DQNetwork/fc1/kernel/read (defined at C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL\DQNet.py:33) ]]

Error: tcpip::Socket::recvAndCheck @ recv: peer shutdown
Quitting (on error).
PS C:\Users\Odeven poste 1\Documents\WorkSpace\V1.0 SumoDQL>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,就像我说的那样:

with tf.Session() as sess:
    # Initialize the variables or load a network
    sess.run(tf.global_variables_initializer())

我还有一个功能,可以确定给定输入集的网络Q值:

with tf.Session() as sess:

我刚刚将其删除,现在一切正常。