【问题标题】:Multiple variable output in Neural Network | Why is Keras yielding negative binary_cross_entropy?神经网络中的多变量输出|为什么 Keras 会产生负的 binary_cross_entropy?
【发布时间】:2021-04-10 09:11:00
【问题描述】:

我遇到了一个学校项目的问题。

我必须根据文本数据在测试集上预测一个人的年龄和性别。我的训练数据集有 4 个特征(ID、关键字、年龄、性别)。

我创建了一个神经网络(请参见下面的代码),但在拟合后者时,我的损失值非常负。

您能告诉我如何缓解这个问题吗?

import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split

#Load the datasets
chunk_train = pd.read_csv('/Users/romeoleon/Downloads/train.csv',chunksize=10**6)
data_train = pd.concat(chunk_train)

#Map the values for sex columns
data_train.sex = data_train.sex.map({'M':0,'F':1})

#Remove the rows with missing data
print('Missing rows represent {} percent of the dataframe'.format(data_train['keywords'].isna().sum()/len(data_train.keywords)*100))

#Drop the missing values
data_train.dropna(inplace=True)

#Plot the distribution of numerical variables
sns.histplot(data_train.age,bins=85)
plt.show()
sns.countplot(x='sex',data=data_train)
plt.show()

#Prepare the data to feed it to the NN
import numpy as np
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

x_train, x_test, y_train, y_test = train_test_split(data_train['keywords'],data_train[["age","sex"]],test_size=0.2)

#Choose parameters
vocab_size = 1000
maxlen = 300
batch_size = 32
embedding_dims = 100
hidden_dims = 5
filters = 250
kernel_size = 3
epochs = 10

#Tokenize the words
tokenizer = Tokenizer(num_words=vocab_size)

tokenizer.fit_on_texts(x_train)

X_train = tokenizer.texts_to_matrix(x_train)
X_test = tokenizer.texts_to_matrix(x_test)

#Pad sequencing : Ensure all sequences have the same length
from tensorflow.keras.preprocessing import sequence
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, MaxPooling1D
from tensorflow.keras.layers import Embedding, LSTM
from tensorflow.keras.layers import Conv1D, Flatten

#Create the model
model = Sequential()
model.add(Embedding(vocab_size, embedding_dims, input_length=maxlen, trainable=True))

model.add(Dropout(0.5))
model.add(Conv1D(filters,
                 kernel_size,
                 padding='valid',
                 activation='relu'))
#model.add(MaxPooling1D(pool_size=4))
model.add(Flatten())
model.add(Dense(hidden_dims, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='sigmoid'))

# Compile neural model
model.compile(loss='binary_crossentropy', # Cross-entropy
                optimizer='adam', # Root Mean Square Propagation
                metrics=['accuracy']) # Accuracy performance metric

model.summary()

#Fit the model
model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=1,
          validation_data=(X_test, y_test), verbose=1)

您可以在下面找到我的训练数据集结构的屏幕截图:

【问题讨论】:

    标签: python tensorflow keras loss-function


    【解决方案1】:

    当使用'binary_crossentropy'作为损失函数时,输出端dense应该只有1个单元而不是2个。(1个单元有2个状态,即1或0) 改用这个:

    model.add(Dense(1, activation='sigmoid'))
    

    【讨论】:

    • 您好,感谢您的回答!我有两个 y_train 变量,因此,当我试图用只有 1 个密集输出层来拟合我的模型时,我收到以下错误消息: ValueError: logits and labels must have the same shape ((None, 1) vs (无,2))
    • 你想预测一个人的年龄和性别,那么网络应该有2个输出;一个是年龄(连续值),另一个是性别/性别(分类)。这可以通过功能 API 来完成。 @RoméoLEON
    • 如果你想有多个输出,'sequential'方法可能不适合。相反,“模型”方法可以在这个多输出任务中正常工作:model = Model(inputs=inputs, outputs=[predictions1, predictions2])@RoméoLEON
    猜你喜欢
    • 2021-09-22
    • 2019-02-28
    • 1970-01-01
    • 2016-01-29
    • 2016-09-24
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多