简单的张量流线性模型

时间:2016-09-24 08:46:55

标签: python python-3.x numpy tensorflow linear-regression

这里train_X(shape = m,64)和train_Y(m,1)是numpy数组,其中m是我的训练样本数。

print('train and test array computed')
print(train_X[:2])
print(train_Y[:2])

这些行的输出是

train and test array computed
[[  8.10590000e+01   8.91460000e+01   1.00000000e+02   1.00000000e+02
    0.00000000e+00   5.26000000e-01   5.80000000e-01   5.80000000e-01
    5.80000000e-01   5.80000000e-01   8.00000000e-01   6.66670000e+01
    6.36160000e+01   8.36000000e-01   1.17300000e+00   5.80000000e-01
    6.48860000e+01   5.80000000e-01   1.73640000e+01  -2.07250000e+01
    5.81000000e-01   0.00000000e+00   5.80000000e-01   5.80000000e-01
    7.00000000e-03   5.80000000e-01  -5.44000000e-01  -2.36000000e-01
    5.80000000e-01   5.81000000e-01   5.81000000e-01   5.80000000e-01
    5.80000000e-01   5.80000000e-01   5.80000000e-01   5.80000000e-01
    0.00000000e+00   4.00000000e-01   2.10000000e-02   1.00000000e+00
    1.00021000e+02   8.18080000e+01   5.71000000e-01   5.48000000e-01
    6.14000000e-01   5.80000000e-01   7.62000000e-01   0.00000000e+00
    1.00000000e+02   1.00000000e+02   0.00000000e+00   1.00000000e+02
    1.00000000e+02   1.16100000e+00   5.80000000e-01   6.56000000e-01
    5.80000000e-01   5.80000000e-01   5.81000000e-01   5.80000000e-01
    1.00000000e+02   5.80000000e-01   0.00000000e+00   5.80000000e-01]
 [  5.12680000e+01   4.87480000e+01   1.00000000e+02   1.00000000e+02
    0.00000000e+00   4.18000000e-01   4.44000000e-01   4.44000000e-01
    4.44000000e-01   4.44000000e-01   5.00000000e-01   6.66670000e+01
    3.83570000e+01   9.03000000e-01   1.10000000e+00   4.44000000e-01
    5.63070000e+01   4.44000000e-01   1.85220000e+01   1.94233000e+02
    4.44000000e-01   0.00000000e+00   4.44000000e-01   4.44000000e-01
    1.00000000e-03   4.44000000e-01  -8.12000000e-01  -3.53000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    0.00000000e+00   5.00000000e-01   2.00000000e-03   1.00000000e+00
    1.00002000e+02   6.91780000e+01   4.42000000e-01   4.29000000e-01
    4.59000000e-01   4.44000000e-01   6.66000000e-01   0.00000000e+00
    5.00000000e+01   5.00000000e+01   0.00000000e+00   5.00000000e+01
    5.00000000e+01   8.88000000e-01   4.44000000e-01   4.75000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    5.00000000e+01   4.44000000e-01  -5.00000000e+01   4.44000000e-01]]
[ 0.44378  0.6821 ]

以下是我的计划的相关部分。

rng = np.random

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50

# Training Data
n_samples = train_X.shape[0]
n_features = train_X.shape[1]

# tf Graph Input
X = tf.placeholder(tf.float32,[None,n_features])
Y = tf.placeholder(tf.float32,[None,1])

# Set model weights
W = tf.Variable(tf.random_normal([n_features,1],mean=0,stddev=(np.sqrt(6/n_features+
                                                         1+1))), name="weight")
b = tf.Variable(tf.random_normal([1,1],
                                    mean=0,
                                    stddev=(np.sqrt(6/n_features+1+1)),
                                    name="bias"))

# Construct a linear model
pred = tf.add(tf.mul(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.initialize_all_variables()

print('starting the session')

# 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+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    # Graphic display
    # plt.plot(train_X, train_Y, 'ro', label='Original data')
    # plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    # plt.legend()
    # plt.show()

    # Testing example, as requested (Issue #2)

    print("Testing... (Mean square loss Comparison)")
    testing_cost = sess.run(
        tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]),
        feed_dict={X: test_X, Y: test_Y})  # same function as cost above
    print("Testing cost=", testing_cost)
    print("Absolute mean square loss difference:", abs(
        training_cost - testing_cost))

    # plt.plot(test_X, test_Y, 'bo', label='Testing data')
    # plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    # plt.legend()
    # plt.show()

我很难让它长时间工作,但它从未奏效。我给了我错误。

Traceback (most recent call last):
  File "q.py", line 158, in <module>
    sess.run(optimizer, feed_dict={X: x, Y: y})
  File "/home/tensorflow/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 382, in run
    run_metadata_ptr)
  File "/home/tensorflow/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 640, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (64,) for Tensor 'Placeholder:0', which has shape '(?, 64)'

问题在于 sess.run(optimizer,feed_dict = {X:x,Y:y})

1 个答案:

答案 0 :(得分:1)

我认为错误是你在zip(train_X,train_Y)中拉链(x,y):所以这将给出一个x和一个y的例子。

你反而希望直接在trainX和trainY中输入:

feed_dict={X: train_X, Y:train_Y}

您可以通过运行

来检查是否属于这种情况
for (x, y) in zip(train_X, train_Y):
    print(x.shape,y.shape) # I guess this will be 64 and 1 for x and y resp.
    sess.run(optimizer, feed_dict={X: x, Y: y})

你希望它有X =(#,64)

的形状