【问题标题】:Can I implement an iterative training loop for the ELM classifier?我可以为 ELM 分类器实现迭代训练循环吗?
【发布时间】:2020-12-03 06:12:30
【问题描述】:

我正在使用极限学习机分类器进行手势识别,但我仍然有 20% 的准确度。谁能帮我实现一个迭代训练循环来提高准确度?我是初学者,这里是我正在使用的代码:我将准备好的数据集在归一化后拆分为训练和测试部分,并使用训练函数通过计算摩尔彭罗斯逆进行训练,然后使用预测函数预测每个手势的类别。

# -*- coding: utf-8 -*-
"""
Created on Sat Jul  4 17:52:25 2020

@author: lenovo
"""

# -*- coding: utf-8 -*-
__author__ = 'Sarra'

import numpy as np

class ELM(object):  

def __init__(self, inputSize, outputSize, hiddenSize):
    """
    Initialize weight and bias between input layer and hidden layer
    Parameters:
    inputSize: int
        The number of input layer dimensions or features in the training data
    outputSize: int
        The number of output layer dimensions
    hiddenSize: int
        The number of hidden layer dimensions        
    """    

    self.inputSize = inputSize
    self.outputSize = outputSize
    self.hiddenSize = hiddenSize       
    
    # Initialize random weight with range [-0.5, 0.5]
    self.weight = np.matrix(np.random.uniform(-0.5, 0.5, (self.hiddenSize, self.inputSize)))

    # Initialize random bias with range [0, 1]
    self.bias = np.matrix(np.random.uniform(0, 1, (1, self.hiddenSize)))
    
    self.H = 0
    self.beta = 0

def sigmoid(self, x):
    """
    Sigmoid activation function
    
    Parameters:
    x: array-like or matrix
        The value that the activation output will look for
    Returns:      
        The results of activation using sigmoid function
    """
    return 1 / (1 + np.exp(-1 * x))

def predict(self, X):
    """
    Predict the results of the training process using test data
    Parameters:
    X: array-like or matrix
        Test data that will be used to determine output using ELM
    Returns:
        Predicted results or outputs from test data
    """
    X = np.matrix(X)
    y = self.sigmoid((X * self.weight.T) + self.bias) * self.beta

    return y

def train(self, X, y):
    """
    Extreme Learning Machine training process
    Parameters:
    X: array-like or matrix
        Training data that contains the value of each feature
    y: array-like or matrix
        Training data that contains the value of the target (class)
    Returns:
        The results of the training process   
    """

    X = np.matrix(X)
    y = np.matrix(y)        
    
    # Calculate hidden layer output matrix (Hinit)
    self.H = (X * self.weight.T) + self.bias

    # Sigmoid activation function
    self.H = self.sigmoid(self.H)

    # Calculate the Moore-Penrose pseudoinverse matriks        
    H_moore_penrose = np.linalg.pinv(self.H.T * self.H) * self.H.T

    # Calculate the output weight matrix beta
    self.beta = H_moore_penrose * y

    return self.H * self.beta


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn import preprocessing
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
# read the dataset
database = pd.read_csv(r"C:\\Users\\lenovo\\tensorflow\\tensorflow1\\Numpy-ELM\\hand_gestures_database.csv")                  
#separate data from labels 
data = database.iloc[:, 1:].values.astype('float64')

#normalize data
#n_data = preprocessing.minmax_scale(data, feature_range=(0, 1), axis=0, copy=True)
scaler = MinMaxScaler()
scaler.fit(data)
n_data = scaler.transform(data)
#identify the labels 
label = database.iloc[:, 0]
#encoding labels to transform each label to a value between 0 to number of labels-1
def prepare_targets(n):
    le =preprocessing.LabelEncoder()
    le.fit(n)
    label_enc = le.transform(n)
    return label_enc
label_enc = prepare_targets(label)
CLASSES = 10
#transform the value of each label to a  binary vector 
target = np.zeros([label_enc.shape[0], CLASSES])
for i in range(label_enc.shape[0]):
     target[i][label_enc[i]] = 1
target.view(type=np.matrix)
print("target",target)



# Create instance of ELM object with 10 hidden neuron
maxx=0
for u in range(10):
    elm = ELM(45, 10, 10)

    # Train test split 80:20
    X_train, X_test, y_train, y_test = train_test_split(n_data, target, test_size=0.34, random_state=1)

    elm.train(X_train,y_train)

    y_pred = elm.predict(X_test)

    # Train data
    correct = 0

    total = y_pred.shape[0]
    for i in range(total):
        predicted = np.argmax(y_pred[i])
        test = np.argmax(y_test[i])
        correct = correct + (1 if predicted == test else 0)    
    print('Accuracy: {:f}'.format(correct/total))
    if(correct/total>maxx):
        maxx=correct/total
print(maxx)
###confusion matrix    
import seaborn as sns
y_pred=np.argmax(y_pred, axis=1)
y_true=(np.argmax(y_test, axis=1))

target_names=["G1","G2","G3","G4","G5","G6","G7","G8","G9","G10"]

cm=confusion_matrix(y_true, y_pred)
#cmn = cm.astype('float')/cm.sum(axis=1)[:, np.newaxis]*100

fig, ax = plt.subplots(figsize=(15,8))
sns.heatmap(cm/np.sum(cm), annot=True, fmt='.2f',xticklabels=target_names,      yticklabels=target_names, cmap='Blues')

#sns.heatmap(cmn, annot=True, fmt='.2%', xticklabels=target_names, yticklabels=target_names)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.ylim(-0.5, len(target_names) + 0.5)
plt.show(block=False)
def perf_measure(y_actual, y_pred):
    TP = 0
    FP = 0
    TN = 0
    FN = 0

for i in range(len(y_pred)): 
    if y_actual[i]==y_pred[i]==1:
       TP += 1
    if y_pred[i]==1 and y_actual[i]!=y_pred[i]:
       FP += 1
    if y_actual[i]==y_pred[i]==0:
       TN += 1
    if y_pred[i]==0 and y_actual[i]!=y_pred[i]:
       FN += 1

return(TP, FP, TN, FN)

TP, FP, TN, FN=perf_measure(y_true, y_pred)
print("precision",TP/(TP+FP))
print("sensivity",TP/(TP+FN))
print("specifity",TN/(TN+FP)) 
print("accuracy",(TP+TN)/(TN+FP+FN+TP))       

【问题讨论】:

  • 嗨,极限学习机只是对感知算法的重新标记,很少有充分的理由使用它们。请参阅 Yann LeCun 的 this。我建议改用其他方法对手势进行分类。
  • 我认为你必须多次训练网络,每次保存你的权重和其他获得的结果,然后选择能给你最好准确率的最好的。可能取决于随机的初始权重。

标签: python classification metrics training-data


【解决方案1】:

关于您是否可以为 ELM 实施迭代训练循环的问题:

不,你不能。 ELM 由一个随机层和一个输出层组成。因为第一层是固定的,所以这本质上是一个线性模型,正如您所指出的,我们可以通过使用伪逆来找到最佳输出权重。

但是,由于您已经一步找到了该模型的完美解决方案,因此没有直接的方法可以迭代地改进此结果。

不过,我不建议使用极限学习机。 除了controversy关于它们的起源之外,它们可以学习的功能非常有限。

还有其他完善的手势分类方法可能更有用。

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 1970-01-01
    • 2019-01-28
    • 2019-08-07
    • 2021-07-12
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多