首页 > 解决方案 > ValueError: 无法挤压 dim[1],预期维度为 1,

问题描述

ValueError:无法挤压暗淡 [1],预期维度为 1,输入形状为 [?,3] 的“metrics/sparse_categorical_accuracy/Squeeze”(操作:“Squeeze”)为 3。

鸢尾花数据集 在本作业中,您将使用鸢尾花数据集。它由来自三种鸢尾(Iris setosa、Iris virginica 和 Iris versicolor)每一种的 50 个样本组成。从每个样本中测量了四个特征:萼片和花瓣的长度和宽度,以厘米为单位。如需参考,请参阅以下论文:

RA费舍尔。“在分类问题中使用多重测量”。优生学年鉴。7 (2): 179–188, 1936。你的目标是构建一个神经网络,将每个样本分类到正确的类别中,并应用验证和正则化技术。

加载和预处理数据首先使用 datasets.load_iris() 读取 Iris 数据集,并将数据集拆分为训练集和测试集。

您现在可以构建模型以适应数据。使用 Sequential API,根据以下规范构建模型:

模型应该使用函数参数中的 input_shape 来设置第一层的输入大小。第一层应该是一个有 64 个单元的密集层。第一层的权重应该使用 He 统一初始化器进行初始化。第一层的偏差最初应该都等于 1。然后应该有另外四个密集层,每个有 128 个单元。接下来应该是四个密集层,每个层有 64 个单元。所有这些 Dense 层都应该使用 ReLU 激活函数。输出 Dense 层应该有 3 个单元和 softmax 激活函数。总的来说,网络应该有 10 层。

from numpy.random import seed
seed(8)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, model_selection 
get_ipython().run_line_magic('matplotlib', 'inline')

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Softmax

def read_in_and_split_data(iris_data):
 
    return model_selection.train_test_split(
         iris_data["data"], 
         iris_data["target"], 
         test_size=0.1
    )

# In[3]:

# Run your function to generate the test and training data.

iris_data = datasets.load_iris()
(train_data, test_data, 
 train_targets, test_targets) = read_in_and_split_data(iris_data)

# We will now convert the training and test targets using a one hot encoder.

# In[4]:

# Convert targets to a one-hot encoding

train_targets = tf.keras.utils.to_categorical(np.array(train_targets))
test_targets = tf.keras.utils.to_categorical(np.array(test_targets))

#### GRADED CELL ####

# Complete the following function. 
# Make sure to not change the function name or arguments.

def get_model(input_shape):
    """
    This function should build a Sequential model according to 
    the above specification. Ensure the weights are initialised 
    by providing the input_shape argument in the first layer, given by the
    function argument.
    Your function should return the model.
    """
    model = Sequential([
       Dense(64, activation = "rely", 
             kernel_initializer='he_uniform', 
             bias_initializer='ones', 
             input_shape=input_shape),
       Dense(128, activation = "relu"),
       Dense(128, activation = "relu"),
       Dense(128, activation = "relu"),
       Dense(128, activation = "relu"),
       Dense(64, activation = "relu"),
       Dense(64, activation = "relu"),
       Dense(64, activation = "relu"),
       Dense(64, activation = "relu"),
       Dense(3, activation = "softmax"),
       ])
    return model    
    
# In[16]:

# Run your function to get the model

model = get_model(train_data[0].shape)


# #### Compile the model
# 
# You should now compile the model using the `compile` method. 
# Remember that you need to specify an optimizer, a loss function and 
# a metric to judge the performance of your model.

# In[23]:

#### GRADED CELL ####

# Complete the following function. 
# Make sure to not change the function name or arguments.

def compile_model(model):
 
    #model.compile(loss="sparse_categorical_crossentropy", optimizer="adam")
    opt = tf.keras.optimizers.Adam(learning_rate=0.0001) 
    acc = tf.keras.metrics.SparseCategoricalAccuracy() 
    model.compile(optimizer=opt, 
                  loss='sparse_categorical_crossentropy', 
                  metrics=[acc] ) 

# In[24]:

# Run your function to compile the model

compile_model(model)

#### GRADED CELL ####

# Complete the following function. 
# Make sure to not change the function name or arguments.

def train_model(model, train_data, train_targets, epochs):
    """
    This function should train the model for the given number of epochs on the 
    train_data and train_targets. 
    Your function should return the training history, as returned by model.fit.
    """
    return model.fit(train_data, train_targets, epochs)

# Run the following cell to run the training for 800 epochs.

# In[26]:

# Run your function to train the model

history = train_model(model, train_data, train_targets, epochs=800)

标签: pythontensorflowkeras

解决方案


这是因为你有错误的损失函数。您的目标是一次性编码的,因此您不应使用'sparse_categorical_crossentropy'. 相反,您应该使用'categorical_crossentropy'

同样的事情acc = tf.keras.metrics.SparseCategoricalAccuracy()。它应该是acc = tf.keras.metrics.CategoricalAccuracy()


推荐阅读