【问题标题】:My accuracy for the ANN is not coming perfect我对 ANN 的准确性并不完美
【发布时间】:2021-06-17 13:34:53
【问题描述】:
crop: object = CropData(path= '')    
crop.dataset: pd.DataFrame = CropAnalysis.rename_columns(dataset= 
crop.dataset)
crop.dataset.head()


data_charaterictics: Generator = 
CropAnalysis.data_characteristics(dataset= crop.dataset)
while True:
try:
    print('-'*100)
    print(data_charaterictics.__next__())
except StopIteration: break


unique_values: Generator = 
CropAnalysis.data_unique_values(dataset= crop.dataset)
while True:
try:
    print('-'*100)
    print(unique_values.__next__())
except StopIteration: break

target_classification: Generator = 
CropAnalysis.target_classification_count(dataset= crop.dataset, 
target= 'crop') 
while True:
try:
    print('-'*100)
    print(target_classification.__next__())
except StopIteration: break

crop.dataset.keys()

numeric_histoplots: Generator = 
CropAnalysis.histograms_numeric_features(dataset= crop.dataset,
                                                                     
numeric_features= [
                                                                         
'Nitrogen', 'Phosphorus',
                                                                         
'Potassium', 'Temp','hum', 'PH','Rain'
                                                                        
]    )
while True:
try: numeric_histoplots.__next__()
except AttributeError: break

crop.dataset: pd.DataFrame = 
CropPreprocess.change_object_to_str(dataset= crop.dataset, cols= 
['crop'])
crop.dataset: pd.DataFrame = 
CropPreprocess.encode_features(dataset= crop.dataset)
crop.dataset

X, y = crop.dataset.drop('crop', axis= 1), crop.dataset['crop']
from sklearn import preprocessing
normalizer = preprocessing.Normalizer()
normalized_train_X = normalizer.fit_transform(X_train)
normalized_train_X

from tensorflow.keras import utils
from tensorflow.keras.utils import to_categorical
one_hot_y_train = to_categorical(y_train)
one_hot_y_test = to_categorical(y_test)


from sklearn.model_selection import train_test_split as tts
X_train,X_test,y_train,y_test=tts(X,y,test_size=0.2)

 from tensorflow import keras

 from tensorflow.keras import layers , Sequential 

` from keras.layers import Dense

import keras
from keras.models import Sequential
from keras.layers import Dense
# Neural network
model = Sequential()
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(22, activation='softmax'))


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

model.fit(X_train, y_train, batch_size = 25, epochs = 100)

准确率和损失的值小到0.04,这与机器学习算法不一样,数据的大小也很大,比如2200*8,不小。帮我看看数据有什么问题 数据集在这里提供https://www.kaggle.com/atharvaingle/crop-recommendation-dataset

【问题讨论】:

  • 嘿 Tanya,似乎正在发生的事情是您的建模没有在训练中收敛。如果模型不适应您的数据,这可能是垃圾(垃圾进垃圾出原则),或者您的模型架构不够好(这似乎是这种情况)。尝试另一种神经网络架构,因为我认为你拥有的那个不会工作。尝试使用谷歌搜索其中的一些,或者至少增加密集层中的节点并更改层的激活函数(最后一层除外)。我不是 keras 框架方面的专家,但这似乎是你的许多问题
  • 只使用一个神经元的 softmax 是没有意义的,因为它会产生一个 1.0 的常数值
  • @Tom 你能帮我更多吗,我无法理解。如果可能,请您更改代码。我已经完成了我能做的所有事情,但仍然无法找到正确的。请帮我解决这个问题
  • @Dr Snoopy 我得到的错误是 ValueError: Unknown loss function: crossentropy。请确保将此对象传递给custom_objects 参数..帮我整理一下
  • 损失不是这样叫的,有binary_crossentropy或者categorical_crossentropy。

标签: python tensorflow keras


【解决方案1】:

您需要向输出层添加更多神经元。目前您正在预测是否使用单一作物。但在 Kaggle 数据集中,标签实际上是 22 种不同的作物,你想决定哪种作物最适合手头的土壤。 所以你的输出层需要 22 个神经元,即将你的最后一层更改为

model.add(Dense(22, activation='softmax'))

由此产生的预测将表明哪种作物最适合土壤。

确保y_train,y_test 是一个热向量。如果它们还没有,您可以将它们转换为 f.e.通过使用one_hot_y_train = to_categorical(y_train)

【讨论】:

  • 我仍然收到此错误@yuki ValueError:形状(无,1)和(无,22)不兼容
  • from tensorflow.keras import utils from tensorflow.keras.utils import to_categorical one_hot_y_train = to_categorical(y_train) one_hot_y_test = to_categorical(y_test) from sklearn.model_selection import train_test_split as tts X_train,X_test,y_train,y_test= tts(X,y,test_size=0.2)
  • @TanyaSaraswat 人们一直在帮助你,但部分信息并不容易,所以请停止寻求帮助,只需提供更多信息。从这个错误中,您是否将最后一层中的神经元数量更改为 22 并且对您的标签进行一次性编码?还有你现在用的是哪个loss?
  • 我在最后一层改变了它,也对标签进行了热编码。我使用的损失是 categorical_crossentropy。 @史努比博士
  • @TanyaSaraswat 然后用你的最新代码更新你的问题。
猜你喜欢
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 2020-11-03
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
相关资源
最近更新 更多