如何训练具有张量流的简单非线性回归模型?

时间:2015-12-16 12:20:20

标签: python regression tensorflow

我已经看过this example for linear regression,我想训练一个模型

enter image description here

其中enter image description here

我尝试了什么

#!/usr/bin/env python

"""Example for learning a regression."""


import tensorflow as tf
import numpy

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50

# Generate training data
train_X = []
train_Y = []
f = lambda x: x**2
for x in range(-20, 20):
    train_X.append(float(x))
    train_Y.append(f(x))
train_X = numpy.asarray(train_X)
train_Y = numpy.asarray(train_Y)
n_samples = train_X.shape[0]

# Graph input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Create Model
W1 = tf.Variable(tf.truncated_normal([1, 10], stddev=0.1), name="weight")
b1 = tf.Variable(tf.constant(0.1, shape=[1, 10]), name="bias")
mul = X * W1
h1 = tf.nn.sigmoid(mul) + b1
W2 = tf.Variable(tf.truncated_normal([10, 1], stddev=0.1), name="weight")
b2 = tf.Variable(tf.constant(0.1, shape=[1]), name="bias")
activation = tf.nn.sigmoid(tf.matmul(h1, W2) + b2)

# Minimize the squared errors
l2_loss = tf.reduce_sum(tf.pow(activation-Y, 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(l2_loss)

# Initializing the variables
init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per epoch step
        if epoch % display_step == 0:
            cost = sess.run(l2_loss, feed_dict={X: train_X, Y: train_Y})
            print("Epoch: {:04d}, cost={:.9f}".format((epoch+1), cost),
                  "W=", sess.run(W1))  # "b=", sess.run(b1)

    print("Optimization Finished!")
    print("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}),
          "W1=", sess.run(W1), )  # "b2=", sess.run(b2)

当我执行它时,我得到:

$ python nnetstest.py
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 2
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 2
W tensorflow/core/common_runtime/executor.cc:1027] 0x314df50 Compute status: Invalid argument: Incompatible shapes: [40] vs. [1,10]
     [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, weight)]]
Traceback (most recent call last):
  File "nnetstest.py", line 56, in <module>
    cost = sess.run(l2_loss, feed_dict={X: train_X, Y: train_Y})
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 345, in run
    results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 419, in _do_run
    e.code)
tensorflow.python.framework.errors.InvalidArgumentError: Incompatible shapes: [40] vs. [1,10]
     [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, weight)]]
Caused by op u'mul', defined at:
  File "nnetstest.py", line 32, in <module>
    mul = X * W1
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 403, in binary_op_wrapper
    return func(x, y, name=name)
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 728, in mul
    return _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
    op_def=op_def)
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/moose/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 988, in __init__
    self._traceback = _extract_stack()

我在输入数据中尝试了一些细微的变化,但我无法让它发挥作用。

如何使用Google TensorFlow训练这样一个简单的非线性回归模型?

2 个答案:

答案 0 :(得分:8)

InvalidArgumentError归因于您正在投放的值(train_Xtrain_Y)没有必要的形状乘以W1

这里有一些问题:

  1. 语句mul = X * W1应为mul = tf.matmul(X, W1),因为*计算元素乘法,而不是您的等式指定的。

  2. 输入数据X应为单列矩阵。要处理标量和矢量数据 - 就像您在Feed调用中一样,您可以按如下方式重新整形:

    X = tf.placeholder(tf.float32)
    reshaped_X = tf.reshape(X, [-1, 1])
    # ...
    mul = reshaped_X * W1
    
  3. 当您获取最终费用时,sess.run的第一个参数应为l2_loss(而不是cost):

    print("cost=", sess.run(l2_loss, feed_dict={X: train_X, Y: train_Y}),
          "W1=", sess.run(W1))
    

答案 1 :(得分:0)

数据的形状(40维)与您乘以的矩阵的形状(10维)不兼容。尝试更改任一尺寸。