簡體   English   中英

預期 flatten_input 具有 3 個維度,但得到了具有形狀的數組

[英]Expected flatten_input to have 3 dimensions, but got array with shape

我正在關注tensorflow的基本分類教程。 由於代理原因,我必須離線使用數據集。 因此,我使用的是 mnist 數據集,而不是使用 fashion_mnist 數據庫。

from __future__ import absolute_import, division, 

print_function, unicode_literals

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Flatten, Dense

# Noting class names
class_names = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']

# Load dataset
mnist = keras.datasets.mnist
path = 'C:/projects/VirtualEnvironment/MyScripts/load/mnist.npz'
(train_x, train_y), (test_x, test_y) = mnist.load_data(path)

# Scale, so that training and testing set is preprocessed in the same way
train_x = train_x / 255.0
test_x = test_y / 255.0
train_y = tf.expand_dims(train_y, axis = -1)
test_y = tf.expand_dims(test_y, axis = -1)

#Build the model

#1. Setup the layers
model = keras.Sequential()
model.add(Flatten(input_shape = (28, 28)))
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dense(10, activation=tf.nn.softmax))


#2. Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(train_x, train_y, epochs=1)
print("Finished Training")

# Evaluate how the model performs on the test dataset
test_loss, test_acc = model.evaluate(test_x,  test_y, verbose=2)

我收到以下錯誤: ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (10000, 1). 我對 tensorflow 知之甚少,所以如果有人可以指導我找到有用的網頁,或者可以向我解釋錯誤的含義,我將非常感激

這對我有用:

(train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data()

# Scale, so that training and testing set is preprocessed in the same way
train_x = train_x / 255.0
test_x = test_x / 255.0

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape = (28, 28)))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))


#2. Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(train_x, train_y, epochs=1)

# Evaluate the model
test_loss, test_acc = model.evaluate(test_x,  test_y, verbose=2)

您收到的錯誤意味着您在代碼中的某處錯誤地重新調整了輸入。

這是由您的代碼中的拼寫錯誤引起的

改變這個

test_x = test_y / 255.0

test_x = test_x / 255.0

我不確定數組形狀問題,但是我可以幫助您解決代理問題,以便您可以正確下載數據集。 假設您從 IT 部門清楚了解如何使用工具,您可以通過在終端級別導出來設置 pip 代理:

假設您的登錄憑據是 COMPANY\username

export http_proxy=http://COMPANY%5Cusername:password@proxy_ip:proxy_port
export https_proxy=http://COMPANY%5Cusername:password@proxy_ip:proxy_port

如果您使用的是 conda 環境,請使用 C:\Users\username 處的 .condarc 並編輯為:

channels:
- defaults

# Show channel URLs when displaying what is going to be downloaded and
# in 'conda list'. The default is False.
show_channel_urls: True
allow_other_channels: True

proxy_servers:
    http: http://COMPANY\username:password@proxy_ip:proxy_port
    https: https://COMPANY\username:password@proxy_ip:proxy_port


ssl_verify: False

希望能幫助到你。 為了調試數組形狀,我建議您在擴展尺寸后使用 train_y.shape() 和 train_x.shape() 打印 train_y 和 train_x 形狀。 該錯誤指定它得到一個 10000D object 值 1 不應該是這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM