received ((None, 2) vs (None, 5))

关于用keras跑深度学习模型,分类问题的小汇总。

 

拿到手的是2分类的模型,要改成5分类。本来是没有头绪的,查了一圈解决了问题,汇总一下,便于以后查看。

python - Tensorflow ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1)) - Stack Overflow

上面是我查到能解决我问题的帖子,

I'm new to Machine Learning, thought I'll start with keras. Here I'm classifying movie reviews as three class classification (positive as 1, neutral as 0 and negative as -1) using binary crossentropy. So, when I'm trying to wrap my keras model with tensorflow estimator, I get the error.
The code is as follows:

 大概意思就是他要跑一个三分类的模型,拿到手的模型去跑,报错了,代码如下:

import tensorflow as tf
import numpy as np
import pandas as pd
import numpy as K

csvfilename_train = 'train(cleaned).csv'
csvfilename_test = 'test(cleaned).csv'

# Read .csv files as pandas dataframes
df_train = pd.read_csv(csvfilename_train)
df_test = pd.read_csv(csvfilename_test)

train_sentences  = df_train['Comment'].values
test_sentences  = df_test['Comment'].values

# Extract labels from dataframes
train_labels = df_train['Sentiment'].values
test_labels = df_test['Sentiment'].values

vocab_size = 10000
embedding_dim = 16
max_length = 30
trunc_type = 'post'
oov_tok = '<OOV>'

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

tokenizer = Tokenizer(num_words = vocab_size, oov_token = oov_tok)
tokenizer.fit_on_texts(train_sentences)
word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(train_sentences)
padded = pad_sequences(sequences, maxlen = max_length, truncating = trunc_type)

test_sequences = tokenizer.texts_to_sequences(test_sentences)
test_padded = pad_sequences(test_sequences, maxlen = max_length)

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length = max_length),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(6, activation = 'relu'),
    tf.keras.layers.Dense(2, activation = 'sigmoid'),
])
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

num_epochs = 10
model.fit(padded, train_labels, epochs = num_epochs, validation_data = (test_padded, test_labels))

 报错信息:

ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))

下面热心老哥的回复:

 一下就指出了问题所在,一个是loss的二分类要改成多分类,还有一个是最后的Dense层数改成3,以及把标签转换成one-hot。

把loss里的binary_crossentropy改成categorical_crossentropy

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

然后是最后一个Dense层的层数改成3

tf.keras.layers.Dense(3, activation = 'sigmoid'),

还有一个老哥的文章也很有参考价值,贴下面了。

tensorflow2.0使用自带的函数求精准率和召回率(解决Shapes (None, 10) and (None, 1) are incompatible)

Dense层的作用

在keras中相当于全连接层,接收上一个Dense输出作为下一个Dense的输入,最后一个Dense负责输出。

以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值