measure_theory
measure_theory

Reputation: 874

Tensorflow: embedding Variable into matrix and solving

Sorry if the title isn't very clear... I'm trying to solve for the value of "w" in the following problem with Tensorflow:

Y = X*B(w) + e

where Y is a 22x5 matrix, X is a 22x3 matrix, and B(w) is a 3*5 matrix with the following structure:

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

Here's my code:

# 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})

But i'm getting the error "FailedPreconditionError (see above for traceback): Attempting to use uninitialized value w..."

I'm guessing this has to do with the how I'm constructing B and passing it along to the cost function, but I'm too new to Tensorflow to see what I'm doing wrong.

Any help?

Upvotes: 1

Views: 74

Answers (1)

Aaron
Aaron

Reputation: 2364

You can't use a variable to define the initial value for another variable. A better way to construct B is like this

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

Upvotes: 1

Related Questions