1

How is it that you would create a tensorflow vector from a tensorflow constant/variable etc? For example I have a constant x and I want to create a vector which is [x].

I have tried the code below and it doesn't work. Any help would be appreciated.

x = tf.placeholder_with_default(1.0,[], name="x")
nextdd = tf.constant([x], shape=[1], dtype=tf.float32)
2
  • 1
    Do you want nextdd be a const or a variable? Commented Mar 18, 2018 at 15:34
  • I guess it would be a variable based on the value of x. The fact that I used constant is wrong in the example. Commented Mar 19, 2018 at 20:22

2 Answers 2

2

First I'd like to define a tensor for you:

Tensors are n-dimensional matrices. A rank 0 tensor is a scalar, e.g. 42. a rank 1 tensor is a Vector, e.g. [1,2,3], a rank 2 tensor is a matrix, a rank 3 tensor might be an image of shape [640, 480, 3] (640x480 resolution, 3 color channels). a rank 4 tensor might be a batch of such images of shape [10, 640, 480, 3] (10 640x480 images), etc.

Second, you have basically 4 types of tensors in Tensorflow.

1) Placeholders - these are tensors that you pass into tensorflow when you call sess.run. For example: sess.run([nextdd], {x:[1,2,3]}) creates a rank 1 tensor out of x.

2) Constants - these are fixed values as the name suggests. E.g. tf.constant(42) and should be specified at compile time, not runtime (eluding to your primary mistake here).

3) Computed tensors - x = tf.add(a,b) is a computed tensor, it's computed from a,b. Its value is not stored after the computation is finished.

4) Variables - These are mutable tensors that are kept around after the computation is complete. For example the weights of a neural network.

Now to address your question explicitly. x is already a tensor. If you were passing in a vector then it's a rank 1 tensor (aka a vector). You can use it just like you'd use a constant, computed tensor, or variable. They all work the same in operations. There is no reason for the nextdd line at all.

Now, nextdd fails becuase you tried to create a constant from a variable term, which isn't a defined operation. tf.constant(42) is well defined, that's what a constant is.

You could just use x directly, as in:

x = tf.placeholder_with_default(1.0,[], name="x")
y = tf.add(x, x)
sess = tf.InteractiveSession()
y.eval()

Result:

2.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but I actually prefer Maxim's. Its more direct.
0

From you description, it looks like you want to use tf.expand_dims:

# 't' is a tensor of shape [2]
tf.shape(tf.expand_dims(t, 0))  # [1, 2]
tf.shape(tf.expand_dims(t, 1))  # [2, 1]
tf.shape(tf.expand_dims(t, -1))  # [2, 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.