简体   繁体   English

张量流中的线性模型

[英]linear model in tensor flow

I was trying to generate a simple linear model in Tensorflow. 我试图在Tensorflow中生成一个简单的线性模型。 Here is the code ... 这是代码......

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)

It turns out that there might be some problem with the dimensions of b , but for some reason as far as I can say, they are all ok ... 事实证明, b的尺寸可能存在一些问题,但出于某些原因,据我所知,它们都可以......

Not sure why this is throwing an error. 不知道为什么这会引发错误。 Can someone please help? 有人可以帮忙吗?

Note: 注意:

result = tf.matmul(d, W)

This is ok. 还行吧。

I have checked the shape of the result, and is the same as that of b . 我检查了结果的形状,与b的相同。 Not really sure what might be the problem. 不确定可能是什么问题。

In a linear model (ie one unit in the output layer), b should be a scalar. 在线性模型中(即输出层中的一个单元), b应该是标量。

Mathematically, for a single observation, you have: result = WX + b , where dimensions W [1 x features], X [features x 1]. 在数学上,对于单个观察,您有: result = WX + b ,其中维度W [1 x特征], X [特征x 1]。 Then, WX is scalar. 然后, WX是标量。 Thus b should be a scalar. 因此b应该是标量。

So you should change b to the following, to get the correct linear model and make the dimensions work out: 因此,您应该将b更改为以下内容,以获得正确的线性模型并使尺寸变为现实:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM