[英]How to fix the error of "plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)"
I'm testing the " Variational autoencoder (VAE)
" from this link: https://blog.keras.io/building-autoencoders-in-keras.html我正在通过此链接测试“ Variational autoencoder (VAE)
”: https://blog.keras.io/building-autoencoders-in-keras.ZFC35FDC70D5FC69D2569883A8EZCA7
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
:我试图打印x_test_encoded
的数据类型:
print("x_test_encoded: type: {}, len: {}".format(
type(x_test_encoded),
len(x_test_encoded)
))
x_test_encoded
Output: 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: 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)
"?如何修复“ plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
”的错误?
What does " [:, 0]
" mean in a " list
" type variable? “ [:, 0]
”在“ list
”类型变量中是什么意思?
Your encoder is has three outputs [z_mean, z_log_sigma, z]
, but you are actually only interested in z
at position [-1].您的编码器具有三个输出[z_mean, z_log_sigma, z]
,但您实际上只对 position [-1] 处的z
感兴趣。 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()
Since z
consists of two latent variables, you index 0 and 1.由于z
包含两个潜在变量,因此您索引 0 和 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.