gpt4 book ai didi

Python 时间序列坐标的二值 RNN 分类

转载 作者:行者123 更新时间:2023-12-01 01:16:53 31 4
gpt4 key购买 nike

我一直在尝试创建一个 RNN。我总共有一个包含 1661 个单独“条目”的数据集,每个条目中有 158 个时间序列坐标。

以下是一个条目的一小部分:

0.00000000e+00  1.92609687e-04  3.85219375e-04  5.77829062e-04
3.00669864e-04 2.35106660e-05 -7.33379576e-04 -1.49026982e-03

这只是一个包含 158 个时间序列值的数组。

现在,我想对一个值数组是否属于条件 A 或条件 B 进行分类。

我查看了很多博客、keras 文档和 YouTube 视频,并提出了以下网络:

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

# Set data and labels

# Somehow find a way to 'unpack' the data
datarnn = np.copy(normalized_data)
datarnn = np.array(rearrange_data(datarnn))
print(len(datarnn))

# Convert labels to binary labels
targetrnn = np.asarray(['1' if 'A' in str(x) else '0' for x in spineMidData_clean[:,0][1:]])

# Split data for training and testing
x_training,x_testing,y_training,y_testing = train_test_split(datarnn,targetrnn,test_size=0.2,random_state=4)

model=Sequential()

# Input layer
model.add(Embedding(1661, 1))

# Hidden layer
model.add(LSTM(3))

# Output layer with binary classification
model.add(Dense(1, activation='sigmoid'))

# Set training settings
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

# Model diagnostics
model.summary()

history = model.fit(x_training,y_training,epochs=20,validation_data=(x_testing,y_testing))

# Predict the test data
results = model.predict(x_testing)

终于看到它起作用了,我感到非常兴奋。然而,我似乎无法提高准确率,一直保持在 50% 左右。有没有办法让这个网络更加准确?例如。我是否添加更多层,或者是否以错误/低效的方式配置了现有层?

最佳答案

确实,添加更多层应该有助于提高准确性。我记得一位作者曾经写过...更深入的内容似乎确实可以带来更好的概括

因此,请看一下我组合在一起的一个不错的 keras 设置。

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("Model")

与往常一样,另一种选择是增大训练数据大小。

希望这有帮助!

干杯!

关于Python 时间序列坐标的二值 RNN 分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262144/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com