Я написал код для сети lstm. мои данные были строковыми, и я использовал word2ve c модель, чтобы преобразовать их в int. Моя цель - класс labe (например, int: fault1, fault2, ...). Но когда я запускаю код, точность всегда равна 1 или 0. Что не так? Разве для этой точности не для целевых? Функция потери неверна?
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import gensim
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, LSTM, Flatten
from keras.layers import Embedding, Input
from keras.preprocessing import sequence
alarm=pd.read_excel('alarmdata.xlsx')
alarm_test=pd.read_excel('testdata.xlsx')
x=alarm['Alarm_tag']
y=alarm['Fault']
x_test=alarm_test['Alarm_tag']
y_test=alarm_test['Fault']
v=x.unique().tolist()
n=x.nunique()
v_test=x_test.unique().tolist()
n_test=x_test.nunique()
c=0
l=0
w=0
k=[[] for j in range(1261)]
q=[[] for j in range(1261)]
for i in range(6305):
if l<6305:
while w<5:
k[c].append(x.loc[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
c=0
l=0
w=0
z=np.reshape(k, (1261,5))
for i in range(6305):
if l<6305:
while w<5:
q[c].append(y.loc[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
r=np.reshape(q, (1261,5))
#testdata
c=0
l=0
w=0
k_test=[[] for j in range(240)]
q_test=[[] for j in range(240)]
for i in range(1200):
if l<1200:
while w<5:
k_test[c].append(x_test.loc[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
c=0
l=0
w=0
z_test=np.reshape(k_test, (240,5))
for i in range(1200):
if l<1200:
while w<5:
q_test[c].append(y_test.loc[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
r_test=np.reshape(q_test, (240,5))
c=0
l=0
w=0
all_alarm=[[] for j in range(6305)]
model = gensim.models.Word2Vec(k, size=10, window=1, min_count=1, workers=10, iter=10)
X = model[model.wv.vocab]
for i in range(46):
specific_alarm=v[i]
for j in range(6305):
if x[j]== specific_alarm:
all_alarm[j]=X[i]
c=0
l=0
w=0
g=[[] for j in range(1261)]
for i in range(6305):
if l<6305:
while w<5:
g[c].append(all_alarm[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
for i in range(46):
specific_alarm=v[i]
for j in range(6305):
if x[j]== specific_alarm:
all_alarm[j]=X[i]
c=0
l=0
w=0
g=[[] for j in range(1261)]
for i in range(6305):
if l<6305:
while w<5:
g[c].append(all_alarm[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
b = np.asarray(g)
#x_train, x_test, y_train, y_test = train_test_split(b,r, test_size=0.3)
#testdata
c=0
l=0
w=0
all_alarmtest=[[] for j in range(1200)]
model2 = gensim.models.Word2Vec(k_test, size=10, window=1, min_count=1, workers=10, iter=10)
X = model[model.wv.vocab]
for i in range(n_test):
specific_alarmtest=v_test[i]
for j in range(1200):
if x_test[j]== specific_alarmtest:
all_alarmtest[j]=X[i]
c=0
l=0
w=0
g_test=[[] for j in range(240)]
for i in range(1200):
if l<1200:
while w<5:
g_test[c].append(all_alarmtest[l])
w=w+1
l=l+1
w=0
c=c+1
i=l+5
b_test=np.asarray(g_test)
b = np.asarray(g)
#train model
model= Sequential()
#model.add(Embedding(input_dim=1261, output_dim=1024, input_length=10))
model.add(LSTM(64, return_sequences=True, input_shape=(5,10,)))
model.add(LSTM(32))
model.add(Dense(5, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['mse'])
model.fit(b, r, validation_data=(b_test, r_test), epochs = 100, batch_size =1261)
#model.fit(b, r, epochs = 100, batch_size =100)`