cgjung
cgjung

Reputation: 47

Numpy array to Tensor

I am changing the code created by numpy to tensorflow code.

However, tensorflow does not support specifying each element, (eg x [i] = 7), boolean (eg.var [x <0.25] = -1) with possible array of numpy It is difficult.

How can I change the following code to tensor?

x=np.random.rand((500*300))
var=np.zeros((500*300), dtype=np.uint16)
var[x<.25] = -1
var[x>.75] = 1
S=var.reshape((500, 300))

Please, help me.

Note: I try this step.

x=tf.random_uniform((500*300), minval=0, maxval=1, dtype=tf.float32)
var=tf.zeros((500*300), int16)
var[x<.25] = -1  # How is the change???????
var[x>.75] = 1   # How is the change???????
S=var.reshape((500, 300))

Upvotes: 2

Views: 749

Answers (2)

Mohan Radhakrishnan
Mohan Radhakrishnan

Reputation: 3197

I think if you flatten both x and var, tf.scatter_update can do it. And then you can reshape it.

x = tf.random_uniform((5,3), minval=0, maxval=1, dtype=tf.float32)

var = tf.Variable(tf.zeros((5 , 3), tf.int32))

flattenx = tf.Variable(tf.reshape(x, [-1]))
flattenvar = tf.Variable(tf.reshape(var, [-1]))

minusone = tf.squeeze(tf.where(tf.less(flattenx, .25)))
plusone = tf.squeeze(tf.where(tf.greater(flattenx, .75)))

with tf.Session() as sess :

    sess.run(tf.global_variables_initializer())
    #create the required number of -1's using tile and update
    update = tf.scatter_update(flattenvar,
                               minusone,
                               tf.tile(tf.constant([-1],
                                       tf.int32),
                                       tf.shape(minusone)))
    #create the required number of 1's using tile and update
    update = tf.scatter_update(update,
                               plusone,
                               tf.tile(tf.constant([1],
                                       tf.int32),
                                       tf.shape(plusone)))
    print(sess.run(tf.transpose(tf.reshape(update,(3,5)))))
    print(sess.run(tf.transpose(tf.reshape(flattenx,(3,5)))))

I am transposing like this because if I directly reshape it to (5,3) the positions don't match.

Output is this.

[[ 0  1 -1]
 [ 0  0 -1]
 [ 0  1  0]
 [ 1  0  1]
 [-1  0  0]]
[[0.53033566 0.8070284  0.0320853 ]
 [0.7295474  0.684116   0.18633509]
 [0.4942881  0.89346325 0.5873647 ]
 [0.96912587 0.5400375  0.8372116 ]
 [0.14598823 0.62156534 0.54353106]]

Upvotes: 1

Nipun Wijerathne
Nipun Wijerathne

Reputation: 1829

Use tf.where as suggested in the comments. I have below provided a sample code and commented where necessary.

x = tf.random_uniform(shape=[5, 3], minval=0, maxval=1, dtype=tf.float32)

#same shape as x and only contains -1
c1 = tf.multiply(tf.ones(x.shape, tf.int32), -1)
#same shape as x and only contains 1
c2 = tf.multiply(tf.ones(x.shape, tf.int32), 1)

var = tf.zeros([5, 3], tf.int32)

#assign 1 element wise if x< 0.25 else 0
r1 = tf.where(tf.less(x, 0.25), c1, var)
#assign -1 element wise if x> 0.75 else 0
r2 = tf.where(tf.greater(x, 0.75), c2, var)

r = tf.add(r1, r2)

with tf.Session() as sess:
    _x, _r = sess.run([x, r])

    print(_x)
    print(_r)

Example Result

[[0.6438687  0.79183984 0.40236235]
 [0.7848805  0.0117377  0.6858672 ]
 [0.6067281  0.5176437  0.9839716 ]
 [0.15617108 0.28574145 0.31405795]
 [0.28515983 0.6034068  0.9314337 ]]

[[ 0  1  0]
 [ 1 -1  0]
 [ 0  0  1]
 [-1  0  0]
 [ 0  0  1]]

Hope this helps.

Upvotes: 2

Related Questions