簡體   English   中英

多類分類 LSTM keras

[英]Multiclass classification LSTM keras

我一直在為多類分類問題編寫 keras 代碼。 我會暴露我的問題。

我在單個 csv 文件中有一個數據集,該文件的行如下所示

1.45    -10.09  1.02    1   0   0   0

前 3 列代表來自加速度計的 X、Y、Z 加速度。 最后 4 列代表類標簽。 我有4節課

[1 0 0 0][0 1 0 0][0 0 1 0][0 0 0 1]

每個向量都是一個類標簽,對應於一個單一的手勢(握手、旋轉手、舉手、前無古人)

我想要的是讓 LSTM 回顧前 N 個步驟(假設是 20 個),將 N 個 3D 向量 [X,Y,Z] 作為輸入,並在每個序列的末尾(由這 20 個 3D 向量組成)吐出出該序列屬於我擁有的四個類中的任何一個的概率。

問題 1:根據我的理解,模型應該是“多對一”,對嗎? 問題 2:我如何使用 keras API for python 實現這樣的網絡?

我對使用 keras 編碼真的很陌生,到目前為止我所做的是這段代碼(我在 colab 中工作):

    # load dataset
    from google.colab import files
    uploaded = files.upload()
    
    dataframe = pd.read_csv(io.BytesIO(uploaded['data_new.csv']), header=None)
    
  
    dataset = dataframe.values
    X = dataset[:,0:3].astype(float)
    Y = dataset[:,3:].astype(int)
    print(X.shape)
    print(Y.shape)
    model = Sequential()
    ...
    ...

打印語句的輸出是:

(48886, 3)
(48886, 4)

我也會在這里發布數據集的一些行:

1.45    -10.09  1.02    1   0   0   0   
1.06    -10.13  1.06    1   0   0   0   
1.22    -10.09  1.02    1   0   0   0   
1.38    -10.05  1.06    1   0   0   0   
1.03    -10.25  1.18    1   0   0   0   
0.04    -10.17  1.11    1   0   0   0   
0.55    -9.57   1.30    1   0   0   0   
1.18    -9.38   1.26    1   0   0   0   
2.36    -9.22   0.35    1   0   0   0
...
... 

我必須以某種方式重塑輸入嗎?我真的卡住了,請幫幫我

編輯我正在嘗試使用真實數據集使用以下代碼

dataframe = pd.read_csv(io.BytesIO(uploaded['data_new.csv']), header=None)

#dataframe = pandas.read_csv("iris.data", header=None)
dataset = dataframe.values
X = dataset[:,0:3].astype(float)
Y = dataset[:,3:].astype(int)

X_train = tf.expand_dims(X, axis=-1)


BATCH_SIZE = 20 
EPOCHS = 2 # Used less epochs for testing purposes

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(100, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(100, activation="relu"))
model.add(tf.keras.layers.Dense(4, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()
print("Fit the model on training data")
history = model.fit(X_train, Y, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=0)

sample_input = np.random.randint(0,100, size=(20, 3))
print(sample_input)
sample_input_reshaped = tf.expand_dims(sample_input, axis=1)
print(sample_input_reshaped.shape)
predictions = model.predict(sample_input_reshaped)
print('Model predictions', predictions)

我收到這個錯誤

ValueError: Input 0 is incompatible with layer sequential_11: expected shape=(None, None, 1), found shape=(None, 1, 3)

我根據您擁有的數據的輸入形狀制作了一些虛擬數據,並運行了您需要的模型。 您可以擁有比這更復雜的模型,但對於初學者來說,這已經足夠了。

import pandas as pd
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

# Dummy data 
X = pd.DataFrame(np.random.randint(0, 100, size=(48886, 3)), columns=list("ABC"))
y = pd.DataFrame(np.random.randint(0, 2, size=(48886, 4)), columns=list("abcd"))
assert X.shape == (48886, 3)
assert y.shape == (48886, 4)

# You would just need to add the code below to yours
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.25)

X_train = tf.expand_dims(X_train, axis=1)
X_test = tf.expand_dims(X_test, axis=1)

BATCH_SIZE = 20 
EPOCHS = 2 # Used less epochs for testing purposes

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(100, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(100, activation="relu"))
model.add(tf.keras.layers.Dense(4, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()
print("Fit the model on training data")
history = model.fit(X_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=0)
print(f"History: {history.history}")
print("Evaluating on test data")
results = model.evaluate(X_test, y_test, batch_size=BATCH_SIZE)
print("test loss, test acc:", results)

sample_input = np.random.randint(0, 100, size=(20, 3))
sample_input_reshaped = tf.expand_dims(sample_input, axis=1)
predictions = model.predict(sample_input_reshaped)
print('Model predictions', predictions)
prediction_class = tf.argmax(predictions, axis=1)
print('Class of predictions', prediction_class)
most_frequent = np.bincount(prediction_class).argmax()
print('Most frequent class: ', most_frequent)

輸出:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 100)               41600     
_________________________________________________________________
dropout (Dropout)            (None, 100)               0         
_________________________________________________________________
dense (Dense)                (None, 100)               10100     
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 404       
=================================================================
Total params: 52,104
Trainable params: 52,104
Non-trainable params: 0
_________________________________________________________________
Fit the model on training data
2021-07-08 21:23:52.292396: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
History: {'loss': [4.253803730010986, 7.2851409912109375], 'accuracy': [0.25578224658966064, 0.24983635544776917]}
Evaluating on test data
612/612 [==============================] - 1s 964us/step - loss: 15.3499 - accuracy: 0.2468
test loss, test acc: [15.3499116897583, 0.24676811695098877]
Model predictions [[6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.8483874e-03 9.9284607e-01 3.0563556e-04 2.0347565e-08]
 [6.7954701e-03 9.9290127e-01 3.0327393e-04 2.0033218e-08]
 [6.7954701e-03 9.9290127e-01 3.0327393e-04 2.0033218e-08]]
Class of predictions tf.Tensor([1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1], shape=(20,), dtype=int64)
Most frequent class: 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM