如何使用 tensorflow 训练一个简单的非线性回归模型?
Posted
技术标签:
【中文标题】如何使用 tensorflow 训练一个简单的非线性回归模型?【英文标题】:How can I train a simple, non-linear regression model with tensor flow? 【发布时间】:2016-03-22 13:57:42 【问题描述】:我见过this example for linear regression,我想训练一个模型
在哪里
我尝试过的
#!/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 训练如此简单的非线性回归模型?
【问题讨论】:
【参考方案1】:InvalidArgumentError
是由于您输入的值(train_X
和 train_Y
)没有乘以 W1
的必要形状。
这里有几个问题:
语句mul = X * W1
应该是mul = tf.matmul(X, W1)
,因为*
计算的是元素乘法,这不是您的方程所指定的。
输入数据X
应该是一个单列矩阵。要处理标量和矢量数据 - 正如您在提要调用中所做的那样,您可以按如下方式对其进行整形:
X = tf.placeholder(tf.float32)
reshaped_X = tf.reshape(X, [-1, 1])
# ...
mul = reshaped_X * W1
当您获取最终成本时,sess.run
的第一个参数应该是 l2_loss
(而不是 cost
):
print("cost=", sess.run(l2_loss, feed_dict=X: train_X, Y: train_Y),
"W1=", sess.run(W1))
【讨论】:
【参考方案2】:数据的形状(40 维)与矩阵的形状(10 维)不兼容。尝试更改任一大小。
【讨论】:
以上是关于如何使用 tensorflow 训练一个简单的非线性回归模型?的主要内容,如果未能解决你的问题,请参考以下文章