首页 > 解决方案 > 神经网络输出问题

问题描述

我用 tensorflow 构建了一个神经网络,代码如下:

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

            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 = 50,
                                      kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                      activation = tf.nn.elu)


            self.output = tf.layers.dense(inputs = self.fc, 
                                        units = self.action_size,
                                        kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                        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)

但是我的输出有问题,

输出通常应该与“action_size”的大小相同,并且 action_size 的值为 3,但我得到的输出类似于 [[5][3]] 而不仅仅是 [[3]],我真的不明白为什么。 ..

这个网络有 2 个密集层,一个有 50 个感知器,另一个有 3 个感知器(= action_size)。

state_size 是格式:[[9][5]]

如果有人知道为什么我的输出是二维的,我将非常感激

标签: tensorflow

解决方案


你的self.inputs_占位符有 shape (5, 9)。您在具有形状matmul(self.inputs_, fc1.w)的密集层中执行操作)并产生形状。然后,您应用另一个具有 shape 的密集层,从而产生输出 shape 。fc1(9, 50(5, 50)(50, 3)(5, 3)

相同的示意图:

  1. matmul(shape(5, 9), shape(9, 50)) ---> shape(5, 50) # output of 1st dense layer

  2. matmul(shape(5, 50), shape(50, 3)) ---> shape(5, 3) # output of 2nd dense layer

通常,输入占位符的第一个维度代表批量大小,第二个维度是输入特征向量的维度。因此,对于批次中的每个样本(在您的情况下,批次大小为 5),您将获得输出形状 3。

要获得概率,请使用以下命令:

import tensorflow as tf
import numpy as np

inputs_ = tf.placeholder(tf.float32, shape=(None, 9))
actions_ = tf.placeholder(tf.float32, shape=(None, 3))

fc = tf.layers.dense(inputs=inputs_, units=2)
output = tf.layers.dense(inputs=fc, units=3)
reduced = tf.reduce_mean(output, axis=0)
probs = tf.nn.softmax(reduced) # <--probabilities

inputs_vals = np.ones((5, 9))
actions_vals = np.ones((1, 3))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print(probs.eval({inputs_:inputs_vals,
                      actions_:actions_vals}))
    # [0.01858923 0.01566187 0.9657489 ]

推荐阅读