2

In the simple program before I am not able to do a simple task and get the following error.

import tensorflow as tf

x_1= tf.constant([1, 2, 3])
x_1= tf.reshape(x_1, shape= (1, 3))
x_2= tf.constant([2, 3, 4])
x_2= tf.reshape(x_2, shape= (1, 3))
x_3= tf.constant([3, 4, 5])
x_3= tf.reshape(x_3, shape= (1, 3))
x= tf.concat((x_1, x_2, x_3), axis=0)

for i in range(0, 3):
    x[i, :]= x[i, :]+ 1

init= tf.global_variables_initializer()

with tf.Session() as sess:
   y= sess.run(x)

And I get the following error:

TypeError: 'Tensor' object does not support item assignment

0

1 Answer 1

2

Tensor objects cannot be accessed/modified by index.

Here's the code fixed:

import tensorflow as tf

x_1 = tf.constant([1, 2, 3])
x_1 = tf.reshape(x_1, shape=(1, 3))
x_2 = tf.constant([2, 3, 4])
x_2 = tf.reshape(x_2, shape=(1, 3))
x_3 = tf.constant([3, 4, 5])
x_3 = tf.reshape(x_3, shape=(1, 3))
x = tf.concat((x_1, x_2, x_3), axis=0)

x = tf.add(x, tf.constant(1, shape=x.shape))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    y = sess.run(x)
    print(y)
Sign up to request clarification or add additional context in comments.

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.