【发布时间】:2016-02-24 20:03:31
【问题描述】:
假设我想在 TensorFlow 中使用封闭式解决方案计算最小二乘系数。通常,我会这样做,
beta_hat = tf.matmul(
tf.matmul(tf.matrix_inverse(tf.matmul(tf.transpose(X), X)), tf.transpose(X)), y
)
其中X 和y 分别是对应于协变量和目标变量的TensorFlow 占位符。
如果我想执行预测,我会做类似的事情,
y_pred = tf.matmul(X, beta_hat)
如果我要执行,
sess.run(y_pred, feed_dict={X: data_X})
我当然会收到一个错误,即我没有为占位符y 提供必要的值。我希望在计算 beta_hat 后灵活地将其视为常量(这样我就不需要为新的协变量矩阵定义新的占位符来进行预测)。实现此目的的一种方法是,
# Make it constant.
beta_hat = sess.run(beta_hat, feed_dict={X: data_X, y: data_y})
y_pred = tf.matmul(X, beta_hat)
我想知道是否有更优雅的方式将张量视为常量,这样我既不需要执行会话并获取常量,也不需要为传入数据创建单独的占位符以用于预测。
这里有一些示例代码来演示我所描述的情况。
import numpy as np
import tensorflow as tf
n, k = 100, 5
X = tf.placeholder(dtype=tf.float32, shape=[None, k])
y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
beta = np.random.normal(size=(k, ))
data_X = np.random.normal(size=(n, k))
data_y = data_X.dot(beta)
data_y += np.random.normal(size=data_y.shape) / 3.0
data_y = np.atleast_2d(data_y).T
# Convert to 32-bit precision.
data_X, data_y = np.float32(data_X), np.float32(data_y)
# Compute the least squares solution.
beta_hat = tf.matmul(
tf.matmul(tf.matrix_inverse(tf.matmul(tf.transpose(X), X)),
tf.transpose(X)), y
)
# Launch the graph
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print "True beta: {}".format(beta)
print "Est. beta: {}".format(
sess.run(beta_hat, feed_dict={X: data_X, y: data_y}).ravel()
)
# # This would error.
# y_pred = tf.matmul(X, beta_hat)
# print "Predictions:"
# print sess.run(y_pred, feed_dict={X: data_X})
# Make it constant.
beta_hat = sess.run(beta_hat, feed_dict={X: data_X, y: data_y})
# This will no longer error.
y_pred = tf.matmul(X, beta_hat)
print "Predictions:"
print sess.run(y_pred, feed_dict={X: data_X})
【问题讨论】:
标签: python numpy constants linear-regression tensorflow