I've always thought x /= y
is equal to x = x / y
. But now I'm facing a situation that I'll have an error when I use /=
but not when using x = x / y
. so definitely they shouldn't be the same in python.
The code is this. (is a simple deep learning code in Tensorflow, read code comments for some details).
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
# x_train is a (60000, 28, 28) numpy matrix
x_train /= 1 # this will raise error "ValueError: output array is read-only"
x_train = x_train / 1 # but this will work fine
ValueError Traceback (most recent call last) <ipython-input-44-fceb080f135a> in <module>() 1 ----> 2 x_train /= 1 ValueError: output array is read-only
I want to ask the difference between them. Why I'm getting this error from /=
?