You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于TensorFlow Keras的5类多分类任务技术咨询

5-Class Multi-Classification Implementation with TensorFlow + Keras

Here's a structured, cleaned-up version of your code for a 5-class multi-classification task, formatted for readability and best practices:

Step 1: Load and Preprocess the Dataset

First, we load the CSV data, split it into input features and target labels, then convert labels to one-hot encoding (required for multi-class classification with categorical loss functions).

# Import required libraries (add these at the top of your script)
import pandas as pd
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load dataset
dataframe = pd.read_csv("Data5Class.csv", header=None)
dataset = dataframe.values

# Split into input (X) and output (Y) variables
X = dataset[:, 0:47].astype(float)  # First 47 columns = numerical input features
Y = dataset[:, 47]  # 48th column = raw class labels

print("Load Data.....")

# Convert raw labels to one-hot encoded format for multi-class training
Y_encoded = to_categorical(Y)

Step 2: Define the Neural Network Model

Next, we build a sequential model with an input layer and first hidden layer. Your original code cuts off at kernel_initiali...—common weight initialization choices here are 'glorot_uniform' or 'he_normal'.

def create_larger():
    model = Sequential()
    print("Create Dense Ip & HL 1 Model ......")
    # Input layer + first hidden layer (47 units matches input dimension)
    model.add(Dense(47, input_dim=47, kernel_initializer='glorot_uniform', activation='relu'))
    
    # Add additional hidden layers here (e.g., model.add(Dense(32, activation='relu')))
    
    # Critical: Add output layer for 5-class classification
    model.add(Dense(5, activation='softmax'))
    
    return model

Key Next Steps:

  • Compile the model: Use categorical_crossentropy loss (for one-hot labels) and an optimizer like Adam:
    model = create_larger()
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    
  • Train the model: Use model.fit() with your preprocessed data:
    history = model.fit(X, Y_encoded, epochs=50, batch_size=32, validation_split=0.2)
    

内容的提问来源于stack exchange,提问作者Amulya Dixit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:20:12