【问题标题】:Initializing a bias term in my nonlinear regression model using TensorFlow使用 TensorFlow 在我的非线性回归模型中初始化偏差项
【发布时间】:2017-07-26 15:22:14
【问题描述】:

我正在尝试建立一个基本的非线性回归模型来预测 FTSE350 公司的回报指数。

我不确定我的偏差项在维度方面应该是什么样的,以及我是否在计算方法中正确使用它:

w1 = tf.Variable(tf.truncated_normal([4, 10], mean=0.0, stddev=1.0, dtype=tf.float64))
b1 = tf.Variable(tf.constant(0.1, shape=[4,10], dtype = tf.float64))

w2 = tf.Variable(tf.truncated_normal([10, 1], mean=0.0, stddev=1.0, dtype=tf.float64))
b2 = tf.Variable(tf.constant(0.1, shape=[1], dtype = tf.float64))

def calculations(x, y):
    w1d = tf.matmul(x, w1)
    h1 = (tf.nn.sigmoid(tf.add(w1d, b1)))
    h1w2 = tf.matmul(h1, w2)
    activation = tf.add(tf.nn.sigmoid(tf.matmul(h1, w2)), b2)
    error = tf.reduce_sum(tf.pow(activation - y,2))/(len(x))

    return [ activation, error ]        

我最初的想法是它应该和我的重量一样大,但我收到了这个错误:

ValueError: Dimensions must be equal, but are 251 and 4 for 'Add' (op: 'Add') with input shapes: [251,10], [4,10]

我尝试过不同的想法,但似乎没有成功。

(我的输入数据有4个特征)

我尝试的网络结构是输入层有 4 个神经元,隐藏层有 10 个神经元,稍后输出有 1 个神经元,但我觉得我的权重层也可能混淆了维度?

【问题讨论】:

    标签: python python-2.7 tensorflow neural-network non-linear-regression


    【解决方案1】:

    当您为前馈全连接神经网络(如您的示例中)构建层时,偏差的形状应等于相应层中的节点数。因此,在您的情况下,由于您的权重矩阵的形状为(4, 10),因此您在该层中有 10 个节点,您应该使用:

    b1 = tf.Variable(tf.constant(0.1, shape=[10], type = tf.float64))
    

    这样做的原因是当您执行w1d = tf.matmul(x, w1) 时,您实际上得到了一个形状为(batch_size, 10) 的矩阵(如果batch_size 是输入矩阵中的行数)。这是因为您将矩阵乘以 (batch_size, 4) 矩阵和 (4, 10) 权重矩阵。然后,您在w1d 的每一列中添加一个偏差,它可以表示为一个 10 维向量,如果您制作 b1 [10] 的形状,您将得到它。

    之后没有非线性(sigmoid),这称为仿射变换,您可以在此处阅读更多信息:https://en.wikipedia.org/wiki/Affine_transformation

    另一个很棒的资源是斯坦福深度学习教程,它很好地解释了这些前馈模型如何在这里工作: http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/.

    希望有所帮助!

    【讨论】:

      【解决方案2】:

      我认为你的 b1 应该只有 10 维并且你的代码应该可以运行

      因为 4 是特征的数量,而 10 是第一层中神经元的数量(我认为是神经网络......)

      那么你必须添加dimention = 10的偏差

      此外,您可能会将偏差视为添加了常数值 = 1 的额外特征。

      如果您有时间,请查看此 pdf 文件:https://cs.stanford.edu/~quocle/tutorial1.pdf

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-04
        • 2019-09-29
        • 2010-12-20
        • 2019-10-09
        • 2016-09-06
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多