首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >神经网络中的多变量输出|为什么凯拉斯会产生负binary_cross_entropy?

神经网络中的多变量输出|为什么凯拉斯会产生负binary_cross_entropy?
EN

Stack Overflow用户
提问于 2021-04-10 17:11:00
回答 1查看 49关注 0票数 0

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

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

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

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

代码语言:javascript
运行
复制
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)

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

EN

回答 1

Stack Overflow用户

发布于 2021-04-10 17:19:41

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

代码语言:javascript
运行
复制
model.add(Dense(1, activation='sigmoid'))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67032620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档