How to normalize the size of a tensorflow variable -
i'm writing tensorflow code want normalize variable vector after each update. following code works well:
sess = tf.interactivesession() y = tf.variable(tf.random_uniform([2], -0.5, 0.5)) init = tf.initialize_all_variables() sess.run(init) = [2, 3] loss = tf.reduce_sum(tf.square(a - y)) y = y / tf.sqrt(tf.reduce_sum(tf.square(y))) optimizer = tf.train.gradientdescentoptimizer(0.05) train = optimizer.minimize(loss) step in range(100): sess.run(train) temp2= sess.run(y) print(temp2)
and gives desired answer [ 0.55469805 0.83205169], normalized vector in direction of [2,3]
however, if change
y = tf.variable(tf.random_uniform([2], -0.5, 0.5))
to
y = tf.variable(tf.random_uniform([2,2], -0.5, 0.5))
and
y = y / tf.sqrt(tf.reduce_sum(tf.square(y)))
to
y[0] = y[0] / tf.sqrt(tf.reduce_sum(tf.square(y[0])))
then error says "'variable' object not support item assignment". changed loss function
loss = tf.reduce_sum(tf.square(a - y[0]))
can how can normalize vector column y[0] of variable type in tensorflow?
as y
tensor object , cannot assign value tensor do. hence, should works on array of tensor, , after assign value following:
yarray = y.eval() = [2, 3] loss = tf.reduce_sum(tf.square(a - y.eval()[0][:])) yarray[0][:] = yarray[0][:] / tf.sqrt(tf.reduce_sum(tf.square(yarray[0][:]))).eval() y.assign(yarray)
in above, array of tensor using eval
function. then, compute loss function, , yarray
normalization. finally, assign value of yarray
y
.
Comments
Post a Comment