tensorflow softmax 总是 return 1
tensorflow softmax always return 1
import tensorflow as tf
import numpy as np
a = np.array([[0,0,1], [1,1,1], [1000,200,300], [-3000,-0.2,0]])
k = tf.placeholder(tf.float32, [None,3])
w = tf.Variable(tf.random_normal([3,1]))
b = tf.Variable(tf.ones([1,1]))
model = tf.nn.softmax(tf.matmul(k,w)+b)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(model, {k:a}))
输出:
[[ 1.]
[ 1.]
[ 1.]
[ 1.]]
我不明白为什么我总是收到 1,无论输入如何,无论是否包含偏差 B...有什么想法吗?将不胜感激。
在我看来,您需要向 softmax
提供 dim
参数作为 0
,以便它计算 列 softmax 而不是 row softmax(默认 dim=-1);因为对于每一行你只有一个元素 (w.shape[1] == 1),无论值是什么,softmax
给出 1
:
model = tf.nn.softmax(tf.matmul(k,w) + b, dim=0)
import tensorflow as tf
import numpy as np
a = np.array([[0,0,1], [1,1,1], [1000,200,300], [-3000,-0.2,0]])
k = tf.placeholder(tf.float32, [None,3])
w = tf.Variable(tf.random_normal([3,1]))
b = tf.Variable(tf.ones([1,1]))
model = tf.nn.softmax(tf.matmul(k,w) + b, dim=0)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(model, {k:a}))
[[ 0.00000000e+00]
[ 0.00000000e+00]
[ 1.00000000e+00]
[ 1.61103498e-12]]
将您的代码分成两步:
mul_tensor = tf.matmul(k,w)+b
model = tf.nn.softmax(mul_tensor)
with tf.Session() as sess:
tf.global_variables_initializer().run()
#print(sess.run(model, {k:a}))
print(sess.run(mul_tensor, {k:a}))
您将得到
的答案
array([[ 0.69425076],
[ 1.7690506 ],
[ 41.94503021],
[ 309.35256958]], dtype=float32)
所以你在 1 * 1 个条目上应用 softmax,这将 return 你总是 1。
改变model = tf.nn.softmax(tf.matmul(k,w)+b)
至 model = tf.nn.softmax(tf.reshape(tf.matmul(k,w)+b, [-1]))
tf.matmul(k,w)+b
的输出是一个二维数组。在你的例子中,[4,1]。
但是 tf.nn.softmax()
中的 reduce_sum
默认应用于最后一个轴。你总是得到 1 b/c 每行只有 1 个元素。 tf.reshape
将 tf.matmul(k,w)+b
的大小更改为 [4]。
import tensorflow as tf
import numpy as np
a = np.array([[0,0,1], [1,1,1], [1000,200,300], [-3000,-0.2,0]])
k = tf.placeholder(tf.float32, [None,3])
w = tf.Variable(tf.random_normal([3,1]))
b = tf.Variable(tf.ones([1,1]))
model = tf.nn.softmax(tf.matmul(k,w)+b)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(model, {k:a}))
输出:
[[ 1.]
[ 1.]
[ 1.]
[ 1.]]
我不明白为什么我总是收到 1,无论输入如何,无论是否包含偏差 B...有什么想法吗?将不胜感激。
在我看来,您需要向 softmax
提供 dim
参数作为 0
,以便它计算 列 softmax 而不是 row softmax(默认 dim=-1);因为对于每一行你只有一个元素 (w.shape[1] == 1),无论值是什么,softmax
给出 1
:
model = tf.nn.softmax(tf.matmul(k,w) + b, dim=0)
import tensorflow as tf
import numpy as np
a = np.array([[0,0,1], [1,1,1], [1000,200,300], [-3000,-0.2,0]])
k = tf.placeholder(tf.float32, [None,3])
w = tf.Variable(tf.random_normal([3,1]))
b = tf.Variable(tf.ones([1,1]))
model = tf.nn.softmax(tf.matmul(k,w) + b, dim=0)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(model, {k:a}))
[[ 0.00000000e+00]
[ 0.00000000e+00]
[ 1.00000000e+00]
[ 1.61103498e-12]]
将您的代码分成两步:
mul_tensor = tf.matmul(k,w)+b
model = tf.nn.softmax(mul_tensor)
with tf.Session() as sess:
tf.global_variables_initializer().run()
#print(sess.run(model, {k:a}))
print(sess.run(mul_tensor, {k:a}))
您将得到
的答案array([[ 0.69425076],
[ 1.7690506 ],
[ 41.94503021],
[ 309.35256958]], dtype=float32)
所以你在 1 * 1 个条目上应用 softmax,这将 return 你总是 1。
改变model = tf.nn.softmax(tf.matmul(k,w)+b)
至 model = tf.nn.softmax(tf.reshape(tf.matmul(k,w)+b, [-1]))
tf.matmul(k,w)+b
的输出是一个二维数组。在你的例子中,[4,1]。
但是 tf.nn.softmax()
中的 reduce_sum
默认应用于最后一个轴。你总是得到 1 b/c 每行只有 1 个元素。 tf.reshape
将 tf.matmul(k,w)+b
的大小更改为 [4]。