Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分类形状中的keras值误差_Python_Tensorflow_Machine Learning_Keras_Classification - Fatal编程技术网

Python 分类形状中的keras值误差

Python 分类形状中的keras值误差,python,tensorflow,machine-learning,keras,classification,Python,Tensorflow,Machine Learning,Keras,Classification,这是我的密码: # data consists of 1 dimensional data (3277 elements). Number of data is 439 train_data = .... # numpy.ndarray # I would like to classify data into 5 classes. train_labels = .... # numpy.ndarray print(train_data.shape) # -> Shape of tra

这是我的密码:

# data consists of 1 dimensional data (3277 elements). Number of data is 439  
train_data = .... # numpy.ndarray
# I would like to classify data into 5 classes.
train_labels = .... # numpy.ndarray

print(train_data.shape) # -> Shape of train_data: (439, 3277)
print('Shape of train_labels:', train_labels.shape) # -> Shape of train_labels: (439,)
# prepare 5 one hot encoding array
categorical_labels = to_categorical(train_labels, 5)
print('Shape of categorical_labels:', categorical_labels.shape) # -> Shape of categorical_labels: (439, 5)

# I make a model to have 3277-elements data and classify data into 5 labels.
model = keras.Sequential([
    keras.layers.Dense(30, activation='relu', input_shape=(3277,)),
    keras.layers.Dense(30, activation='relu'),
    keras.layers.Dense(5, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])
model.fit(data, categorical_labels, epochs=5, verbose=1) # A
#model.fit(data, train_labels, epochs=5, verbose=1) # B
当我尝试使用标记为“A”的行时,我得到了这个错误

ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (5,)
使用“B”,它运行正常(无明显错误,机器返回高分)

显然,这个错误与形状的不同有关。。。当我想使用
keras.utils.to_category
时,如何修改我的代码

另一个问题是为什么这个案例有效()而我的案例无效


结构看起来很相似。。。对我来说。

因为
sparse\u categorical\u crossentropy
不希望标签采用一种热编码格式,所以应该使用
loss='categorical\u crossentropy'

简言之,关于你的情况:

  • train\u标签
    =>
    loss='sparse'u categorical\u crossentropy'
  • categorical\u标签
    =>
    loss='categorical\u交叉熵'