0

For example,

a = Input(...)
b = keras.layers.Conv2D(...)(a)
c = keras.backend.zeros(...)

c[...].assign(b[...])

Because it's used before a model is compiled, when I try to use the function assign() (this is a TensorFlow function), there comes the error: 'Tensor' object has no attribute 'assign'.

This is probably because before the model is compiled, the first dim of the variable is None. So, is there any way to do a sliced assign ?

Sharky
  • 4,473
  • 2
  • 19
  • 27
dascat
  • 1
  • It's too little context in your question. And compiled model likely has nothing to do with this. – Sharky Apr 11 '19 at 18:31

1 Answers1

1

In general, TensorFlow tensors are not assignable. However, as per official docs, tf.assign() function works only with mutable Tensors, which should be from a Variable node. So, the code below works. The rest will depend heavily on your particular case.

var1 = tf.keras.backend.zeros(1,1)
var2 = var1[0].assign(1)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(var2))

You may find this answer useful How to do slice assignment in Tensorflow

Sharky
  • 4,473
  • 2
  • 19
  • 27