tensorflow - 神经网络输出问题
问题描述
我用 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]]
如果有人知道为什么我的输出是二维的,我将非常感激
解决方案
你的self.inputs_
占位符有 shape (5, 9)
。您在具有形状matmul(self.inputs_, fc1.w)
的密集层中执行操作)并产生形状。然后,您应用另一个具有 shape 的密集层,从而产生输出 shape 。fc1
(9, 50
(5, 50)
(50, 3)
(5, 3)
相同的示意图:
matmul(shape(5, 9), shape(9, 50)) ---> shape(5, 50)
# output of 1st dense layer
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 ]
推荐阅读
- azure - 如何在本地开发中调用 NoAutomaticTrigger 函数?
- c - 我是否以正确的方式用 C 语言编写了选择排序算法?
- windows - 批处理文件错误“/100 在这个时候是意外的”
- css - 如何在居中视频旁边的剩余空间中居中按钮
- mongodb - 如何从 mongo 数据库中仅导出少数集合,而不是全部?
- python - sqlite3.InterfaceError:错误绑定参数 4 - 可能不支持的类型
- python - 在 A*(A 星)算法中实现开放集时,使用 min() 获取最小值或对数组进行排序,然后在 Python 中弹出第一个值?
- java - 如何将链接列表分成两个其他列表,一个具有奇数索引,另一个具有偶数索引但以递归方式
- javascript - 我疯了,试图弄清楚如何将 JSON 响应作为字符串传递给 dynamodb 调用
- python - “静音”命令根本不起作用(Discord.py,重写)