gpt4 book ai didi

python - tensorflow 占位符形状 [None] [None,1] 区别

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:09 25 4
gpt4 key购买 nike

我是 tensorflow 的新手。我用一个涉及的部分试验了 DQN 算法

a = tf.placeholder(tf.int32, shape = [None],name='A')
q = tf.reduce_sum(critic_q * tf.one_hot(a,n_outputs),axis=1,keepdims=True,name='Q')#Q value for chosen action

y = tf.placeholder(tf.float32, shape = [None],name='Y')

learning_rate = 1e-4
cost = tf.reduce_mean(tf.square(y-q))#mean squared error
global_step = tf.Variable(0,trainable=False,name='global_step')
optimizer = tf.train.AdamOptimizer(learning_rate)
training_op = optimizer.minimize(cost,global_step=global_step)

并使用 y_batch=np.zeros(nbatch) 初始化输入 y。网络几乎没有训练过。

然后,我转而将 y 定义为

y = tf.placeholder(tf.float32, shape = [None,1],name='Y')

并使用 y_batch=np.zeros(nbatch).reshape(-1,1) 初始化输入,效果很好。

第一次实现发生了什么?

最佳答案

每个张量都有一个秩(维度数)和一组维度。

形状为 [1] 的占位符是等级为 1 且位置 0 的维度为 1 的占位符。

形状为 [None, 1] 的占位符是等级为 2 的占位符,因此它有 2 个维度。第一个维度(索引 0)的大小未知(它将在运行时解析),而第二个维度(索引 1)的大小已知为 1。

为了兼容,张量必须具有相同的秩和维度。

您可以在此处阅读有关张量形状的更完整评估:https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic

关于python - tensorflow 占位符形状 [None] [None,1] 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com