张量流中的线性模型

linear model in tensor flow

我试图在 Tensorflow 中生成一个简单的线性模型。这是代码...

N        = 400
features = 100
nSteps   = 1000

data = (np.random.randn(N, features), np.random.randint(0, 2, N))

W = tf.placeholder(tf.float32, shape=(features,1), name='W')
b = tf.placeholder(tf.float32, shape=(features,1), name='b')
d = tf.constant(data[0], dtype=tf.float32)

result = tf.add( tf.matmul(d, W), b)

事实证明b的尺寸可能存在一些问题,但出于某种原因,据我所知,它们都可以...

不确定为什么会抛出错误。有人可以帮忙吗?

注:

result = tf.matmul(d, W)

没关系。

我检查了结果的形状,与b的形状相同。不确定可能是什么问题。

在线性模型中(即输出层中的一个单元),b 应该是标量。

从数学上讲,对于单个观察,您有:result = WX + b,其中维度 W [1 x 特征],X [特征 x 1]。然后,WX 是标量。因此 b 应该是一个标量。

因此您应该将 b 更改为以下内容,以获得正确的线性模型并计算尺寸:

b = tf.placeholder(tf.float32, shape=(1,1), name='b')