我见过这个线性回归的例子和我想训练一个模特

哪里

我试过的
#!/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来训练这样简单的非线性回归模型呢?
发布于 2015-12-16 16:04:16
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 * W1sess.run的第一个参数应该是l2_loss (而不是cost):
打印(“cost=”,sess.run(l2_loss,feed_dict={X: train_X,Y: train_Y}),"W1=",sess.run(W1))发布于 2015-12-16 15:35:30
数据的形状(40维)与将其乘以(10维)的矩阵形状不兼容。试着改变这两种尺寸。
https://stackoverflow.com/questions/34311893
复制相似问题