很抱歉,如果标题不是很清楚......我试图解决" w"的问题。在Tensorflow的以下问题中:
Y = X*B(w) + e
其中Y是22x5矩阵,X是22x3矩阵,B(w)是具有以下结构的3 * 5矩阵:
B = [[1, 1, 1, 1, 1],
[exp(-3w), exp(-6w), exp(-12w), exp(-24w), exp(-36w)],
[3*exp(-3w), 6*exp(-6w), 12*exp(-12w), 24*exp(-24w), 36*exp(-36w)]]
这是我的代码:
# Parameters
learning_rate = 0.01
display_step = 50
tolerance = 0.0000000000000001
# Training Data
Y_T = df.values
X_T = factors.values
X = tf.placeholder("float32", shape = (22, 3))
Y = tf.placeholder("float32", shape = (22, 5))
w = tf.Variable(1.0, name="w")
def slope_loading(q):
return tf.exp(tf.multiply(tf.negative(q),w))
def curve_loading(q):
return tf.multiply(w,tf.exp(tf.multiply(tf.negative(q),w)))
B = tf.Variable([[1.0, 1.0, 1.0, 1.0, 1.0],
[slope_loading(float(x)) for x in [3, 6, 12, 24, 36]],
[curve_loading(float(x)) for x in [3, 6, 12, 24, 36]]])
pred = tf.matmul(X,B)
cost = tf.matmul(tf.transpose(Y-pred), (Y-pred))/22
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
# Launch the graph
with tf.Session() as sess:
# Set initial values for weights
sess.run(init)
# Set initial values for the error tolerance
tol = abs(sess.run(cost, feed_dict={X: X_T, Y: Y_T})[0][0])
iteration = 0
while tol > tolerance:
c_old = sess.run(cost, feed_dict={X: X_T, Y: Y_T})[0][0]
sess.run(optimizer, feed_dict={X: X_T, Y: Y_T})
c_new = sess.run(cost, feed_dict={X: X_T, Y: Y_T})[0][0]
tol = abs(c_new - c_old)
iteration = iteration + 1
if iteration % display_step == 0:
print("Iteration= ", iteration, "Gain= ", tol)
training_cost = sess.run(cost, feed_dict={X: X_T, Y: Y_T})
但是我收到错误" FailedPreconditionError(请参阅上面的回溯):尝试使用未初始化的值w ..."
我猜这与我如何构建B并将其传递给成本函数有关,但我对Tensorflow来说太新了,看看我是什么做错了。
任何帮助?
答案 0 :(得分:1)
您无法使用变量来定义另一个变量的初始值。构建B的更好方法是这样的
ones = tf.ones(5)
vals = tf.constant([3.0, 6.0, 12.0, 24.0, 36.0])
slopes = slope_loading(vals)
curves = curve_loading(vals)
B = tf.stack([ones, slopes, curves])