How to fix the error of "plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)"

Viewed 45

I'm testing the "Variational autoencoder (VAE)" from this link: https://blog.keras.io/building-autoencoders-in-keras.html

But the following code will cause error:

x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
plt.figure(figsize=(6, 6))
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
plt.colorbar()
plt.show()

Here is the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_3369/2564361963.py in <cell line: 2>()
      1 plt.figure(figsize=(6, 6))
----> 2 plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
      3 plt.colorbar()
      4 plt.show()

TypeError: list indices must be integers or slices, not tuple

I have tried to print the data type of x_test_encoded:

print("x_test_encoded: type: {}, len: {}".format(
    type(x_test_encoded),
    len(x_test_encoded)
))
x_test_encoded

Output:

x_test_encoded: type: <class 'list'>, len: 3

[array([[-0.5947027 , -0.52120334],
        [ 0.4796909 ,  0.3287477 ],
        [-3.005046  , -0.34468982],
        ...,
        [-0.62982583, -0.240578  ],
        [-0.9350344 ,  0.38294736],
        [ 0.82171804, -0.7310184 ]], dtype=float32),
 array([[-1.2073065 , -1.0317452 ],
        [-1.517373  , -1.224308  ],
        [-0.15326577, -0.5757952 ],
        ...,
        [-1.0883944 , -0.97350955],
        [-0.97157186, -1.1322904 ],
        [-1.5575923 , -1.2778456 ]], dtype=float32),
 array([[-0.60485274, -0.56204545],
        [ 0.46670908,  0.34251845],
        [-2.9650807 , -0.40624568],
        ...,
        [-0.5976225 , -0.25444514],
        [-0.95324576,  0.4048244 ],
        [ 0.8028061 , -0.66895425]], dtype=float32)]

Print the first element:

print("x_test_encoded[:][0]: type: {}, len: {}, shape: {}, dtype: {}".format(
    type(x_test_encoded[:][0]),
    len(x_test_encoded[:][0]),
    x_test_encoded[:][0].shape,
    x_test_encoded[:][0].dtype
))
x_test_encoded[:][0]

Output:

x_test_encoded[:][0]: type: <class 'numpy.ndarray'>, len: 10000, shape: (10000, 2), dtype: float32

array([[-0.5947027 , -0.52120334],
       [ 0.4796909 ,  0.3287477 ],
       [-3.005046  , -0.34468982],
       ...,
       [-0.62982583, -0.240578  ],
       [-0.9350344 ,  0.38294736],
       [ 0.82171804, -0.7310184 ]], dtype=float32)

How to fix the error of "plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)"?

What does "[:, 0]" mean in a "list" type variable?

1 Answers

Your encoder is has three outputs [z_mean, z_log_sigma, z], but you are actually only interested in z at position [-1]. So, it should actually be something like this (the tutorial seems to have an error):

x_test_encoded = encoder.predict(x_test, batch_size=32)
plt.figure(figsize=(6, 6))
plt.scatter(x_test_encoded[-1][:, 0], x_test_encoded[-1][:, 1], c=y_test)
plt.colorbar()
plt.show()

enter image description here

Since z consists of two latent variables, you index 0 and 1.

Related