파이썬 pyplot - ValueError: Illegal argument(s) to subplot: (2, 10)
조회수 938회
Traceback (most recent call last):
File "C:/Users/yym30/PycharmProjects/tensorflow/mnist/networks/autoencoder2.py", line 62, in <module>
fig, ax = plt.subplot(2, sample_size, figsize = (sample_size, 2))
File "C:\Users\yym30\Anaconda3\envs\dl\lib\site-packages\matplotlib\pyplot.py", line 1072, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Users\yym30\Anaconda3\envs\dl\lib\site-packages\matplotlib\figure.py", line 1239, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Users\yym30\Anaconda3\envs\dl\lib\site-packages\matplotlib\axes\_subplots.py", line 72, in __init__
raise ValueError('Illegal argument(s) to subplot: %s' % (args,))
ValueError: Illegal argument(s) to subplot: (2, 10)
Process finished with exit code 1
골빈해커의 텐서플로 책을 보며 배우는 중인데 저는 골빈해커의 책에 나오는 코드를 그대로 베껴썼는데도 이런 오류가 나오네요... pip, pyplot 모두 최신버전으로 업그레이드 했습니다. 오토인코더를 연습하는 중이었습니다.
sample_size = 10
samples = sess.run(decoder, feed_dict={X: mnist.test.images[:sample_size]})
fig, ax = plt.subplot(2, sample_size, figsize = (sample_size, 2))
for i in range(sample_size):
ax[0][i].set_axis_off()
ax[1][i].set_axis_off()
ax[0][i].imshow(np.reshape((mnist.test.images[i], (28,28))))
ax[1][i].imshow(np.reshape(samples[i], (28,28)))
plt.show()
오토인코더를 통과한 이미지를 표시해 달라는 코드가 위 코드입니다.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("C:\WD\mnist\mnist_data", one_hot= True)
#옵션 설정합니다.
learning_rate = 0.01
training_epoch = 20
batch_size = 100
n_hidden = 256
n_input = 28*28
X = tf.placeholder(tf.float32, [None, n_input])
W_encode = tf.Variable(tf.random_normal([n_input, n_hidden]))
b_encode = tf.Variable(tf.random_normal([n_hidden]))
encoder = tf.nn.sigmoid(
tf.add(tf.matmul(X, W_encode), b_encode)
)
W_decode = tf.Variable(tf.random_normal([n_hidden,n_input]))
b_decode = tf.Variable(tf.random_normal([n_input]))
decoder = tf.nn.sigmoid(tf.add(
tf.matmul(encoder, W_decode), b_decode
))
cost = tf.reduce_mean(tf.pow(X - decoder, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)
#학습
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
total_batch = int(mnist.train.num_examples/batch_size)
for epoch in range(training_epoch):
total_cost = 0
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, cost_val = sess.run([optimizer,cost],
feed_dict={X: batch_xs})
total_cost += cost_val
print('Epoch:', '%04d' % (epoch + 1),
'Avg.cost = ', '{:.4f}'.format(total_cost / total_batch))
print("최적화 완료!")
위 코드는 학습 부분으로, 이 부분은 문제 없이 통과합니다.
무엇이 문제인지를 몰라서 진전이 막혔습니다. 도와주세요 ㅜㅜㅜ.
1 답변
-
plt.subplot(2, sample_size, figsize = (sample_size, 2))
가 아니라plt.subplots(2, sample_size, figsize = (sample_size, 2))
로 써야합니다. (s 가 빠져있었음)
또, 골빈해커 - 텐서플로 책에 수록된 코드는 golbin / TensorFlow-Tutorials에 전부 나와있는 것 같아요.
이쪽을 참고해보시는게 좋을 듯욤!
댓글 입력