2017-09-26 23 views
1

很抱歉,如果標題是不是很清楚......我想在下面的問題Tensorflow來解決的「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(請參閱上面的追溯):試圖使用未初始化的值...」

我猜這與我如何構建B並傳遞它以及成本函數,但對於Tensorflow來說,我太陌生了,看看我做錯了什麼。

任何幫助?

+0

我可以爲你解決這個問題。現在處理它。 – Aaron

回答

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])