我无法解决我的代码的问题。我在运行代码时不断收到以下错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-cdb1929785d0> in <module>()
108 tf.reset_default_graph()
109
--> 110 train_neural_network(x)
<ipython-input-1-cdb1929785d0> in train_neural_network(x)
93 end = i+batch_size
94 batch_x = np.array(X_train[start:end])
---> 95 batch_x = batch_x.reshape((batch_size,n_chunks,chunk_size))
96 batch_y = np.array(y_1Hot_train.eval()[start:end])
97
ValueError: cannot reshape array of size 784 into shape (10,28,28)
我的数据集是一个 (88041, 784) 数组,我的批量大小为 10。当我使用第 95 行并独立运行它时,我没有收到任何错误, reshape 无一异常(exception)地发生。
例如在 tensorflow 之外,此代码段有效:
batch_x = np.array(X_train[0:10])
batch_x = batch_x.reshape((batch_size,n_chunks,chunk_size))
batch_x.shape # returns a shape of (10, 28, 28)
所以我不确定为什么 tensorflow 一直抛出错误。如果您有更好的主意,我将不胜感激。
tf.sessions 部分是:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range (hm_epochs):
epoch_loss = 0
itere = int(X_train.shape[0]/batch_size)
last = 0
add = 1
batch_size = 10
i=0
while i < len(X_train):
start = i
end = i+batch_size
batch_x = np.array(X_train[start:end])
batch_x = batch_x.reshape((batch_size,n_chunks,chunk_size))
batch_y = np.array(y_1Hot_train.eval()[start:end])
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
epoch_loss += c
i+=batch_size
sess_end = time.time() - start_time
代码在这里:https://gist.github.com/makark/bab1cd6a80667226d0aff35f637463b0
请您参考如下方法:
我解决了这个问题,问题似乎是我使用的数据集大小为 (88041, 784),无法被 10 (batch_Size) 等分。似乎在循环的最后一次迭代中,留下了 1 个样本并导致了错误。我从 X_train 中删除了最后一个样本,现在它可以工作了……
